Почти завершенная регистрация

This commit is contained in:
Антон
2025-08-30 07:40:53 +03:00
parent 07a9bbf9ff
commit eea5e42573
6 changed files with 84 additions and 23 deletions
+30 -14
View File
@@ -18,24 +18,16 @@ string MySQLUserDAO::Create(const User& created_user)
optional<User> MySQLUserDAO::GetByGUID(string guid)
{
return nullopt;
mysqlx::SqlResult sql_result = session_.sql("SELECT * FROM `up_and_down`.`users` WHERE (guid = '" + guid + "') LIMIT 1;"s).execute();
return GetSingleUserBySQLResult(std::move(sql_result));
}
std::optional<User> MySQLUserDAO::GetByLogin(std::string login)
optional<User> MySQLUserDAO::GetByLogin(string login)
{
auto sql_result = session_.sql("SELECT * FROM `up_and_down`.`users` WHERE (login = '" + login + "') LIMIT 1;"s).execute();
list<mysqlx::Row> rows = sql_result.fetchAll();
mysqlx::SqlResult sql_result = session_.sql("SELECT * FROM `up_and_down`.`users` WHERE (login = '" + login + "') LIMIT 1;"s).execute();
if (rows.size())
{
auto row_data = *rows.begin();
string replicated_login = row_data[1].get<string>();
cout << "SUCCESS!" << endl;
}
return nullopt;
return GetSingleUserBySQLResult(std::move(sql_result));
}
vector<User> MySQLUserDAO::GetAll()
@@ -54,4 +46,28 @@ bool MySQLUserDAO::Delete(string id)
{
return false;
}
std::optional<User> MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result)
{
list<mysqlx::Row> rows = sql_result.fetchAll();
if (!rows.size())
{
return nullopt;
}
auto row_data = *rows.begin();
string user_guid = row_data[0].get<string>();
string user_login = row_data[1].get<string>();
string user_hashed_password = row_data[2].get<string>();
User user;
user.SetGUID(user_guid);
user.SetLogin(user_login);
user.SetHashedPassword(user_hashed_password);
return optional<User>(std::move(user));
}
} // uad
+9 -6
View File
@@ -10,16 +10,19 @@ class MySQLUserDAO : public IUserDAO
public:
explicit MySQLUserDAO(mysqlx::Session& session);
std::string Create(const User& created_user);
std::string Create(const User& created_user) override;
std::optional<User> GetByGUID(std::string guid);
std::optional<User> GetByGUID(std::string guid) override;
std::optional<User> GetByLogin(std::string login);
std::optional<User> GetByLogin(std::string login) override;
std::vector<User> GetAll();
std::vector<User> GetAll() override;
bool Update(const User& u);
bool Update(const User& u) override;
bool Delete(std::string id);
bool Delete(std::string id) override;
private:
std::optional<User> GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result);
};
}
@@ -1,5 +1,6 @@
#pragma once
#include <regex>
#include <boost/json.hpp>
#include <mysqlx/xdevapi.h>
#include <mysqlx/common/api.h>
@@ -41,7 +42,7 @@ public:
{
http::response<ResponseType> res{http::status::bad_request, req.version()};
res.body() = "{ \"detail\": \"cannot deserialize json\"}";
res.body() = "{ \"Result\": \"cannot deserialize json\"}";
res.set(http::field::content_type, "application/json");
res.content_length(res.body().size());
@@ -51,8 +52,26 @@ public:
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()};
res.body() = "{ \"Result\": \"validations failed\"}";
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()};
res.body() = "{ \"Result\": \"user with login " + login + " exists\" }";
res.set(http::field::content_type, "application/json");
res.content_length(res.body().size());
return res;
}
User user;
@@ -61,14 +80,29 @@ public:
user.SetPassword(password);
http::response<ResponseType> res{
http::status::ok, req.version()
http::status::created, req.version()
};
res.body() = "{ \"detail\": \"ok\"}";
res.body() = "{ \"Result\": \"ok\"}";
res.set(http::field::content_type, "application/json");
res.content_length(res.body().size());
return res;
}
private:
bool ValidateLogin(const std::string& login)
{
if (login.size() < 3 || login.size() > 50) return false;
std::regex pattern("[az09._-]");
return std::regex_match(login, pattern);
}
bool ValidatePassword(const std::string& password)
{
return password.size() >= 5;
}
};
}
+5
View File
@@ -33,6 +33,11 @@ void User::SetPassword(const string& password)
hashed_password_ = ToHex((byte*)&calculated_hash, sizeof(calculated_hash));
}
void User::SetHashedPassword(const std::string& hashed_password)
{
hashed_password_ = hashed_password;
}
const string& User::GetHashedPassword() const noexcept
{
return hashed_password_;
+2
View File
@@ -22,6 +22,8 @@ public:
void SetPassword(const std::string& password);
void SetHashedPassword(const std::string& hashed_password);
[[nodiscard]] const std::string& GetHashedPassword() const noexcept;
};
}