Разработка ручки для GetAllMedications

This commit is contained in:
2026-01-07 16:17:03 +03:00
parent f06d22febf
commit d929cbae02
6 changed files with 49 additions and 1798 deletions
@@ -3,6 +3,7 @@
#include <regex>
#include <boost/json.hpp>
#include <boost/mpl/vector/vector0.hpp>
#include <mysqlx/xdevapi.h>
#include "IExecutor.h"
@@ -39,25 +40,66 @@ public:
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].begin() == req[http::field::authorization].end())
if (req[http::field::authorization].empty())
{
BOOST_LOG_TRIVIAL(info) << "GET /api/v1/User/Medications - Response 401: Unauthorized";
BOOST_LOG_TRIVIAL(info) << invalid_token_message;
throw session_exception(http::status::unauthorized, "Unauthorized");
}
const std::string auth_string = {*req[http::field::authorization].begin()};
const std::string auth_token = {auth_string.begin() + auth_prefix.size(), auth_string.end()};
if (auth_dao_->HasAuthorized(auth_token)) {}
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;
}
};
}