generated from Sithas/conan_template
Рабочая сборка с третьей ручкой
This commit is contained in:
+1
-1
@@ -3,7 +3,7 @@
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
#include "../entities/User.h"
|
||||
#include "../entities/user.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
|
||||
+6
-6
@@ -4,22 +4,22 @@
|
||||
#include <vector>
|
||||
#include <optional>
|
||||
|
||||
#include "../entities/User.h"
|
||||
#include "../entities/user.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
class IUserDAO
|
||||
{
|
||||
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;
|
||||
|
||||
|
||||
+21
-21
@@ -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::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;
|
||||
|
||||
session_.sql(sql_script)
|
||||
.bind(uuid_str, created_user.GetLogin(), created_user.GetHashedPassword())
|
||||
.bind(uuid_str, created_user.login, created_user.hashed_password)
|
||||
.execute();
|
||||
|
||||
return uuid_str;
|
||||
}
|
||||
|
||||
optional<User> MySQLUserDAO::GetByUUID(const string& uuid)
|
||||
optional<user> MySQLUserDAO::GetByUUID(const string& uuid)
|
||||
{
|
||||
mysqlx::SqlResult sql_result = session_.
|
||||
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));
|
||||
}
|
||||
|
||||
optional<User> MySQLUserDAO::GetByLogin(const string& login)
|
||||
optional<user> MySQLUserDAO::GetByLogin(const string& login)
|
||||
{
|
||||
mysqlx::SqlResult sql_result = session_.
|
||||
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));
|
||||
}
|
||||
|
||||
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_
|
||||
.sql("SELECT * FROM `up_and_down`.`users` LIMIT ? OFFSET ?;"s)
|
||||
.bind(limit, offset)
|
||||
.execute();
|
||||
list<mysqlx::Row> rows = sql_result.fetchAll();
|
||||
pair<bool, vector<User>> ret;
|
||||
pair<bool, vector<user>> ret;
|
||||
|
||||
if (!rows.size())
|
||||
{
|
||||
ret.first = true;
|
||||
ret.second = vector<User>{};
|
||||
ret.second = vector<user>{};
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret.first = rows.size() != limit + 1;
|
||||
ret.second = vector<User>{};
|
||||
ret.second = vector<user>{};
|
||||
|
||||
ret.second.reserve(limit);
|
||||
|
||||
@@ -76,13 +76,13 @@ pair<bool, vector<User>> MySQLUserDAO::GetAll(size_t limit, size_t offset)
|
||||
break;
|
||||
}
|
||||
|
||||
User user;
|
||||
user user;
|
||||
|
||||
string user_uuid = row[0].get<string>();
|
||||
string user_login = row[1].get<string>();
|
||||
|
||||
user.SetLogin(user_login);
|
||||
user.SetUUID(user_uuid);
|
||||
user.login = user_login;
|
||||
user.uuid = user_uuid;
|
||||
|
||||
ret.second.push_back(std::move(user));
|
||||
--limit;
|
||||
@@ -91,15 +91,15 @@ pair<bool, vector<User>> MySQLUserDAO::GetAll(size_t limit, size_t offset)
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool MySQLUserDAO::Update(const User& u)
|
||||
bool MySQLUserDAO::Update(const user& u)
|
||||
{
|
||||
auto schema = session_.getSchema("up_and_down");
|
||||
auto table = schema.getTable("users");
|
||||
mysqlx::Result res = table.update()
|
||||
.set("login", u.GetLogin())
|
||||
.set("hashed_password", u.GetHashedPassword())
|
||||
.set("login", u.login)
|
||||
.set("hashed_password", u.hashed_password)
|
||||
.where("uuid = :uuid")
|
||||
.bind("uuid", u.GetUUID())
|
||||
.bind("uuid", u.uuid)
|
||||
.execute();
|
||||
|
||||
return !!res.getAffectedItemsCount();
|
||||
@@ -117,7 +117,7 @@ bool MySQLUserDAO::Delete(const string& uuid)
|
||||
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();
|
||||
|
||||
@@ -132,12 +132,12 @@ std::optional<User> MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& s
|
||||
string user_login = row_data[1].get<string>();
|
||||
string user_hashed_password = row_data[2].get<string>();
|
||||
|
||||
User user;
|
||||
user single_user;
|
||||
|
||||
user.SetUUID(user_uuid);
|
||||
user.SetLogin(user_login);
|
||||
user.SetHashedPassword(user_hashed_password);
|
||||
single_user.uuid = user_uuid;
|
||||
single_user.login = user_login;
|
||||
single_user.hashed_password = user_hashed_password;
|
||||
|
||||
return optional<User>(std::move(user));
|
||||
return optional<user>(std::move(single_user));
|
||||
}
|
||||
} // uad
|
||||
|
||||
@@ -10,19 +10,19 @@ class MySQLUserDAO : public IUserDAO
|
||||
public:
|
||||
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;
|
||||
|
||||
private:
|
||||
std::optional<User> GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result);
|
||||
std::optional<user> GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result);
|
||||
};
|
||||
}
|
||||
@@ -62,9 +62,9 @@ public:
|
||||
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()};
|
||||
response_body.as_object().emplace("Result", "Incorrect login or password");
|
||||
@@ -76,7 +76,7 @@ public:
|
||||
return res;
|
||||
}
|
||||
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()};
|
||||
response_body.as_object().emplace("token", token);
|
||||
|
||||
@@ -90,10 +90,10 @@ public:
|
||||
return res;
|
||||
}
|
||||
|
||||
User user;
|
||||
user user;
|
||||
|
||||
user.SetLogin(login);
|
||||
user.SetPassword(password);
|
||||
user.login = login;
|
||||
user.hashed_password = HashPassword(password);
|
||||
|
||||
const auto uuid_stringified = user_dao_->Create(user);
|
||||
|
||||
@@ -107,7 +107,7 @@ public:
|
||||
);
|
||||
response_body.as_object().emplace(
|
||||
"login",
|
||||
user.GetLogin()
|
||||
user.login
|
||||
);
|
||||
|
||||
res.body() = serialize(response_body);
|
||||
|
||||
@@ -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_;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
}
|
||||
@@ -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
@@ -18,7 +18,7 @@
|
||||
#include "./session/WebsocketSession.h"
|
||||
#include "./listener/Listener.h"
|
||||
#include "./db/mysql_connector.h"
|
||||
#include "entities/User.h"
|
||||
#include "entities/user.h"
|
||||
|
||||
namespace beast = boost::beast;
|
||||
namespace http = beast::http;
|
||||
|
||||
Reference in New Issue
Block a user