generated from Sithas/conan_template
143 lines
3.6 KiB
C++
143 lines
3.6 KiB
C++
#include <boost/uuid.hpp>
|
|
|
|
#include "MySQLUserDAO.h"
|
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
using namespace string_literals;
|
|
|
|
namespace uad
|
|
{
|
|
MySQLUserDAO::MySQLUserDAO(mysqlx::Session& session) :
|
|
session_(session)
|
|
{
|
|
}
|
|
|
|
string MySQLUserDAO::Create(const user_dto& created_user)
|
|
{
|
|
boost::uuids::random_generator generator;
|
|
boost::uuids::uuid uuid = generator();
|
|
const std::string uuid_str = boost::uuids::to_string(uuid);
|
|
|
|
|
|
static const string sql_script =
|
|
"INSERT INTO `up_and_down`.`users` (`uuid`, `login`, `hashed_password`) VALUES (?, ?, ?);"s;
|
|
|
|
session_.
|
|
sql(sql_script)
|
|
.bind(uuid_str, created_user.login, created_user.hashed_password).execute();
|
|
|
|
return uuid_str;
|
|
}
|
|
|
|
optional<user_dto> 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_.
|
|
sql(sql_script)
|
|
.bind(uuid)
|
|
.execute();
|
|
|
|
return GetSingleUserBySQLResult(std::move(sql_result));
|
|
}
|
|
|
|
optional<user_dto> 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_.
|
|
sql(sql_script)
|
|
.bind(login)
|
|
.execute();
|
|
|
|
return GetSingleUserBySQLResult(std::move(sql_result));
|
|
}
|
|
|
|
pair<bool, vector<user_dto>> MySQLUserDAO::GetAll(size_t limit, size_t offset)
|
|
{
|
|
static const string sql_script = "SELECT * FROM `up_and_down`.`users` LIMIT ? OFFSET ?;"s;
|
|
|
|
mysqlx::SqlResult sql_result = session_
|
|
.sql(sql_script)
|
|
.bind(limit, offset)
|
|
.execute();
|
|
list<mysqlx::Row> rows = sql_result.fetchAll();
|
|
pair<bool, vector<user_dto>> ret;
|
|
|
|
if (!rows.size())
|
|
{
|
|
ret.first = true;
|
|
ret.second = vector<user_dto>{};
|
|
|
|
return ret;
|
|
}
|
|
|
|
ret.first = rows.size() < limit + 1;
|
|
ret.second = vector<user_dto>{};
|
|
|
|
ret.second.reserve(limit);
|
|
|
|
for (const auto& row : rows)
|
|
{
|
|
if (!limit)
|
|
{
|
|
break;
|
|
}
|
|
|
|
user_dto user;
|
|
|
|
const string user_uuid = row[0].get<string>();
|
|
const string user_login = row[1].get<string>();
|
|
|
|
ret.second.push_back({.uuid = user_uuid, .login = user_login});
|
|
--limit;
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
bool MySQLUserDAO::Update(const user_dto& u)
|
|
{
|
|
static const string sql_script = "UPDATE `up_and_down`.`users` SET `login` = ? WHERE `uuid` = ?;"s;
|
|
|
|
auto schema = session_.sql(sql_script)
|
|
.bind(u.login, u.uuid)
|
|
.execute();
|
|
|
|
return !!schema.getAffectedItemsCount();
|
|
}
|
|
|
|
bool MySQLUserDAO::Delete(const string& uuid)
|
|
{
|
|
static const string sql_script = "DELETE FROM `up_and_down`.`users` WHERE `uuid` = ?;";
|
|
|
|
auto schema = session_.sql(sql_script)
|
|
.bind(uuid)
|
|
.execute();
|
|
|
|
return !!schema.getAffectedItemsCount();
|
|
}
|
|
|
|
std::optional<user_dto> MySQLUserDAO::GetSingleUserBySQLResult(mysqlx::SqlResult&& sql_result)
|
|
{
|
|
list<mysqlx::Row> rows = sql_result.fetchAll();
|
|
|
|
if (!rows.size())
|
|
{
|
|
return nullopt;
|
|
}
|
|
|
|
auto row_data = *rows.begin();
|
|
|
|
const string user_uuid = row_data[0].get<string>();
|
|
const string user_login = row_data[1].get<string>();
|
|
const string user_hashed_password = row_data[2].get<string>();
|
|
|
|
return optional<user_dto>({
|
|
.uuid = user_uuid,
|
|
.login = user_login,
|
|
.hashed_password = user_hashed_password
|
|
});
|
|
}
|
|
} // uad
|