База для ручки PostUserMedicationsExecutor.h

This commit is contained in:
2026-01-10 13:32:44 +03:00
parent a4d5384fd3
commit 8eb44b6e45
8 changed files with 119 additions and 48 deletions
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include <vector>
#include <string>
#include "./../dtos/user_treatment_scheme_dto.h"
namespace uad
{
class IUserTreatmentSchemeDAO {
public:
virtual std::vector<user_treatment_scheme_dto> FindByUserLogin(const std::string& login) = 0;
virtual ~IUserTreatmentSchemeDAO() = default;
};
}
+66
View File
@@ -0,0 +1,66 @@
#include <mysqlx/xdevapi.h>
#include <unordered_map>
#include "MySQLUserTreatmentSchemesDAO.h"
namespace uad
{
MySQLUserTreatmentSchemesDAO::MySQLUserTreatmentSchemesDAO(mysqlx::Session& session)
: session_(session)
{
}
std::vector<user_treatment_scheme_dto> MySQLUserTreatmentSchemesDAO::FindByUserLogin(
const std::string& login)
{
const std::string query = R"(
SELECT
uts.uuid,
uts.treatment_name,
uts.instructions,
ts.medication_uuid
FROM up_and_down.users u
JOIN up_and_down.user_treatment_schemes uts
ON uts.user_uuid = u.uuid
LEFT JOIN up_and_down.treatment_schemes ts
ON ts.user_treatment_schemes_uuid = uts.uuid
WHERE u.login = ?
ORDER BY uts.uuid
)";
mysqlx::SqlStatement stmt = session_.sql(query);
stmt.bind(login);
mysqlx::SqlResult result = stmt.execute();
std::unordered_map<std::string, user_treatment_scheme_dto> scheme_map;
for (const mysqlx::Row& row : result) {
const std::string scheme_uuid = row[0].get<std::string>();
if (scheme_map.find(scheme_uuid) == scheme_map.end()) {
user_treatment_scheme_dto dto;
dto.uuid = scheme_uuid;
dto.treatment_name = row[1].isNull() ? "" : row[1].get<std::string>();
dto.instructions = row[2].isNull() ? "" : row[2].get<std::string>();
scheme_map.emplace(scheme_uuid, std::move(dto));
}
if (!row[3].isNull()) {
scheme_map[scheme_uuid]
.medication_uuids
.push_back(row[3].get<std::string>());
}
}
std::vector<user_treatment_scheme_dto> schemes;
schemes.reserve(scheme_map.size());
for (auto& [_, dto] : scheme_map) {
schemes.push_back(std::move(dto));
}
return schemes;
}
} // uad
+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include "IUserTreatmentSchemesDAO.h"
#include <mysqlx/xdevapi.h>
namespace uad
{
class MySQLUserTreatmentSchemesDAO : public IUserTreatmentSchemeDAO
{
mysqlx::Session& session_;
public:
explicit MySQLUserTreatmentSchemesDAO(mysqlx::Session& session);
std::vector<user_treatment_scheme_dto> FindByUserLogin(const std::string& login) override;
};
} // uad
-11
View File
@@ -1,11 +0,0 @@
#pragma once
#include "medication_dto.h"
namespace uad
{
struct treatment_scheme_dto {
std::string scheme_uuid;
medication_dto medication;
};
}
+5 -3
View File
@@ -1,6 +1,7 @@
#pragma once
#include "treatment_scheme_dto.h"
#include <string>
#include <vector>
namespace uad
{
@@ -8,7 +9,8 @@ struct user_treatment_scheme_dto {
std::string uuid;
std::string user_uuid;
std::string treatment_name;
std::optional<std::string> instructions;
std::vector<treatment_scheme_dto> medications;
std::string instructions;
std::vector<std::string> medication_uuids;
};
}