diff --git a/CMakeLists.txt b/CMakeLists.txt index 785cc1e..5f29a50 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,8 +38,8 @@ add_executable(App ./src/main.cpp ./src/db/mysql_connector.cpp ./src/db/mysql_connector.h ./src/DAO/IUserDAO.h - ./src/entities/user.h - ./src/entities/medication.h + src/dtos/user_dto.h + src/dtos/medication_dto.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 a8b9d1d..1a9e17d 100644 --- a/src/DAO/IAuthDAO.h +++ b/src/DAO/IAuthDAO.h @@ -3,7 +3,7 @@ #include -#include "../entities/user.h" +#include "../dtos/user_dto.h" namespace uad { diff --git a/src/DAO/IMedicationsDAO.h b/src/DAO/IMedicationsDAO.h index 838c0c2..691b971 100644 --- a/src/DAO/IMedicationsDAO.h +++ b/src/DAO/IMedicationsDAO.h @@ -3,16 +3,16 @@ #include #include -#include "../entities/medication.h" +#include "../dtos/medication_dto.h" namespace uad { class IMedicationsDAO { public: - [[nodiscard]] virtual std::vector GetAll() const = 0; + [[nodiscard]] virtual std::vector GetAll() const = 0; - [[nodiscard]] virtual std::string Create(const medication& m) const = 0; + [[nodiscard]] virtual std::string Create(const medication_dto& m) const = 0; virtual ~IMedicationsDAO() = default; }; diff --git a/src/DAO/IUserDAO.h b/src/DAO/IUserDAO.h index 47dfe42..a49881b 100644 --- a/src/DAO/IUserDAO.h +++ b/src/DAO/IUserDAO.h @@ -4,22 +4,22 @@ #include #include -#include "../entities/user.h" +#include "../dtos/user_dto.h" namespace uad { class IUserDAO { public: - virtual std::string Create(const user& created_user) = 0; + virtual std::string Create(const user_dto& 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_dto& u) = 0; virtual bool Delete(const std::string& uuid) = 0; diff --git a/src/DAO/MySQLMedicationsDAO.cpp b/src/DAO/MySQLMedicationsDAO.cpp index f5dbe88..ef6d2a6 100644 --- a/src/DAO/MySQLMedicationsDAO.cpp +++ b/src/DAO/MySQLMedicationsDAO.cpp @@ -10,20 +10,20 @@ MySQLMedicationsDAO::MySQLMedicationsDAO(mysqlx::Session& session): session_(ses { } -std::vector MySQLMedicationsDAO::GetAll() const +std::vector MySQLMedicationsDAO::GetAll() const { static const string sql_script = "SELECT uuid, name, dose, unit, is_urgent FROM up_and_down.medications;"; mysqlx::SqlResult sql_result = session_.sql(sql_script).execute(); list rows = sql_result.fetchAll(); - vector ret; + vector ret; if (rows.empty()) return ret; for (const auto& row : rows) { - medication m; + medication_dto m; m.uuid = row[0].get(); m.name = row[1].get(); @@ -37,7 +37,7 @@ std::vector MySQLMedicationsDAO::GetAll() const return ret; } -std::string MySQLMedicationsDAO::Create(const medication& m) const +std::string MySQLMedicationsDAO::Create(const medication_dto& m) const { boost::uuids::random_generator generator; boost::uuids::uuid uuid = generator(); diff --git a/src/DAO/MySQLMedicationsDAO.h b/src/DAO/MySQLMedicationsDAO.h index baa6a03..437e79c 100644 --- a/src/DAO/MySQLMedicationsDAO.h +++ b/src/DAO/MySQLMedicationsDAO.h @@ -4,7 +4,7 @@ #include #include "IMedicationsDAO.h" -#include "../entities/medication.h" +#include "../dtos/medication_dto.h" namespace uad { @@ -14,8 +14,8 @@ class MySQLMedicationsDAO : public IMedicationsDAO public: explicit MySQLMedicationsDAO(mysqlx::Session& session); - [[nodiscard]] std::vector GetAll() const override; + [[nodiscard]] std::vector GetAll() const override; - [[nodiscard]] std::string Create(const medication& m) const override; + [[nodiscard]] std::string Create(const medication_dto& m) const override; }; } diff --git a/src/DAO/MySQLUserDAO.cpp b/src/DAO/MySQLUserDAO.cpp index 55023c4..0736382 100644 --- a/src/DAO/MySQLUserDAO.cpp +++ b/src/DAO/MySQLUserDAO.cpp @@ -14,7 +14,7 @@ MySQLUserDAO::MySQLUserDAO(mysqlx::Session& session) : { } -string MySQLUserDAO::Create(const user& created_user) +string MySQLUserDAO::Create(const user_dto& created_user) { boost::uuids::random_generator generator; boost::uuids::uuid uuid = generator(); @@ -31,7 +31,7 @@ string MySQLUserDAO::Create(const user& created_user) return uuid_str; } -optional MySQLUserDAO::GetByUUID(const string& uuid) +optional MySQLUserDAO::GetByUUID(const string& uuid) { static const string sql_script = "SELECT * FROM `up_and_down`.`users` WHERE (uuid = ?) LIMIT 1;"s; mysqlx::SqlResult sql_result = session_. @@ -42,7 +42,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) { static const std::string sql_script = "SELECT * FROM `up_and_down`.`users` WHERE (login = ?) LIMIT 1;"s; mysqlx::SqlResult sql_result = session_. @@ -53,7 +53,7 @@ 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) { static const string sql_script = "SELECT * FROM `up_and_down`.`users` LIMIT ? OFFSET ?;"s; @@ -62,18 +62,18 @@ pair> MySQLUserDAO::GetAll(size_t limit, size_t offset) .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); @@ -84,7 +84,7 @@ pair> MySQLUserDAO::GetAll(size_t limit, size_t offset) break; } - user user; + user_dto user; const string user_uuid = row[0].get(); const string user_login = row[1].get(); @@ -96,7 +96,7 @@ pair> MySQLUserDAO::GetAll(size_t limit, size_t offset) return ret; } -bool MySQLUserDAO::Update(const user& u) +bool MySQLUserDAO::Update(const user_dto& u) { static const string sql_script = "UPDATE `up_and_down`.`users` SET `login` = ? WHERE `uuid` = ?;"s; @@ -118,7 +118,7 @@ bool MySQLUserDAO::Delete(const string& uuid) return !!schema.getAffectedItemsCount(); } -std::optional MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result) +std::optional MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result) { list rows = sql_result.fetchAll(); @@ -133,7 +133,7 @@ std::optional MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& s const string user_login = row_data[1].get(); const string user_hashed_password = row_data[2].get(); - return optional({ + return optional({ .uuid = user_uuid, .login = user_login, .hashed_password = user_hashed_password diff --git a/src/DAO/MySQLUserDAO.h b/src/DAO/MySQLUserDAO.h index b7fac08..4e5864a 100644 --- a/src/DAO/MySQLUserDAO.h +++ b/src/DAO/MySQLUserDAO.h @@ -11,19 +11,19 @@ class MySQLUserDAO : public IUserDAO public: explicit MySQLUserDAO(mysqlx::Session& session); - std::string Create(const user& created_user) override; + std::string Create(const user_dto& 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_dto& 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/entities/medication.h b/src/dtos/medication_dto.h similarity index 89% rename from src/entities/medication.h rename to src/dtos/medication_dto.h index 30b3f50..b427797 100644 --- a/src/entities/medication.h +++ b/src/dtos/medication_dto.h @@ -6,7 +6,7 @@ namespace uad { -struct medication +struct medication_dto { std::string uuid; std::string name; diff --git a/src/entities/user.h b/src/dtos/user_dto.h similarity index 90% rename from src/entities/user.h rename to src/dtos/user_dto.h index cc7ac61..ac7c835 100644 --- a/src/entities/user.h +++ b/src/dtos/user_dto.h @@ -6,7 +6,7 @@ namespace uad { -struct user +struct user_dto { std::string uuid; std::string login; diff --git a/src/endpoints_handlers/AuthLoginExecutor.h b/src/endpoints_handlers/AuthLoginExecutor.h index 47eef99..e2b49ba 100644 --- a/src/endpoints_handlers/AuthLoginExecutor.h +++ b/src/endpoints_handlers/AuthLoginExecutor.h @@ -64,7 +64,7 @@ public: throw session_exception(http::status::unprocessable_entity, "Login or password are empty"s); } - const std::optional maybe_user = user_dao_->GetByLogin(login); + const std::optional maybe_user = user_dao_->GetByLogin(login); if (!maybe_user.has_value() || maybe_user.value().hashed_password != HashPassword(password)) { diff --git a/src/endpoints_handlers/AuthRegistrationExecutor.h b/src/endpoints_handlers/AuthRegistrationExecutor.h index 0153454..01eef68 100644 --- a/src/endpoints_handlers/AuthRegistrationExecutor.h +++ b/src/endpoints_handlers/AuthRegistrationExecutor.h @@ -66,7 +66,7 @@ public: throw session_exception(http::status::conflict, "User with login "s + login + " exists"s); } - user user; + user_dto user; user.login = login; user.hashed_password = HashPassword(password); diff --git a/src/endpoints_handlers/GetUserMedicationsExecutor.h b/src/endpoints_handlers/GetUserMedicationsExecutor.h index d57814b..c1093c7 100644 --- a/src/endpoints_handlers/GetUserMedicationsExecutor.h +++ b/src/endpoints_handlers/GetUserMedicationsExecutor.h @@ -59,7 +59,7 @@ public: } http::response res{http::status::ok, req.version()}; - const std::vector medications = medications_dao_->GetAll(); + const std::vector medications = medications_dao_->GetAll(); value response_body; response_body.emplace_object(); @@ -73,7 +73,7 @@ public: } private: - boost::json::object ToJSON(const medication& m) + boost::json::object ToJSON(const medication_dto& m) { return { { "uuid", m.uuid }, @@ -84,7 +84,7 @@ private: }; } - boost::json::array toJSONArray(const std::vector& medications) + boost::json::array toJSONArray(const std::vector& medications) { using namespace boost; using namespace boost::json; diff --git a/src/endpoints_handlers/PostUserMedicationsExecutor.h b/src/endpoints_handlers/PostUserMedicationsExecutor.h index 7bacb17..1fa4f54 100644 --- a/src/endpoints_handlers/PostUserMedicationsExecutor.h +++ b/src/endpoints_handlers/PostUserMedicationsExecutor.h @@ -60,7 +60,7 @@ public: const auto body = req.body(); value req_json; - medication m; + medication_dto m; try { @@ -97,7 +97,7 @@ public: } private: - boost::json::object ToJSON(const medication& m) + boost::json::object ToJSON(const medication_dto& m) { return { { "uuid", m.uuid }, diff --git a/src/main.cpp b/src/main.cpp index 925f75c..28473d7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,7 +20,7 @@ #include "./session/WebsocketSession.h" #include "./listener/Listener.h" #include "./db/mysql_connector.h" -#include "entities/user.h" +#include "dtos/user_dto.h" #include "log/Log.h" namespace beast = boost::beast; diff --git a/tests/endpoint_handlers/AuthRegistrationExecutor_TEST.cpp b/tests/endpoint_handlers/AuthRegistrationExecutor_TEST.cpp index c0e5a73..57c072f 100644 --- a/tests/endpoint_handlers/AuthRegistrationExecutor_TEST.cpp +++ b/tests/endpoint_handlers/AuthRegistrationExecutor_TEST.cpp @@ -81,7 +81,7 @@ BOOST_FIXTURE_TEST_CASE(AuthRegistrationExecutor_Unsuccesfull_User_Registration, RouteAuthRegistrationExecutor executor(GetMySqlSession(), user_dao); user_dao->Create( - user { + user_dto { .login = "MyLogin123456780"s + uuid, .hashed_password = HashPassword("Qwerty123456"s)} );