Files
UpAndDown/src/endpoints_handlers/GetUserTreatmentSchemeExecutor.h
T

121 lines
3.7 KiB
C++

#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 GetUserTreatmentSchemeExecutor : 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:
GetUserTreatmentSchemeExecutor(
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 =
"GET /api/v1/UserTreatmentSchemes - Response 401: Unauthorized"s;
BOOST_LOG_TRIVIAL(info) << "GET /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()};
std::vector<user_treatment_scheme_dto> schemes = user_treatment_scheme_dao_->FindByUserUUID(user_uuid);
http::response<ResponseType> res{http::status::ok, req.version()};
value response_body;
response_body.emplace_object();
response_body.as_object().emplace("user_treatment_schemes", toJSONArray(schemes));
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 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<user_treatment_scheme_dto>& 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);
}
};
}