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; }