diff --git a/src/endpoints_handlers/AuthRegistrationExecutor.h b/src/endpoints_handlers/AuthRegistrationExecutor.h index 440093b..436441d 100644 --- a/src/endpoints_handlers/AuthRegistrationExecutor.h +++ b/src/endpoints_handlers/AuthRegistrationExecutor.h @@ -9,6 +9,8 @@ #include "IExecutor.h" #include "../DAO/IUserDAO.h" #include "../exceptions/exception400_bad_request.h" +#include "../exceptions/exception409_conflict.h" +#include "../exceptions/exception422_unprocessable_entity.h" namespace uad { @@ -36,7 +38,6 @@ public: const auto& body = req.body(); value req_json; - value response_body; try { @@ -47,41 +48,19 @@ public: throw exception400_bad_request("cannot deserialize json"); } - response_body.emplace_object(); - const std::string login = req_json.as_object().at("login").as_string().c_str(); const std::string password = req_json.as_object().at("password").as_string().c_str(); if (!ValidateLogin(login) || !ValidatePassword(password)) { - http::response res{http::status::unprocessable_entity, req.version()}; - - response_body.as_object().emplace( - "Result", - "Validations failed. Login should have length from 3 to 50. Password from 5 characters length." + throw exception422_unprocessable_entity( + "Validations failed. Login should have length from 3 to 50. Password from 5 characters length."s ); - - res.body() = serialize(response_body); - res.set(http::field::content_type, "application/json"); - res.content_length(res.body().size()); - - return res; } if (user_dao_->GetByLogin(login).has_value()) { - http::response res{http::status::conflict, req.version()}; - - response_body.as_object().emplace( - "Result", - "user with login "s + login + " exists"s - ); - - res.body() = serialize(response_body); - res.set(http::field::content_type, "application/json"); - res.content_length(res.body().size()); - - return res; + throw exception409_conflict("user with login "s + login + " exists"s); } user user; @@ -94,6 +73,9 @@ public: http::response res{ http::status::created, req.version() }; + value response_body; + + response_body.emplace_object(); response_body.as_object().emplace( "uuid", diff --git a/src/endpoints_handlers/RootExecutor.h b/src/endpoints_handlers/RootExecutor.h index 480f679..53e1ca8 100644 --- a/src/endpoints_handlers/RootExecutor.h +++ b/src/endpoints_handlers/RootExecutor.h @@ -108,6 +108,34 @@ public: res.set(boost::beast::http::field::content_type, "application/json"); res.content_length(res.body().size()); + return send(std::move(res)); + } + catch (const exception409_conflict& e) + { + boost::beast::http::response res{boost::beast::http::status::conflict, req.version()}; + boost::json::value response_body; + + response_body.emplace_object(); + response_body.as_object().emplace("Result", e.what()); + + res.body() = serialize(response_body); + res.set(boost::beast::http::field::content_type, "application/json"); + res.content_length(res.body().size()); + + return send(std::move(res)); + } + catch (const exception422_unprocessable_entity& e) + { + boost::beast::http::response res{boost::beast::http::status::unprocessable_entity, req.version()}; + boost::json::value response_body; + + response_body.emplace_object(); + response_body.as_object().emplace("Result", e.what()); + + res.body() = serialize(response_body); + res.set(boost::beast::http::field::content_type, "application/json"); + res.content_length(res.body().size()); + return send(std::move(res)); } }