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

This commit is contained in:
2026-01-16 08:08:11 +03:00
parent f56c14b3da
commit 08943b5938
3 changed files with 122 additions and 7 deletions
+1
View File
@@ -64,6 +64,7 @@ add_executable(App ./src/main.cpp
src/DAO/MySQLUserTreatmentSchemesDAO.cpp src/DAO/MySQLUserTreatmentSchemesDAO.cpp
src/DAO/MySQLUserTreatmentSchemesDAO.h src/DAO/MySQLUserTreatmentSchemesDAO.h
src/endpoints_handlers/GetUserTreatmentSchemeExecutor.h src/endpoints_handlers/GetUserTreatmentSchemeExecutor.h
src/endpoints_handlers/GetUserDiariesExecutor.h
src/DAO/IDiariesDAO.h src/DAO/IDiariesDAO.h
src/dtos/diary_dto.h src/dtos/diary_dto.h
src/DAO/MySQLDiariesDao.cpp src/DAO/MySQLDiariesDao.cpp
+8 -4
View File
@@ -32,10 +32,12 @@ std::vector<user_treatment_scheme_dto> MySQLUserTreatmentSchemesDAO::FindByUserL
std::unordered_map<std::string, user_treatment_scheme_dto> scheme_map; std::unordered_map<std::string, user_treatment_scheme_dto> scheme_map;
for (const mysqlx::Row& row : result) { for (const mysqlx::Row& row : result)
{
const std::string scheme_uuid = row[0].get<std::string>(); const std::string scheme_uuid = row[0].get<std::string>();
if (scheme_map.find(scheme_uuid) == scheme_map.end()) { if (scheme_map.find(scheme_uuid) == scheme_map.end())
{
user_treatment_scheme_dto dto; user_treatment_scheme_dto dto;
dto.uuid = scheme_uuid; dto.uuid = scheme_uuid;
dto.treatment_name = row[1].isNull() ? "" : row[1].get<std::string>(); dto.treatment_name = row[1].isNull() ? "" : row[1].get<std::string>();
@@ -44,7 +46,8 @@ std::vector<user_treatment_scheme_dto> MySQLUserTreatmentSchemesDAO::FindByUserL
scheme_map.emplace(scheme_uuid, std::move(dto)); scheme_map.emplace(scheme_uuid, std::move(dto));
} }
if (!row[3].isNull()) { if (!row[3].isNull())
{
scheme_map[scheme_uuid] scheme_map[scheme_uuid]
.medication_uuids .medication_uuids
.push_back(row[3].get<std::string>()); .push_back(row[3].get<std::string>());
@@ -54,7 +57,8 @@ std::vector<user_treatment_scheme_dto> MySQLUserTreatmentSchemesDAO::FindByUserL
std::vector<user_treatment_scheme_dto> schemes; std::vector<user_treatment_scheme_dto> schemes;
schemes.reserve(scheme_map.size()); schemes.reserve(scheme_map.size());
for (auto& [_, dto] : scheme_map) { for (auto& [_, dto] : scheme_map)
{
schemes.push_back(std::move(dto)); schemes.push_back(std::move(dto));
} }
@@ -0,0 +1,110 @@
#pragma once
#include <boost/log/trivial.hpp>
#include <regex>
#include <boost/json.hpp>
#include <boost/mpl/vector/vector0.hpp>
#include <mysqlx/xdevapi.h>
#include "IExecutor.h"
#include "../DAO/IAuthDAO.h"
#include "../DAO/IUserTreatmentSchemesDAO.h"
#include "../exceptions/session_exception.h"
namespace uad
{
template <class Body, class Allocator, class ResponseType>
class GetUserDiariesExecutor : public IExecutor<Body, Allocator, ResponseType>
{
mysqlx::Session& session_;
const std::shared_ptr<IAuthDAO>& auth_dao_;
const std::shared_ptr<IUserTreatmentSchemeDAO>& user_treatment_scheme_dao_;
public:
GetUserDiariesExecutor(
mysqlx::Session& session,
const std::shared_ptr<IAuthDAO>& auth_dao,
const std::shared_ptr<IUserTreatmentSchemeDAO>& user_treatment_scheme_dao
) : session_(session), auth_dao_(auth_dao), user_treatment_scheme_dao_(user_treatment_scheme_dao)
{
}
boost::beast::http::response<ResponseType> operator ()(
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>>&& req
) override
{
using namespace boost;
using namespace boost::json;
using namespace boost::beast;
using namespace std::string_literals;
using namespace std::string_view_literals;
constexpr std::string_view auth_prefix = "Bearer "sv;
static const std::string invalid_token_message =
"GET /api/v1/User/Medications - Response 401: Unauthorized"s;
BOOST_LOG_TRIVIAL(info) << "GET /api/v1/User/Medications - Request";
if (req[http::field::authorization].size() <= auth_prefix.size())
{
BOOST_LOG_TRIVIAL(error) << invalid_token_message;
throw session_exception(http::status::unauthorized, "Unauthorized");
}
const std::string auth_token = {
req[http::field::authorization].begin() + auth_prefix.size(),
req[http::field::authorization].end()
};
if (!auth_dao_->HasAuthorized(auth_token))
{
BOOST_LOG_TRIVIAL(error) << invalid_token_message;
throw session_exception(http::status::unauthorized, "Unauthorized");
}
std::string_view user_login_ref = auth_dao_->GetLogin(auth_token);
const std::string user_login{user_login_ref.begin(), user_login_ref.end()};
http::response<ResponseType> res{http::status::ok, req.version()};
return res;
}
private:
boost::json::object ToJSON(const user_treatment_scheme_dto& scheme)
{
boost::json::array medications_json;
medications_json.reserve(scheme.medication_uuids.size());
for (const auto& medication_uuid : scheme.medication_uuids) {
medications_json.emplace_back(medication_uuid);
}
boost::json::object scheme_json;
scheme_json["uuid"] = scheme.uuid;
scheme_json["treatment_name"] = scheme.treatment_name;
scheme_json["instructions"] = scheme.instructions;
scheme_json["medications"] = std::move(medications_json);
return scheme_json;
}
boost::json::array toJSONArray(const std::vector<user_treatment_scheme_dto>& schemes)
{
using namespace boost;
using namespace boost::json;
using namespace boost::beast;
using namespace std::string_literals;
using namespace std::string_view_literals;
json::array arr;
for (const auto& m : schemes)
{
arr.emplace_back(ToJSON(m));
}
return std::move(arr);
}
};
}