generated from Sithas/conan_template
Registration переведен на систему исключений вместо возврата
This commit is contained in:
@@ -9,6 +9,8 @@
|
|||||||
#include "IExecutor.h"
|
#include "IExecutor.h"
|
||||||
#include "../DAO/IUserDAO.h"
|
#include "../DAO/IUserDAO.h"
|
||||||
#include "../exceptions/exception400_bad_request.h"
|
#include "../exceptions/exception400_bad_request.h"
|
||||||
|
#include "../exceptions/exception409_conflict.h"
|
||||||
|
#include "../exceptions/exception422_unprocessable_entity.h"
|
||||||
|
|
||||||
namespace uad
|
namespace uad
|
||||||
{
|
{
|
||||||
@@ -36,7 +38,6 @@ public:
|
|||||||
|
|
||||||
const auto& body = req.body();
|
const auto& body = req.body();
|
||||||
value req_json;
|
value req_json;
|
||||||
value response_body;
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@@ -47,41 +48,19 @@ public:
|
|||||||
throw exception400_bad_request("cannot deserialize json");
|
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 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();
|
const std::string password = req_json.as_object().at("password").as_string().c_str();
|
||||||
|
|
||||||
if (!ValidateLogin(login) || !ValidatePassword(password))
|
if (!ValidateLogin(login) || !ValidatePassword(password))
|
||||||
{
|
{
|
||||||
http::response<ResponseType> res{http::status::unprocessable_entity, req.version()};
|
throw exception422_unprocessable_entity(
|
||||||
|
"Validations failed. Login should have length from 3 to 50. Password from 5 characters length."s
|
||||||
response_body.as_object().emplace(
|
|
||||||
"Result",
|
|
||||||
"Validations failed. Login should have length from 3 to 50. Password from 5 characters length."
|
|
||||||
);
|
);
|
||||||
|
|
||||||
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())
|
if (user_dao_->GetByLogin(login).has_value())
|
||||||
{
|
{
|
||||||
http::response<ResponseType> res{http::status::conflict, req.version()};
|
throw exception409_conflict("user with login "s + login + " exists"s);
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
user user;
|
user user;
|
||||||
@@ -94,6 +73,9 @@ public:
|
|||||||
http::response<ResponseType> res{
|
http::response<ResponseType> res{
|
||||||
http::status::created, req.version()
|
http::status::created, req.version()
|
||||||
};
|
};
|
||||||
|
value response_body;
|
||||||
|
|
||||||
|
response_body.emplace_object();
|
||||||
|
|
||||||
response_body.as_object().emplace(
|
response_body.as_object().emplace(
|
||||||
"uuid",
|
"uuid",
|
||||||
|
|||||||
@@ -108,6 +108,34 @@ public:
|
|||||||
res.set(boost::beast::http::field::content_type, "application/json");
|
res.set(boost::beast::http::field::content_type, "application/json");
|
||||||
res.content_length(res.body().size());
|
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));
|
return send(std::move(res));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user