userDao: Implement list()

This commit is contained in:
Albert S. 2021-09-26 15:05:15 +02:00
parent 8d685dc581
commit 164b2c19ee
3 changed files with 37 additions and 0 deletions

View File

@ -3,6 +3,7 @@
#include <string> #include <string>
#include <optional> #include <optional>
#include "../user.h" #include "../user.h"
#include "queryoption.h"
class UserDao class UserDao
{ {
public: public:
@ -10,6 +11,7 @@ class UserDao
virtual bool exists(std::string username) = 0; virtual bool exists(std::string username) = 0;
virtual std::optional<User> find(std::string username) = 0; virtual std::optional<User> find(std::string username) = 0;
virtual std::optional<User> find(int id) = 0; virtual std::optional<User> find(int id) = 0;
virtual std::vector<User> list(QueryOption o) = 0;
virtual void deleteUser(std::string username) = 0; virtual void deleteUser(std::string username) = 0;
virtual void save(const User &u) = 0; virtual void save(const User &u) = 0;
virtual ~UserDao(){}; virtual ~UserDao(){};

View File

@ -23,6 +23,7 @@ SOFTWARE.
#include <memory> #include <memory>
#include <cstring> #include <cstring>
#include "userdaosqlite.h" #include "userdaosqlite.h"
#include "sqlitequeryoption.h"
UserDaoSqlite::UserDaoSqlite() UserDaoSqlite::UserDaoSqlite()
{ {
@ -82,6 +83,39 @@ std::optional<User> UserDaoSqlite::find(int id)
} }
} }
std::vector<User> UserDaoSqlite::list(QueryOption o)
{
std::vector<User> result;
try
{
std::string queryOption = SqliteQueryOption(o).setOrderByColumn("username").setPrependWhere(true).build();
std::string query = "SELECT username, password, salt, permissions, enabled FROM user " + queryOption;
*db << query >>
[&](std::string username, std::vector<char> pw, std::vector<char> salt, int permisisons, bool enabled)
{
User tmp;
tmp.login = username;
tmp.password = pw;
tmp.salt = salt;
tmp.permissions = Permissions{permisisons};
tmp.enabled = enabled;
result.push_back(tmp);
};
}
catch(const sqlite::errors::no_rows &e)
{
return result;
}
catch(sqlite::sqlite_exception &e)
{
throwFrom(e);
}
return result;
}
void UserDaoSqlite::deleteUser(std::string username) void UserDaoSqlite::deleteUser(std::string username)
{ {
// What to do with the contributions of the user? // What to do with the contributions of the user?

View File

@ -13,6 +13,7 @@ class UserDaoSqlite : public UserDao, protected SqliteDao
std::optional<User> find(std::string username); std::optional<User> find(std::string username);
std::optional<User> find(int id); std::optional<User> find(int id);
std::vector<User> list(QueryOption o);
void deleteUser(std::string username); void deleteUser(std::string username);
void save(const User &u); void save(const User &u);
using SqliteDao::SqliteDao; using SqliteDao::SqliteDao;