diff --git a/CMakeLists.txt b/CMakeLists.txt index 0efb83f..b7cc61e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,6 +64,7 @@ add_executable(App ./src/main.cpp src/DAO/MySQLUserTreatmentSchemesDAO.cpp src/DAO/MySQLUserTreatmentSchemesDAO.h src/endpoints_handlers/GetUserTreatmentSchemeExecutor.h + src/endpoints_handlers/GetUserDiariesExecutor.h src/DAO/IDiariesDAO.h src/dtos/diary_dto.h src/DAO/MySQLDiariesDao.cpp diff --git a/src/DAO/MySQLUserTreatmentSchemesDAO.cpp b/src/DAO/MySQLUserTreatmentSchemesDAO.cpp index c4567f3..6661c59 100644 --- a/src/DAO/MySQLUserTreatmentSchemesDAO.cpp +++ b/src/DAO/MySQLUserTreatmentSchemesDAO.cpp @@ -32,29 +32,33 @@ std::vector MySQLUserTreatmentSchemesDAO::FindByUserL std::unordered_map scheme_map; - for (const mysqlx::Row& row : result) { + for (const mysqlx::Row& row : result) + { const std::string scheme_uuid = row[0].get(); - if (scheme_map.find(scheme_uuid) == scheme_map.end()) { + 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(); - dto.instructions = row[2].isNull() ? "" : row[2].get(); + dto.instructions = row[2].isNull() ? "" : row[2].get(); scheme_map.emplace(scheme_uuid, std::move(dto)); } - if (!row[3].isNull()) { + if (!row[3].isNull()) + { scheme_map[scheme_uuid] - .medication_uuids - .push_back(row[3].get()); + .medication_uuids + .push_back(row[3].get()); } } std::vector schemes; schemes.reserve(scheme_map.size()); - for (auto& [_, dto] : scheme_map) { + for (auto& [_, dto] : scheme_map) + { schemes.push_back(std::move(dto)); } diff --git a/src/endpoints_handlers/GetUserDiariesExecutor.h b/src/endpoints_handlers/GetUserDiariesExecutor.h new file mode 100644 index 0000000..fac1ecd --- /dev/null +++ b/src/endpoints_handlers/GetUserDiariesExecutor.h @@ -0,0 +1,110 @@ +#pragma once +#include + +#include +#include +#include +#include + +#include "IExecutor.h" +#include "../DAO/IAuthDAO.h" +#include "../DAO/IUserTreatmentSchemesDAO.h" +#include "../exceptions/session_exception.h" + +namespace uad +{ +template +class GetUserDiariesExecutor : public IExecutor +{ + mysqlx::Session& session_; + const std::shared_ptr& auth_dao_; + const std::shared_ptr& user_treatment_scheme_dao_; + +public: + GetUserDiariesExecutor( + mysqlx::Session& session, + const std::shared_ptr& auth_dao, + const std::shared_ptr& user_treatment_scheme_dao + ) : session_(session), auth_dao_(auth_dao), user_treatment_scheme_dao_(user_treatment_scheme_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(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 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& 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); + } +}; +}