generated from Sithas/conan_template
Разработка ручки для GetAllMedications
This commit is contained in:
+1
-1
@@ -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
|
||||
|
||||
+2
-2
@@ -14,13 +14,13 @@
|
||||
namespace uad
|
||||
{
|
||||
template <class Body, class Allocator, class ResponseType>
|
||||
class UserGetMedicationsExecutor : public IExecutor<Body, Allocator, ResponseType>
|
||||
class GetUserMedicationsExecutor : public IExecutor<Body, Allocator, ResponseType>
|
||||
{
|
||||
mysqlx::Session& session_;
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
||||
const std::shared_ptr<IMedicationsDAO>& medications_dao_;
|
||||
public:
|
||||
UserGetMedicationsExecutor(
|
||||
GetUserMedicationsExecutor(
|
||||
mysqlx::Session& session,
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao,
|
||||
const std::shared_ptr<IMedicationsDAO>& medications_dao
|
||||
@@ -0,0 +1,105 @@
|
||||
#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/IMedicationsDAO.h"
|
||||
#include "../exceptions/session_exception.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
template <class Body, class Allocator, class ResponseType>
|
||||
class GetUserMedicationsExecutor : public IExecutor<Body, Allocator, ResponseType>
|
||||
{
|
||||
mysqlx::Session& session_;
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
||||
const std::shared_ptr<IMedicationsDAO>& medications_dao_;
|
||||
public:
|
||||
GetUserMedicationsExecutor(
|
||||
mysqlx::Session& session,
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao,
|
||||
const std::shared_ptr<IMedicationsDAO>& medications_dao
|
||||
): session_(session), auth_dao_(auth_dao), medications_dao_(medications_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(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<ResponseType> res{http::status::ok, req.version()};
|
||||
const std::vector<medication> 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<medication>& 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;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -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<Body, Allocator, boost::beast::http::string_body>;
|
||||
using RouteController = Controller<Body, Allocator, boost::beast::http::string_body>;
|
||||
|
||||
Reference in New Issue
Block a user