From 8df55238be7874941a1b6b154142815bb029e847 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 5 Jan 2026 15:29:47 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=94=D0=90=D0=9E=20=D0=B4=D0=BB=D1=8F=20?= =?UTF-8?q?=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=B2=20=D0=BD=D0=BE=D0=B2=D0=BE=D0=BC=20?= =?UTF-8?q?=D0=BC=D0=B0=D1=80=D1=88=D1=80=D1=83=D1=82=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 + src/DAO/IMedicationsDAO.h | 4 +- src/DAO/MySQLMedicationsDAO.cpp | 52 ++++++++++++++++++++++++++ src/DAO/MySQLMedicationsDAO.h | 21 +++++++++++ src/endpoints_handlers/HandleRequest.h | 6 ++- src/endpoints_handlers/RootExecutor.h | 7 +++- src/entities/medication.h | 6 ++- 7 files changed, 91 insertions(+), 7 deletions(-) create mode 100644 src/DAO/MySQLMedicationsDAO.cpp create mode 100644 src/DAO/MySQLMedicationsDAO.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0ba2b85..d4c5d2f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,6 +57,8 @@ add_executable(App ./src/main.cpp tests/fixtures/AuthFixture.h src/endpoints_handlers/UserGetMedicationsExecutor.h src/DAO/IMedicationsDAO.h + src/DAO/MySQLMedicationsDAO.h + src/DAO/MySQLMedicationsDAO.cpp ) target_link_libraries(App PRIVATE Boost::boost diff --git a/src/DAO/IMedicationsDAO.h b/src/DAO/IMedicationsDAO.h index f4e7ee2..838c0c2 100644 --- a/src/DAO/IMedicationsDAO.h +++ b/src/DAO/IMedicationsDAO.h @@ -10,9 +10,9 @@ namespace uad class IMedicationsDAO { public: - virtual std::vector GetAll() = 0; + [[nodiscard]] virtual std::vector GetAll() const = 0; - virtual std::string Create(medication) = 0; + [[nodiscard]] virtual std::string Create(const medication& m) const = 0; virtual ~IMedicationsDAO() = default; }; diff --git a/src/DAO/MySQLMedicationsDAO.cpp b/src/DAO/MySQLMedicationsDAO.cpp new file mode 100644 index 0000000..f5dbe88 --- /dev/null +++ b/src/DAO/MySQLMedicationsDAO.cpp @@ -0,0 +1,52 @@ +#include + +#include "MySQLMedicationsDAO.h" + +using namespace std; + +namespace uad +{ +MySQLMedicationsDAO::MySQLMedicationsDAO(mysqlx::Session& session): session_(session) +{ +} + +std::vector MySQLMedicationsDAO::GetAll() const +{ + static const string sql_script = "SELECT uuid, name, dose, unit, is_urgent FROM up_and_down.medications;"; + + mysqlx::SqlResult sql_result = session_.sql(sql_script).execute(); + list rows = sql_result.fetchAll(); + + vector ret; + + if (rows.empty()) return ret; + + for (const auto& row : rows) + { + medication m; + + m.uuid = row[0].get(); + m.name = row[1].get(); + m.dose = row[2].get(); + m.unit = row[3].get(); + m.is_urgent = row[4].get(); + + ret.push_back(m); + } + + return ret; +} + +std::string MySQLMedicationsDAO::Create(const medication& m) const +{ + boost::uuids::random_generator generator; + boost::uuids::uuid uuid = generator(); + const std::string uuid_str = boost::uuids::to_string(uuid); + + static const string sql_script = "INSERT INTO `up_and_down`.`medications` (`uuid`, `name`, `dose`, `unit`, `is_urgent`) VALUES ( ?, ?, ?, ?, ?);"s; + + session_.sql(sql_script).bind(uuid_str, m.name, m.dose, m.unit, m.is_urgent).execute(); + + return uuid_str; +} +} diff --git a/src/DAO/MySQLMedicationsDAO.h b/src/DAO/MySQLMedicationsDAO.h new file mode 100644 index 0000000..baa6a03 --- /dev/null +++ b/src/DAO/MySQLMedicationsDAO.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include +#include + +#include "IMedicationsDAO.h" +#include "../entities/medication.h" + +namespace uad +{ +class MySQLMedicationsDAO : public IMedicationsDAO +{ + mysqlx::Session& session_; +public: + explicit MySQLMedicationsDAO(mysqlx::Session& session); + + [[nodiscard]] std::vector GetAll() const override; + + [[nodiscard]] std::string Create(const medication& m) const override; +}; +} diff --git a/src/endpoints_handlers/HandleRequest.h b/src/endpoints_handlers/HandleRequest.h index 3f3b008..77422a3 100644 --- a/src/endpoints_handlers/HandleRequest.h +++ b/src/endpoints_handlers/HandleRequest.h @@ -4,10 +4,12 @@ #include "../db/mysql_connector.h" #include "../DAO/IUserDAO.h" +#include "../DAO/IMedicationsDAO.h" #include "AuthRegistrationExecutor.h" #include "RootExecutor.h" #include "../DAO/MemoryAuthDAO.h" #include "../DAO/MySQLUserDAO.h" +#include "../DAO/MySQLMedicationsDAO.h" namespace uad { @@ -19,11 +21,13 @@ void HandleRequest( { static std::shared_ptr user_dao = std::make_shared(GetMySqlSession()); static std::shared_ptr auth_dao = std::make_shared(GetMySqlSession()); + static std::shared_ptr medications_dao = std::make_shared(GetMySqlSession()); static RootExecutor root_executor( GetMySqlSession(), user_dao, - auth_dao + auth_dao, + medications_dao ); root_executor(doc_root, std::move(req), std::forward(send)); diff --git a/src/endpoints_handlers/RootExecutor.h b/src/endpoints_handlers/RootExecutor.h index be91570..ca3659f 100644 --- a/src/endpoints_handlers/RootExecutor.h +++ b/src/endpoints_handlers/RootExecutor.h @@ -10,6 +10,7 @@ #include "UserGetMedicationsExecutor.h" #include "../DAO/IUserDAO.h" #include "../DAO/IAuthDAO.h" +#include "../DAO/IMedicationsDAO.h" #include "./../helpers/helpers.h" #include "./../exceptions/session_exception.h" @@ -40,13 +41,15 @@ private: mysqlx::Session& session_; const std::shared_ptr& user_dao_; const std::shared_ptr& auth_dao_; + const std::shared_ptr& medications_dao_; public: RootExecutor( mysqlx::Session& session, const std::shared_ptr& user_dao, - const std::shared_ptr& auth_dao) : - session_(session), user_dao_(user_dao), auth_dao_(auth_dao) + const std::shared_ptr& auth_dao, + const std::shared_ptr& medications_dao) : + session_(session), user_dao_(user_dao), auth_dao_(auth_dao), medications_dao_(medications_dao) { routes_pathes_["/api/v1/Auth/Register"] = std::make_unique( typename RouteController::HTTPMethodsToExecutors{ diff --git a/src/entities/medication.h b/src/entities/medication.h index 4644577..30b3f50 100644 --- a/src/entities/medication.h +++ b/src/entities/medication.h @@ -9,7 +9,9 @@ namespace uad struct medication { std::string uuid; - std::string login; - std::string hashed_password; + std::string name; + int64_t dose; + std::string unit; + bool is_urgent; }; }