Рабочая сборка второго executor'а

This commit is contained in:
Антон
2025-09-20 09:28:46 +03:00
parent bd5b7dd6ac
commit fb0cbd0161
4 changed files with 27 additions and 62 deletions
+19 -61
View File
@@ -45,6 +45,25 @@ public:
try
{
req_json = json::parse(body);
std::string login = req_json.as_object().at("login").as_string().c_str();
std::string password = req_json.as_object().at("password").as_string().c_str();
if (login.empty() || password.empty())
{
http::response<ResponseType> res{http::status::unprocessable_entity, req.version()};
response_body.as_object().emplace("Result", "Login or password are empty");
res.body() = serialize(response_body);
res.set(http::field::content_type, "application/json");
res.content_length(res.body().size());
return res;
}
std::string hashed_password = HashPassword(password);
std::optional<User> maybe_user = user_dao_->GetByLogin(login);
}
catch (const system::system_error& err)
{
@@ -57,67 +76,6 @@ public:
return res;
}
std::string login = req_json.as_object().at("login").as_string().c_str();
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."
);
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;
}
User user;
user.SetLogin(login);
user.SetPassword(password);
const auto uuid_stringified = user_dao_->Create(user);
http::response<ResponseType> res{
http::status::created, req.version()
};
response_body.as_object().emplace(
"uuid",
uuid_stringified
);
response_body.as_object().emplace(
"login",
user.GetLogin()
);
res.body() = serialize(response_body);
res.set(http::field::content_type, "application/json");
res.content_length(res.body().size());
return res;
}
private:
+6
View File
@@ -116,6 +116,12 @@ std::string ToHex(std::byte* src, size_t len)
return ret;
}
std::string HashPassword(const std::string& password)
{
size_t calculated_hash = std::hash<string>{}(password);
return ToHex((byte*)&calculated_hash, sizeof(calculated_hash));
}
uint64_t Random()
{
std::random_device device;
+2
View File
@@ -12,5 +12,7 @@ void Fail(boost::beast::error_code ec, char const* what);
std::string ToHex(std::byte* src, size_t len);
std::string HashPassword(const std::string& password);
uint64_t Random();
} // namespace uad