Registration переведен на систему исключений вместо возврата

This commit is contained in:
Антон
2025-09-29 18:13:29 +03:00
parent 0ef4c7e46c
commit 0042b7e6bc
2 changed files with 36 additions and 26 deletions
@@ -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<ResponseType> 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<ResponseType> 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<ResponseType> res{
http::status::created, req.version()
};
value response_body;
response_body.emplace_object();
response_body.as_object().emplace(
"uuid",
+28
View File
@@ -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<ResponseType> 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<ResponseType> 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));
}
}