From a3e69b65fb8312b1826f2bc4c2409872ca958168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Thu, 8 Jan 2026 16:04:57 +0300 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=B0=D0=B7=D0=B0=20=D0=B4=D0=BB=D1=8F?= =?UTF-8?q?=20=D1=80=D1=83=D1=87=D0=BA=D0=B8=20PostUserMedicationsExecutor?= =?UTF-8?q?.h?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PostUserMedicationsExecutor.h | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/endpoints_handlers/PostUserMedicationsExecutor.h b/src/endpoints_handlers/PostUserMedicationsExecutor.h index 3918fc7..7bacb17 100644 --- a/src/endpoints_handlers/PostUserMedicationsExecutor.h +++ b/src/endpoints_handlers/PostUserMedicationsExecutor.h @@ -58,8 +58,41 @@ public: 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 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; }