generated from Sithas/conan_template
53 lines
1.3 KiB
C++
53 lines
1.3 KiB
C++
#include <boost/uuid.hpp>
|
|
|
|
#include "MySQLMedicationsDAO.h"
|
|
|
|
using namespace std;
|
|
|
|
namespace uad
|
|
{
|
|
MySQLMedicationsDAO::MySQLMedicationsDAO(mysqlx::Session& session): session_(session)
|
|
{
|
|
}
|
|
|
|
std::vector<medication_dto> 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_dto> ret;
|
|
|
|
if (rows.empty()) return ret;
|
|
|
|
for (const auto& row : rows)
|
|
{
|
|
medication_dto 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_dto& 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;
|
|
}
|
|
}
|