База для ручки PostUserMedicationsExecutor.h

This commit is contained in:
2026-01-08 16:04:57 +03:00
parent 50bad13f86
commit a3e69b65fb
@@ -58,8 +58,41 @@ public:
throw session_exception(http::status::unauthorized, "Unauthorized"); throw session_exception(http::status::unauthorized, "Unauthorized");
} }
const auto body = req.body();
value req_json;
medication m;
try
{
req_json = json::parse(body);
}
catch (const system::system_error& err)
{
BOOST_LOG_TRIVIAL(info) << "POST /api/v1/User/Medications - Response 400: Cannot deserialize json";
throw session_exception(http::status::bad_request, "Cannot deserialize json");
}
m.name = req_json.as_object().at("name").as_string().c_str();
m.dose = req_json.as_object().at("dose").as_int64();
m.unit = req_json.as_object().at("unit").as_string().c_str();
m.is_urgent = req_json.as_object().at("is_urgent").as_bool();
try
{
m.uuid = medications_dao_->Create(m);
}
catch (const std::runtime_error& err)
{
BOOST_LOG_TRIVIAL(error) << "POST /api/v1/User/Medications - Response 409: This medication already exists";
throw session_exception(http::status::conflict, "This medication already exists");
}
http::response<ResponseType> res{http::status::ok, req.version()}; http::response<ResponseType> res{http::status::ok, req.version()};
res.body() = serialize(ToJSON(m));
res.set(http::field::content_type, "application/json");
res.content_length(res.body().size());
return res; return res;
} }