generated from Sithas/conan_template
Добавление ДАО для использования в новом маршруте
This commit is contained in:
@@ -10,9 +10,9 @@ namespace uad
|
||||
class IMedicationsDAO
|
||||
{
|
||||
public:
|
||||
virtual std::vector<medication> GetAll() = 0;
|
||||
[[nodiscard]] virtual std::vector<medication> GetAll() const = 0;
|
||||
|
||||
virtual std::string Create(medication) = 0;
|
||||
[[nodiscard]] virtual std::string Create(const medication& m) const = 0;
|
||||
|
||||
virtual ~IMedicationsDAO() = default;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
#include <boost/uuid.hpp>
|
||||
|
||||
#include "MySQLMedicationsDAO.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace uad
|
||||
{
|
||||
MySQLMedicationsDAO::MySQLMedicationsDAO(mysqlx::Session& session): session_(session)
|
||||
{
|
||||
}
|
||||
|
||||
std::vector<medication> 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<mysqlx::Row> rows = sql_result.fetchAll();
|
||||
|
||||
vector<medication> ret;
|
||||
|
||||
if (rows.empty()) return ret;
|
||||
|
||||
for (const auto& row : rows)
|
||||
{
|
||||
medication m;
|
||||
|
||||
m.uuid = row[0].get<string>();
|
||||
m.name = row[1].get<string>();
|
||||
m.dose = row[2].get<int64_t>();
|
||||
m.unit = row[3].get<string>();
|
||||
m.is_urgent = row[4].get<bool>();
|
||||
|
||||
ret.push_back(m);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string MySQLMedicationsDAO::Create(const medication& m) const
|
||||
{
|
||||
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`.`medications` (`uuid`, `name`, `dose`, `unit`, `is_urgent`) VALUES ( ?, ?, ?, ?, ?);"s;
|
||||
|
||||
session_.sql(sql_script).bind(uuid_str, m.name, m.dose, m.unit, m.is_urgent).execute();
|
||||
|
||||
return uuid_str;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
#include <mysqlx/xdevapi.h>
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#include "IMedicationsDAO.h"
|
||||
#include "../entities/medication.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
class MySQLMedicationsDAO : public IMedicationsDAO
|
||||
{
|
||||
mysqlx::Session& session_;
|
||||
public:
|
||||
explicit MySQLMedicationsDAO(mysqlx::Session& session);
|
||||
|
||||
[[nodiscard]] std::vector<medication> GetAll() const override;
|
||||
|
||||
[[nodiscard]] std::string Create(const medication& m) const override;
|
||||
};
|
||||
}
|
||||
@@ -4,10 +4,12 @@
|
||||
|
||||
#include "../db/mysql_connector.h"
|
||||
#include "../DAO/IUserDAO.h"
|
||||
#include "../DAO/IMedicationsDAO.h"
|
||||
#include "AuthRegistrationExecutor.h"
|
||||
#include "RootExecutor.h"
|
||||
#include "../DAO/MemoryAuthDAO.h"
|
||||
#include "../DAO/MySQLUserDAO.h"
|
||||
#include "../DAO/MySQLMedicationsDAO.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
@@ -19,11 +21,13 @@ void HandleRequest(
|
||||
{
|
||||
static std::shared_ptr<IUserDAO> user_dao = std::make_shared<MySQLUserDAO>(GetMySqlSession());
|
||||
static std::shared_ptr<IAuthDAO> auth_dao = std::make_shared<MemoryAuthDAO>(GetMySqlSession());
|
||||
static std::shared_ptr<IMedicationsDAO> medications_dao = std::make_shared<MySQLMedicationsDAO>(GetMySqlSession());
|
||||
|
||||
static RootExecutor<Body, Allocator, boost::beast::http::string_body, Send> root_executor(
|
||||
GetMySqlSession(),
|
||||
user_dao,
|
||||
auth_dao
|
||||
auth_dao,
|
||||
medications_dao
|
||||
);
|
||||
|
||||
root_executor(doc_root, std::move(req), std::forward<Send>(send));
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "UserGetMedicationsExecutor.h"
|
||||
#include "../DAO/IUserDAO.h"
|
||||
#include "../DAO/IAuthDAO.h"
|
||||
#include "../DAO/IMedicationsDAO.h"
|
||||
#include "./../helpers/helpers.h"
|
||||
#include "./../exceptions/session_exception.h"
|
||||
|
||||
@@ -40,13 +41,15 @@ private:
|
||||
mysqlx::Session& session_;
|
||||
const std::shared_ptr<IUserDAO>& user_dao_;
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
||||
const std::shared_ptr<IMedicationsDAO>& medications_dao_;
|
||||
|
||||
public:
|
||||
RootExecutor(
|
||||
mysqlx::Session& session,
|
||||
const std::shared_ptr<IUserDAO>& user_dao,
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao) :
|
||||
session_(session), user_dao_(user_dao), auth_dao_(auth_dao)
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao,
|
||||
const std::shared_ptr<IMedicationsDAO>& medications_dao) :
|
||||
session_(session), user_dao_(user_dao), auth_dao_(auth_dao), medications_dao_(medications_dao)
|
||||
{
|
||||
routes_pathes_["/api/v1/Auth/Register"] = std::make_unique<RouteController>(
|
||||
typename RouteController::HTTPMethodsToExecutors{
|
||||
|
||||
@@ -9,7 +9,9 @@ namespace uad
|
||||
struct medication
|
||||
{
|
||||
std::string uuid;
|
||||
std::string login;
|
||||
std::string hashed_password;
|
||||
std::string name;
|
||||
int64_t dose;
|
||||
std::string unit;
|
||||
bool is_urgent;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user