Рабочая сборка с третьей ручкой

This commit is contained in:
Антон
2025-09-27 13:34:30 +03:00
parent d4c01cd70c
commit d8cbdaf635
11 changed files with 58 additions and 118 deletions
+1 -2
View File
@@ -38,8 +38,7 @@ add_executable(App ./src/main.cpp
./src/db/mysql_connector.cpp ./src/db/mysql_connector.cpp
./src/db/mysql_connector.h ./src/db/mysql_connector.h
./src/DAO/IUserDAO.h ./src/DAO/IUserDAO.h
./src/entities/User.cpp ./src/entities/user.h
./src/entities/User.h
./src/DAO/MySQLUserDAO.cpp ./src/DAO/MySQLUserDAO.cpp
./src/DAO/MySQLUserDAO.h ./src/DAO/MySQLUserDAO.h
./src/endpoints_handlers/IExecutor.h ./src/endpoints_handlers/IExecutor.h
+1 -1
View File
@@ -3,7 +3,7 @@
#include <cstdint> #include <cstdint>
#include "../entities/User.h" #include "../entities/user.h"
namespace uad namespace uad
{ {
+6 -6
View File
@@ -4,22 +4,22 @@
#include <vector> #include <vector>
#include <optional> #include <optional>
#include "../entities/User.h" #include "../entities/user.h"
namespace uad namespace uad
{ {
class IUserDAO class IUserDAO
{ {
public: public:
virtual std::string Create(const User& created_user) = 0; virtual std::string Create(const user& created_user) = 0;
virtual std::optional<User> GetByUUID(const std::string& uuid) = 0; virtual std::optional<user> GetByUUID(const std::string& uuid) = 0;
virtual std::optional<User> GetByLogin(const std::string& login) = 0; virtual std::optional<user> GetByLogin(const std::string& login) = 0;
virtual std::pair<bool, std::vector<User>> GetAll(size_t limit, size_t offset) = 0; virtual std::pair<bool, std::vector<user>> GetAll(size_t limit, size_t offset) = 0;
virtual bool Update(const User& u) = 0; virtual bool Update(const user& u) = 0;
virtual bool Delete(const std::string& uuid) = 0; virtual bool Delete(const std::string& uuid) = 0;
+21 -21
View File
@@ -13,7 +13,7 @@ MySQLUserDAO::MySQLUserDAO(mysqlx::Session& session) : session_(session)
{ {
} }
string MySQLUserDAO::Create(const User& created_user) string MySQLUserDAO::Create(const user& created_user)
{ {
boost::uuids::random_generator generator; boost::uuids::random_generator generator;
boost::uuids::uuid uuid = generator(); boost::uuids::uuid uuid = generator();
@@ -23,13 +23,13 @@ string MySQLUserDAO::Create(const User& created_user)
"INSERT INTO `up_and_down`.`users` (`uuid`, `login`, `hashed_password`) VALUES (?, ?, ?);"s; "INSERT INTO `up_and_down`.`users` (`uuid`, `login`, `hashed_password`) VALUES (?, ?, ?);"s;
session_.sql(sql_script) session_.sql(sql_script)
.bind(uuid_str, created_user.GetLogin(), created_user.GetHashedPassword()) .bind(uuid_str, created_user.login, created_user.hashed_password)
.execute(); .execute();
return uuid_str; return uuid_str;
} }
optional<User> MySQLUserDAO::GetByUUID(const string& uuid) optional<user> MySQLUserDAO::GetByUUID(const string& uuid)
{ {
mysqlx::SqlResult sql_result = session_. mysqlx::SqlResult sql_result = session_.
sql("SELECT * FROM `up_and_down`.`users` WHERE (uuid = '" + uuid + sql("SELECT * FROM `up_and_down`.`users` WHERE (uuid = '" + uuid +
@@ -38,7 +38,7 @@ optional<User> MySQLUserDAO::GetByUUID(const string& uuid)
return GetSingleUserBySQLResult(std::move(sql_result)); return GetSingleUserBySQLResult(std::move(sql_result));
} }
optional<User> MySQLUserDAO::GetByLogin(const string& login) optional<user> MySQLUserDAO::GetByLogin(const string& login)
{ {
mysqlx::SqlResult sql_result = session_. mysqlx::SqlResult sql_result = session_.
sql("SELECT * FROM `up_and_down`.`users` WHERE (login = '" + login sql("SELECT * FROM `up_and_down`.`users` WHERE (login = '" + login
@@ -47,25 +47,25 @@ optional<User> MySQLUserDAO::GetByLogin(const string& login)
return GetSingleUserBySQLResult(std::move(sql_result)); return GetSingleUserBySQLResult(std::move(sql_result));
} }
pair<bool, vector<User>> MySQLUserDAO::GetAll(size_t limit, size_t offset) pair<bool, vector<user>> MySQLUserDAO::GetAll(size_t limit, size_t offset)
{ {
mysqlx::SqlResult sql_result = session_ mysqlx::SqlResult sql_result = session_
.sql("SELECT * FROM `up_and_down`.`users` LIMIT ? OFFSET ?;"s) .sql("SELECT * FROM `up_and_down`.`users` LIMIT ? OFFSET ?;"s)
.bind(limit, offset) .bind(limit, offset)
.execute(); .execute();
list<mysqlx::Row> rows = sql_result.fetchAll(); list<mysqlx::Row> rows = sql_result.fetchAll();
pair<bool, vector<User>> ret; pair<bool, vector<user>> ret;
if (!rows.size()) if (!rows.size())
{ {
ret.first = true; ret.first = true;
ret.second = vector<User>{}; ret.second = vector<user>{};
return ret; return ret;
} }
ret.first = rows.size() != limit + 1; ret.first = rows.size() != limit + 1;
ret.second = vector<User>{}; ret.second = vector<user>{};
ret.second.reserve(limit); ret.second.reserve(limit);
@@ -76,13 +76,13 @@ pair<bool, vector<User>> MySQLUserDAO::GetAll(size_t limit, size_t offset)
break; break;
} }
User user; user user;
string user_uuid = row[0].get<string>(); string user_uuid = row[0].get<string>();
string user_login = row[1].get<string>(); string user_login = row[1].get<string>();
user.SetLogin(user_login); user.login = user_login;
user.SetUUID(user_uuid); user.uuid = user_uuid;
ret.second.push_back(std::move(user)); ret.second.push_back(std::move(user));
--limit; --limit;
@@ -91,15 +91,15 @@ pair<bool, vector<User>> MySQLUserDAO::GetAll(size_t limit, size_t offset)
return ret; return ret;
} }
bool MySQLUserDAO::Update(const User& u) bool MySQLUserDAO::Update(const user& u)
{ {
auto schema = session_.getSchema("up_and_down"); auto schema = session_.getSchema("up_and_down");
auto table = schema.getTable("users"); auto table = schema.getTable("users");
mysqlx::Result res = table.update() mysqlx::Result res = table.update()
.set("login", u.GetLogin()) .set("login", u.login)
.set("hashed_password", u.GetHashedPassword()) .set("hashed_password", u.hashed_password)
.where("uuid = :uuid") .where("uuid = :uuid")
.bind("uuid", u.GetUUID()) .bind("uuid", u.uuid)
.execute(); .execute();
return !!res.getAffectedItemsCount(); return !!res.getAffectedItemsCount();
@@ -117,7 +117,7 @@ bool MySQLUserDAO::Delete(const string& uuid)
return !!res.getAffectedItemsCount(); return !!res.getAffectedItemsCount();
} }
std::optional<User> MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result) std::optional<user> MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result)
{ {
list<mysqlx::Row> rows = sql_result.fetchAll(); list<mysqlx::Row> rows = sql_result.fetchAll();
@@ -132,12 +132,12 @@ std::optional<User> MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& s
string user_login = row_data[1].get<string>(); string user_login = row_data[1].get<string>();
string user_hashed_password = row_data[2].get<string>(); string user_hashed_password = row_data[2].get<string>();
User user; user single_user;
user.SetUUID(user_uuid); single_user.uuid = user_uuid;
user.SetLogin(user_login); single_user.login = user_login;
user.SetHashedPassword(user_hashed_password); single_user.hashed_password = user_hashed_password;
return optional<User>(std::move(user)); return optional<user>(std::move(single_user));
} }
} // uad } // uad
+6 -6
View File
@@ -10,19 +10,19 @@ class MySQLUserDAO : public IUserDAO
public: public:
explicit MySQLUserDAO(mysqlx::Session& session); explicit MySQLUserDAO(mysqlx::Session& session);
std::string Create(const User& created_user) override; std::string Create(const user& created_user) override;
std::optional<User> GetByUUID(const std::string& uuid) override; std::optional<user> GetByUUID(const std::string& uuid) override;
std::optional<User> GetByLogin(const std::string& login) override; std::optional<user> GetByLogin(const std::string& login) override;
std::pair<bool, std::vector<User>> GetAll(size_t limit, size_t offset) override; std::pair<bool, std::vector<user>> GetAll(size_t limit, size_t offset) override;
bool Update(const User& u) override; bool Update(const user& u) override;
bool Delete(const std::string& uuid) override; bool Delete(const std::string& uuid) override;
private: private:
std::optional<User> GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result); std::optional<user> GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result);
}; };
} }
+3 -3
View File
@@ -62,9 +62,9 @@ public:
return res; return res;
} }
std::optional<User> maybe_user = user_dao_->GetByLogin(login); std::optional<user> maybe_user = user_dao_->GetByLogin(login);
if (!maybe_user.has_value() || (maybe_user.value().GetHashedPassword() != HashPassword(password))) if (!maybe_user.has_value() || maybe_user.value().hashed_password != HashPassword(password))
{ {
http::response<ResponseType> res{http::status::unprocessable_entity, req.version()}; http::response<ResponseType> res{http::status::unprocessable_entity, req.version()};
response_body.as_object().emplace("Result", "Incorrect login or password"); response_body.as_object().emplace("Result", "Incorrect login or password");
@@ -76,7 +76,7 @@ public:
return res; return res;
} }
auto token = GenerateUUID(); auto token = GenerateUUID();
auth_dao_->Login(maybe_user.value().GetUUID(), token); auth_dao_->Login(maybe_user.value().uuid, token);
http::response<ResponseType> res{http::status::ok, req.version()}; http::response<ResponseType> res{http::status::ok, req.version()};
response_body.as_object().emplace("token", token); response_body.as_object().emplace("token", token);
@@ -90,10 +90,10 @@ public:
return res; return res;
} }
User user; user user;
user.SetLogin(login); user.login = login;
user.SetPassword(password); user.hashed_password = HashPassword(password);
const auto uuid_stringified = user_dao_->Create(user); const auto uuid_stringified = user_dao_->Create(user);
@@ -107,7 +107,7 @@ public:
); );
response_body.as_object().emplace( response_body.as_object().emplace(
"login", "login",
user.GetLogin() user.login
); );
res.body() = serialize(response_body); res.body() = serialize(response_body);
-45
View File
@@ -1,45 +0,0 @@
#include <iostream>
#include <string>
#include "User.h"
using namespace std;
namespace uad
{
const string& User::GetUUID() const noexcept
{
return uuid_;
}
void User::SetUUID(const string& new_uuid)
{
uuid_ = new_uuid;
}
const string& User::GetLogin() const noexcept
{
return login_;
}
void User::SetLogin(const string& login)
{
login_ = login;
}
void User::SetPassword(const string& password)
{
size_t calculated_hash = hash<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_;
}
}
-29
View File
@@ -1,29 +0,0 @@
#pragma once
#include <string>
#include "../helpers/helpers.h"
namespace uad
{
class User
{
std::string uuid_;
std::string login_;
std::string hashed_password_;
public:
[[nodiscard]] const std::string& GetUUID() const noexcept;
void SetUUID(const std::string& new_uuid);
[[nodiscard]] const std::string& GetLogin() const noexcept;
void SetLogin(const std::string& login);
void SetPassword(const std::string& password);
void SetHashedPassword(const std::string& hashed_password);
[[nodiscard]] const std::string& GetHashedPassword() const noexcept;
};
}
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include <string>
#include "../helpers/helpers.h"
namespace uad
{
struct user
{
std::string uuid;
std::string login;
std::string hashed_password;
};
}
+1 -1
View File
@@ -18,7 +18,7 @@
#include "./session/WebsocketSession.h" #include "./session/WebsocketSession.h"
#include "./listener/Listener.h" #include "./listener/Listener.h"
#include "./db/mysql_connector.h" #include "./db/mysql_connector.h"
#include "entities/User.h" #include "entities/user.h"
namespace beast = boost::beast; namespace beast = boost::beast;
namespace http = beast::http; namespace http = beast::http;