From 0042b7e6bc5a07ec976777e3e70e6b87857a48d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Mon, 29 Sep 2025 18:13:29 +0300 Subject: [PATCH] =?UTF-8?q?Registration=20=D0=BF=D0=B5=D1=80=D0=B5=D0=B2?= =?UTF-8?q?=D0=B5=D0=B4=D0=B5=D0=BD=20=D0=BD=D0=B0=20=D1=81=D0=B8=D1=81?= =?UTF-8?q?=D1=82=D0=B5=D0=BC=D1=83=20=D0=B8=D1=81=D0=BA=D0=BB=D1=8E=D1=87?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B9=20=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE?= =?UTF-8?q?=20=D0=B2=D0=BE=D0=B7=D0=B2=D1=80=D0=B0=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../AuthRegistrationExecutor.h | 34 +++++-------------- src/endpoints_handlers/RootExecutor.h | 28 +++++++++++++++ 2 files changed, 36 insertions(+), 26 deletions(-) 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)); } }