diff --git a/src/DAO/IUserDAO.h b/src/DAO/IUserDAO.h index 5779866..fa8d4ab 100644 --- a/src/DAO/IUserDAO.h +++ b/src/DAO/IUserDAO.h @@ -16,7 +16,7 @@ public: virtual std::optional GetByLogin(std::string login) = 0; - virtual std::vector 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; diff --git a/src/DAO/MySQLUserDAO.cpp b/src/DAO/MySQLUserDAO.cpp index 1485c14..4704724 100644 --- a/src/DAO/MySQLUserDAO.cpp +++ b/src/DAO/MySQLUserDAO.cpp @@ -47,11 +47,49 @@ optional MySQLUserDAO::GetByLogin(string login) return GetSingleUserBySQLResult(std::move(sql_result)); } -vector MySQLUserDAO::GetAll(size_t limit, size_t offset) +pair> MySQLUserDAO::GetAll(size_t limit, size_t offset) { - vector users{}; + 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; - return {}; + if (!rows.size()) + { + ret.first = true + ; + ret.second = vector{}; + + return ret; + } + + ret.first = rows.size() < limit + 1; + ret.second = vector{}; + + ret.second.reserve(limit); + + for (const auto& row : rows) + { + if (!limit) + { + break; + } + + User user; + + string user_uuid = row[0].get(); + string user_login = row[1].get(); + + user.SetLogin(user_login); + user.SetUUID(user_uuid); + + ret.second.push_back(std::move(user)); + --limit; + } + + return ret; } bool MySQLUserDAO::Update(const User& u) diff --git a/src/DAO/MySQLUserDAO.h b/src/DAO/MySQLUserDAO.h index 84c4652..6cbea20 100644 --- a/src/DAO/MySQLUserDAO.h +++ b/src/DAO/MySQLUserDAO.h @@ -16,7 +16,7 @@ public: std::optional GetByLogin(std::string login) override; - std::vector GetAll(size_t limit, size_t offset) override; + std::pair> GetAll(size_t limit, size_t offset) override; bool Update(const User& u) override;