diff --git a/CMakeLists.txt b/CMakeLists.txt index 8bd66d7..946c041 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -63,7 +63,7 @@ add_executable(App ./src/main.cpp src/DAO/IUserTreatmentSchemesDAO.h src/DAO/MySQLUserTreatmentSchemesDAO.cpp src/DAO/MySQLUserTreatmentSchemesDAO.h - src/endpoints_handlers/GetUserTreatmentSchemesExecutor.h + src/endpoints_handlers/GetUserTreatmentSchemeExecutor.h ) target_link_libraries(App PRIVATE Boost::boost diff --git a/src/endpoints_handlers/GetUserTreatmentSchemeExecutor.h b/src/endpoints_handlers/GetUserTreatmentSchemeExecutor.h new file mode 100644 index 0000000..5716afc --- /dev/null +++ b/src/endpoints_handlers/GetUserTreatmentSchemeExecutor.h @@ -0,0 +1,66 @@ +#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 GetUserTreatmentSchemeExecutor : public IExecutor +{ + mysqlx::Session& session_; + const std::shared_ptr& auth_dao_; + const std::shared_ptr& user_treatment_scheme_dao_; +public: + GetUserTreatmentSchemeExecutor( + 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"); + } + + http::response res{http::status::ok, req.version()}; + + return res; + } +}; +} diff --git a/src/endpoints_handlers/GetUserTreatmentSchemesExecutor.h b/src/endpoints_handlers/GetUserTreatmentSchemesExecutor.h deleted file mode 100644 index e69de29..0000000 diff --git a/src/endpoints_handlers/RootExecutor.h b/src/endpoints_handlers/RootExecutor.h index f605348..c10aaf4 100644 --- a/src/endpoints_handlers/RootExecutor.h +++ b/src/endpoints_handlers/RootExecutor.h @@ -8,6 +8,7 @@ #include "AuthLoginExecutor.h" #include "AuthLogoutExecutor.h" #include "GetUserMedicationsExecutor.h" +#include "GetUserTreatmentSchemeExecutor.h" #include "PostUserMedicationsExecutor.h" #include "../DAO/IUserDAO.h" #include "../DAO/IAuthDAO.h" @@ -28,9 +29,11 @@ class RootExecutor Body, Allocator, boost::beast::http::string_body>; using RouteAuthLogoutExecutor = AuthLogoutExecutor< Body, Allocator, boost::beast::http::string_body>; - using RouteUserGetMedicationsExecutor = GetUserMedicationsExecutor< + using RouteGetUserMedicationsExecutor = GetUserMedicationsExecutor< Body, Allocator, boost::beast::http::string_body>; - using RouteUserPostMedicationsExecutor = PostUserMedicationsExecutor< + using RoutePostUserMedicationsExecutor = PostUserMedicationsExecutor< + Body, Allocator, boost::beast::http::string_body>; + using RouteGetUserTreatmentSchemeExecutor = GetUserTreatmentSchemeExecutor< Body, Allocator, boost::beast::http::string_body>; using IRouteController = IController; using RouteController = Controller; @@ -93,11 +96,20 @@ public: typename RouteController::HTTPMethodsToExecutors{ { boost::beast::http::verb::get, - std::make_shared(session_, auth_dao_, medications_dao_) + std::make_shared(session_, auth_dao_, medications_dao_) }, { boost::beast::http::verb::post, - std::make_shared(session_, auth_dao_, medications_dao_) + std::make_shared(session_, auth_dao_, medications_dao_) + }, + } + ); + + routes_pathes_["api/v1/UserTreatmentSchemes"] = std::make_unique( + typename RouteController::HTTPMethodsToExecutors{ + { + boost::beast::http::verb::get, + std::make_shared(session_, auth_dao_, user_treatment_scheme_dao_) }, } );