diff --git a/CMakeLists.txt b/CMakeLists.txt index 70aac04..877e7b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,8 +38,7 @@ add_executable(App ./src/main.cpp ./src/db/mysql_connector.cpp ./src/db/mysql_connector.h ./src/DAO/IUserDAO.h - ./src/entities/User.cpp - ./src/entities/User.h + ./src/entities/user.h ./src/DAO/MySQLUserDAO.cpp ./src/DAO/MySQLUserDAO.h ./src/endpoints_handlers/IExecutor.h diff --git a/src/DAO/IAuthDAO.h b/src/DAO/IAuthDAO.h index 6fbde19..a8b9d1d 100644 --- a/src/DAO/IAuthDAO.h +++ b/src/DAO/IAuthDAO.h @@ -3,7 +3,7 @@ #include -#include "../entities/User.h" +#include "../entities/user.h" namespace uad { diff --git a/src/DAO/IUserDAO.h b/src/DAO/IUserDAO.h index f00f998..47dfe42 100644 --- a/src/DAO/IUserDAO.h +++ b/src/DAO/IUserDAO.h @@ -4,22 +4,22 @@ #include #include -#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 GetByUUID(const std::string& uuid) = 0; + virtual std::optional GetByUUID(const std::string& uuid) = 0; - virtual std::optional GetByLogin(const std::string& login) = 0; + virtual std::optional GetByLogin(const std::string& login) = 0; - virtual std::pair> GetAll(size_t limit, size_t offset) = 0; + virtual std::pair> 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; diff --git a/src/DAO/MySQLUserDAO.cpp b/src/DAO/MySQLUserDAO.cpp index 0cb958e..2ab829f 100644 --- a/src/DAO/MySQLUserDAO.cpp +++ b/src/DAO/MySQLUserDAO.cpp @@ -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 MySQLUserDAO::GetByUUID(const string& uuid) +optional 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 MySQLUserDAO::GetByUUID(const string& uuid) return GetSingleUserBySQLResult(std::move(sql_result)); } -optional MySQLUserDAO::GetByLogin(const string& login) +optional 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 MySQLUserDAO::GetByLogin(const string& login) return GetSingleUserBySQLResult(std::move(sql_result)); } -pair> MySQLUserDAO::GetAll(size_t limit, size_t offset) +pair> 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 rows = sql_result.fetchAll(); - pair> ret; + pair> ret; if (!rows.size()) { ret.first = true; - ret.second = vector{}; + ret.second = vector{}; return ret; } ret.first = rows.size() != limit + 1; - ret.second = vector{}; + ret.second = vector{}; ret.second.reserve(limit); @@ -76,13 +76,13 @@ pair> MySQLUserDAO::GetAll(size_t limit, size_t offset) break; } - User user; + user user; string user_uuid = row[0].get(); string user_login = row[1].get(); - 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> 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 MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result) +std::optional MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result) { list rows = sql_result.fetchAll(); @@ -132,12 +132,12 @@ std::optional MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& s string user_login = row_data[1].get(); string user_hashed_password = row_data[2].get(); - 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(std::move(user)); + return optional(std::move(single_user)); } } // uad diff --git a/src/DAO/MySQLUserDAO.h b/src/DAO/MySQLUserDAO.h index 03378d6..86facf5 100644 --- a/src/DAO/MySQLUserDAO.h +++ b/src/DAO/MySQLUserDAO.h @@ -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 GetByUUID(const std::string& uuid) override; + std::optional GetByUUID(const std::string& uuid) override; - std::optional GetByLogin(const std::string& login) override; + std::optional GetByLogin(const std::string& login) override; - std::pair> GetAll(size_t limit, size_t offset) override; + std::pair> 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 GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result); + std::optional GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result); }; } \ No newline at end of file diff --git a/src/endpoints_handlers/AuthLoginExecutor.h b/src/endpoints_handlers/AuthLoginExecutor.h index bf66fb1..b77e34e 100644 --- a/src/endpoints_handlers/AuthLoginExecutor.h +++ b/src/endpoints_handlers/AuthLoginExecutor.h @@ -62,9 +62,9 @@ public: return res; } - std::optional maybe_user = user_dao_->GetByLogin(login); + std::optional 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 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 res{http::status::ok, req.version()}; response_body.as_object().emplace("token", token); diff --git a/src/endpoints_handlers/AuthRegistrationExecutor.h b/src/endpoints_handlers/AuthRegistrationExecutor.h index 2532b6b..7bb69b2 100644 --- a/src/endpoints_handlers/AuthRegistrationExecutor.h +++ b/src/endpoints_handlers/AuthRegistrationExecutor.h @@ -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); diff --git a/src/entities/User.cpp b/src/entities/User.cpp deleted file mode 100644 index 208f7af..0000000 --- a/src/entities/User.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include -#include - -#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{}(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_; -} -} \ No newline at end of file diff --git a/src/entities/User.h b/src/entities/User.h deleted file mode 100644 index c69d3bb..0000000 --- a/src/entities/User.h +++ /dev/null @@ -1,29 +0,0 @@ -#pragma once - -#include - -#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; -}; -} diff --git a/src/entities/user.h b/src/entities/user.h new file mode 100644 index 0000000..cc7ac61 --- /dev/null +++ b/src/entities/user.h @@ -0,0 +1,15 @@ +#pragma once + +#include + +#include "../helpers/helpers.h" + +namespace uad +{ +struct user +{ + std::string uuid; + std::string login; + std::string hashed_password; +}; +} diff --git a/src/main.cpp b/src/main.cpp index ab0bdc4..ec8e577 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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;