generated from Sithas/conan_template
Ручка PostUserTreatmentScheme.h
This commit is contained in:
@@ -10,6 +10,11 @@ class IUserTreatmentSchemeDAO {
|
||||
public:
|
||||
virtual std::vector<user_treatment_scheme_dto> FindByUserUUID(const std::string& login) = 0;
|
||||
|
||||
virtual void CreateUserTreatmentScheme(
|
||||
const std::string& user_login,
|
||||
const user_treatment_scheme_dto& dto
|
||||
) = 0;
|
||||
|
||||
virtual ~IUserTreatmentSchemeDAO() = default;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -67,4 +67,46 @@ std::vector<user_treatment_scheme_dto> MySQLUserTreatmentSchemesDAO::FindByUserU
|
||||
|
||||
return std::move(schemes);
|
||||
}
|
||||
|
||||
void MySQLUserTreatmentSchemesDAO::CreateUserTreatmentScheme(
|
||||
const std::string& user_uuid,
|
||||
const user_treatment_scheme_dto& dto)
|
||||
{
|
||||
session_.startTransaction();
|
||||
|
||||
try {
|
||||
session_.sql(R"(
|
||||
INSERT INTO up_and_down.user_treatment_schemes (
|
||||
uuid,
|
||||
user_uuid,
|
||||
treatment_name,
|
||||
instructions
|
||||
) VALUES (?, ?, ?, ?)
|
||||
)")
|
||||
.bind(
|
||||
dto.uuid,
|
||||
user_uuid,
|
||||
dto.treatment_name,
|
||||
dto.instructions
|
||||
)
|
||||
.execute();
|
||||
|
||||
for (const auto& medication_uuid : dto.medication_uuids) {
|
||||
session_.sql(R"(
|
||||
INSERT INTO up_and_down.treatment_schemes (
|
||||
user_treatment_schemes_uuid,
|
||||
medication_uuid
|
||||
) VALUES (?, ?)
|
||||
)")
|
||||
.bind(dto.uuid, medication_uuid)
|
||||
.execute();
|
||||
}
|
||||
|
||||
session_.commit();
|
||||
}
|
||||
catch (...) {
|
||||
session_.rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
} // uad
|
||||
|
||||
@@ -12,5 +12,10 @@ public:
|
||||
explicit MySQLUserTreatmentSchemesDAO(mysqlx::Session& session);
|
||||
|
||||
std::vector<user_treatment_scheme_dto> FindByUserUUID(const std::string& uuid) override;
|
||||
|
||||
void CreateUserTreatmentScheme(
|
||||
const std::string& user_login,
|
||||
const user_treatment_scheme_dto& dto
|
||||
) override;
|
||||
};
|
||||
} // uad
|
||||
|
||||
@@ -41,9 +41,9 @@ public:
|
||||
|
||||
constexpr std::string_view auth_prefix = "Bearer "sv;
|
||||
static const std::string invalid_token_message =
|
||||
"GET /api/v1/User/Medications - Response 401: Unauthorized"s;
|
||||
"GET /api/v1/UserTreatmentSchemes - Response 401: Unauthorized"s;
|
||||
|
||||
BOOST_LOG_TRIVIAL(info) << "GET /api/v1/User/Medications - Request";
|
||||
BOOST_LOG_TRIVIAL(info) << "GET /api/v1/UserTreatmentSchemes - Request";
|
||||
|
||||
if (req[http::field::authorization].size() <= auth_prefix.size())
|
||||
{
|
||||
|
||||
@@ -68,7 +68,7 @@ public:
|
||||
}
|
||||
catch (const system::system_error& err)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(info) << "POST /api/v1/User/Medications - Response 400: Cannot deserialize json";
|
||||
BOOST_LOG_TRIVIAL(error) << "POST /api/v1/User/Medications - Response 400: Cannot deserialize json";
|
||||
throw session_exception(http::status::bad_request, "Cannot deserialize json");
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
#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/IUserTreatmentSchemesDAO.h"
|
||||
#include "../exceptions/session_exception.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
template <class Body, class Allocator, class ResponseType>
|
||||
class PostUserTreatmentSchemeExecutor : public IExecutor<Body, Allocator, ResponseType>
|
||||
{
|
||||
mysqlx::Session& session_;
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
||||
const std::shared_ptr<IUserTreatmentSchemeDAO>& user_treatment_scheme_dao_;
|
||||
|
||||
public:
|
||||
PostUserTreatmentSchemeExecutor(
|
||||
mysqlx::Session& session,
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao,
|
||||
const std::shared_ptr<IUserTreatmentSchemeDAO>& user_treatment_scheme_dao
|
||||
) : session_(session), auth_dao_(auth_dao), user_treatment_scheme_dao_(user_treatment_scheme_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 =
|
||||
"POST /api/v1/UserTreatmentSchemes - Response 401: Unauthorized"s;
|
||||
|
||||
BOOST_LOG_TRIVIAL(info) << "POST /api/v1/UserTreatmentSchemes - 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_uuid_ref = auth_dao_->GetUUID(auth_token);
|
||||
const std::string user_uuid{user_uuid_ref.begin(), user_uuid_ref.end()};
|
||||
const auto body = req.body();
|
||||
value req_json;
|
||||
|
||||
try
|
||||
{
|
||||
req_json = json::parse(body);
|
||||
}
|
||||
catch (const system::system_error& err)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(error) <<
|
||||
"POST /api/v1/UserTreatmentSchemes - Response 400: Cannot deserialize json";
|
||||
throw session_exception(http::status::bad_request, "Cannot deserialize json");
|
||||
}
|
||||
|
||||
user_treatment_scheme_dto utsd = FromJSON(req_json);
|
||||
utsd.uuid = GenerateUUID();
|
||||
|
||||
try
|
||||
{
|
||||
user_treatment_scheme_dao_->CreateUserTreatmentScheme(user_uuid, utsd);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
BOOST_LOG_TRIVIAL(error) <<
|
||||
"POST /api/v1/UserTreatmentSchemes - Response 400: Cannot write entity";
|
||||
throw session_exception(http::status::bad_request, "Cannot write entity");
|
||||
}
|
||||
|
||||
http::response<ResponseType> res{http::status::ok, req.version()};
|
||||
json::value res_json = ToJSON(utsd);
|
||||
|
||||
res.body() = serialize(res_json);
|
||||
res.set(http::field::content_type, "application/json");
|
||||
res.content_length(res.body().size());
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
private:
|
||||
user_treatment_scheme_dto FromJSON(const boost::json::value& scheme)
|
||||
{
|
||||
user_treatment_scheme_dto dto;
|
||||
|
||||
dto.treatment_name =
|
||||
scheme.at("treatment_name").as_string().c_str();
|
||||
|
||||
dto.instructions =
|
||||
scheme.at("instructions").as_string().c_str();
|
||||
|
||||
for (const auto& med : scheme.at("medications").as_array())
|
||||
{
|
||||
dto.medication_uuids.push_back(
|
||||
med.as_string().c_str()
|
||||
);
|
||||
}
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
boost::json::object ToJSON(const user_treatment_scheme_dto& utsd)
|
||||
{
|
||||
boost::json::array medications_json;
|
||||
medications_json.reserve(utsd.medication_uuids.size());
|
||||
|
||||
for (const auto& medication_uuid : utsd.medication_uuids) {
|
||||
medications_json.emplace_back(medication_uuid);
|
||||
}
|
||||
|
||||
boost::json::object scheme_json;
|
||||
scheme_json["uuid"] = utsd.uuid;
|
||||
scheme_json["treatment_name"] = utsd.treatment_name;
|
||||
scheme_json["instructions"] = utsd.instructions;
|
||||
scheme_json["medications"] = std::move(medications_json);
|
||||
|
||||
return scheme_json;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "AuthLogoutExecutor.h"
|
||||
#include "GetUserMedicationsExecutor.h"
|
||||
#include "GetUserTreatmentSchemeExecutor.h"
|
||||
#include "PostUserTreatmentSchemeExecutor.h"
|
||||
#include "PostUserMedicationsExecutor.h"
|
||||
#include "GetDiariesExecutor.h"
|
||||
#include "../DAO/IUserDAO.h"
|
||||
@@ -37,6 +38,8 @@ class RootExecutor
|
||||
Body, Allocator, boost::beast::http::string_body>;
|
||||
using RouteGetUserTreatmentSchemeExecutor = GetUserTreatmentSchemeExecutor<
|
||||
Body, Allocator, boost::beast::http::string_body>;
|
||||
using RoutePostUserTreatmentSchemeExecutor = PostUserTreatmentSchemeExecutor<
|
||||
Body, Allocator, boost::beast::http::string_body>;
|
||||
using RouteGetDiariesExecutor = GetDiariesExecutor<
|
||||
Body, Allocator, boost::beast::http::string_body>;
|
||||
using IRouteController = IController<Body, Allocator, boost::beast::http::string_body>;
|
||||
@@ -121,6 +124,11 @@ public:
|
||||
std::make_shared<RouteGetUserTreatmentSchemeExecutor>(session_, auth_dao_,
|
||||
user_treatment_scheme_dao_)
|
||||
},
|
||||
{
|
||||
boost::beast::http::verb::post,
|
||||
std::make_shared<RoutePostUserTreatmentSchemeExecutor>(session_, auth_dao_,
|
||||
user_treatment_scheme_dao_)
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user