From 1cb1a9d0eb2abf4305c185523267af4207c20720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Thu, 8 Jan 2026 11:53:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B0=D0=B7=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=BA=D0=B0=20=D1=80=D1=83=D1=87=D0=BA=D0=B8=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20GetAllMedications?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- ...xecutor.h => GetUserMedicationsExecutor.h} | 4 +- .../PostUserMedicationsExecutor.h | 105 ++++++++++++++++++ src/endpoints_handlers/RootExecutor.h | 4 +- 4 files changed, 110 insertions(+), 5 deletions(-) rename src/endpoints_handlers/{UserGetMedicationsExecutor.h => GetUserMedicationsExecutor.h} (96%) create mode 100644 src/endpoints_handlers/PostUserMedicationsExecutor.h diff --git a/CMakeLists.txt b/CMakeLists.txt index d4c5d2f..785cc1e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -55,7 +55,7 @@ add_executable(App ./src/main.cpp src/log/Log.h src/log/Log.cpp tests/fixtures/AuthFixture.h - src/endpoints_handlers/UserGetMedicationsExecutor.h + src/endpoints_handlers/GetUserMedicationsExecutor.h src/DAO/IMedicationsDAO.h src/DAO/MySQLMedicationsDAO.h src/DAO/MySQLMedicationsDAO.cpp diff --git a/src/endpoints_handlers/UserGetMedicationsExecutor.h b/src/endpoints_handlers/GetUserMedicationsExecutor.h similarity index 96% rename from src/endpoints_handlers/UserGetMedicationsExecutor.h rename to src/endpoints_handlers/GetUserMedicationsExecutor.h index 152b5bd..71b189a 100644 --- a/src/endpoints_handlers/UserGetMedicationsExecutor.h +++ b/src/endpoints_handlers/GetUserMedicationsExecutor.h @@ -14,13 +14,13 @@ namespace uad { template -class UserGetMedicationsExecutor : public IExecutor +class GetUserMedicationsExecutor : public IExecutor { mysqlx::Session& session_; const std::shared_ptr& auth_dao_; const std::shared_ptr& medications_dao_; public: - UserGetMedicationsExecutor( + GetUserMedicationsExecutor( mysqlx::Session& session, const std::shared_ptr& auth_dao, const std::shared_ptr& medications_dao diff --git a/src/endpoints_handlers/PostUserMedicationsExecutor.h b/src/endpoints_handlers/PostUserMedicationsExecutor.h new file mode 100644 index 0000000..71b189a --- /dev/null +++ b/src/endpoints_handlers/PostUserMedicationsExecutor.h @@ -0,0 +1,105 @@ +#pragma once +#include + +#include +#include +#include +#include + +#include "IExecutor.h" +#include "../DAO/IAuthDAO.h" +#include "../DAO/IMedicationsDAO.h" +#include "../exceptions/session_exception.h" + +namespace uad +{ +template +class GetUserMedicationsExecutor : public IExecutor +{ + mysqlx::Session& session_; + const std::shared_ptr& auth_dao_; + const std::shared_ptr& medications_dao_; +public: + GetUserMedicationsExecutor( + mysqlx::Session& session, + const std::shared_ptr& auth_dao, + const std::shared_ptr& medications_dao + ): session_(session), auth_dao_(auth_dao), medications_dao_(medications_dao) + { + + } + + boost::beast::http::response operator ()( + boost::beast::http::request>&& 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(info) << 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(info) << invalid_token_message; + throw session_exception(http::status::unauthorized, "Unauthorized"); + } + + http::response res{http::status::ok, req.version()}; + const std::vector medications = medications_dao_->GetAll(); + value response_body; + + response_body.emplace_object(); + response_body.as_object().emplace("medications", toJSONArray(medications)); + + res.body() = serialize(response_body); + res.set(http::field::content_type, "application/json"); + res.content_length(res.body().size()); + + return res; + } + +private: + boost::json::object ToJSON(const medication& m) + { + return { + { "uuid", m.uuid }, + { "name", m.name }, + { "dose", m.dose }, + { "unit", m.unit }, + { "is_urgent", m.is_urgent } + }; + } + + boost::json::array toJSONArray(const std::vector& medications) + { + 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 : medications) + { + arr.emplace_back(ToJSON(m)); + } + + return arr; + } +}; +} diff --git a/src/endpoints_handlers/RootExecutor.h b/src/endpoints_handlers/RootExecutor.h index 0555657..be3c272 100644 --- a/src/endpoints_handlers/RootExecutor.h +++ b/src/endpoints_handlers/RootExecutor.h @@ -7,7 +7,7 @@ #include "AuthRegistrationExecutor.h" #include "AuthLoginExecutor.h" #include "AuthLogoutExecutor.h" -#include "UserGetMedicationsExecutor.h" +#include "GetUserMedicationsExecutor.h" #include "../DAO/IUserDAO.h" #include "../DAO/IAuthDAO.h" #include "../DAO/IMedicationsDAO.h" @@ -26,7 +26,7 @@ class RootExecutor Body, Allocator, boost::beast::http::string_body>; using RouteAuthLogoutExecutor = AuthLogoutExecutor< Body, Allocator, boost::beast::http::string_body>; - using RouteUserGetMedicationsExecutor = UserGetMedicationsExecutor< + using RouteUserGetMedicationsExecutor = GetUserMedicationsExecutor< Body, Allocator, boost::beast::http::string_body>; using IRouteController = IController; using RouteController = Controller;