generated from Sithas/conan_template
Compare commits
5 Commits
f698196778
...
a3e69b65fb
| Author | SHA1 | Date | |
|---|---|---|---|
| a3e69b65fb | |||
| 50bad13f86 | |||
| 8657b2910b | |||
| 8e1db614bf | |||
| 1cb1a9d0eb |
+1
-1
@@ -55,7 +55,7 @@ add_executable(App ./src/main.cpp
|
|||||||
src/log/Log.h
|
src/log/Log.h
|
||||||
src/log/Log.cpp
|
src/log/Log.cpp
|
||||||
tests/fixtures/AuthFixture.h
|
tests/fixtures/AuthFixture.h
|
||||||
src/endpoints_handlers/UserGetMedicationsExecutor.h
|
src/endpoints_handlers/GetUserMedicationsExecutor.h
|
||||||
src/DAO/IMedicationsDAO.h
|
src/DAO/IMedicationsDAO.h
|
||||||
src/DAO/MySQLMedicationsDAO.h
|
src/DAO/MySQLMedicationsDAO.h
|
||||||
src/DAO/MySQLMedicationsDAO.cpp
|
src/DAO/MySQLMedicationsDAO.cpp
|
||||||
|
|||||||
+4
-4
@@ -14,13 +14,13 @@
|
|||||||
namespace uad
|
namespace uad
|
||||||
{
|
{
|
||||||
template <class Body, class Allocator, class ResponseType>
|
template <class Body, class Allocator, class ResponseType>
|
||||||
class UserGetMedicationsExecutor : public IExecutor<Body, Allocator, ResponseType>
|
class GetUserMedicationsExecutor : public IExecutor<Body, Allocator, ResponseType>
|
||||||
{
|
{
|
||||||
mysqlx::Session& session_;
|
mysqlx::Session& session_;
|
||||||
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
||||||
const std::shared_ptr<IMedicationsDAO>& medications_dao_;
|
const std::shared_ptr<IMedicationsDAO>& medications_dao_;
|
||||||
public:
|
public:
|
||||||
UserGetMedicationsExecutor(
|
GetUserMedicationsExecutor(
|
||||||
mysqlx::Session& session,
|
mysqlx::Session& session,
|
||||||
const std::shared_ptr<IAuthDAO>& auth_dao,
|
const std::shared_ptr<IAuthDAO>& auth_dao,
|
||||||
const std::shared_ptr<IMedicationsDAO>& medications_dao
|
const std::shared_ptr<IMedicationsDAO>& medications_dao
|
||||||
@@ -46,7 +46,7 @@ public:
|
|||||||
|
|
||||||
if (req[http::field::authorization].size() <= auth_prefix.size())
|
if (req[http::field::authorization].size() <= auth_prefix.size())
|
||||||
{
|
{
|
||||||
BOOST_LOG_TRIVIAL(info) << invalid_token_message;
|
BOOST_LOG_TRIVIAL(error) << invalid_token_message;
|
||||||
throw session_exception(http::status::unauthorized, "Unauthorized");
|
throw session_exception(http::status::unauthorized, "Unauthorized");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,7 +54,7 @@ public:
|
|||||||
|
|
||||||
if (!auth_dao_->HasAuthorized(auth_token))
|
if (!auth_dao_->HasAuthorized(auth_token))
|
||||||
{
|
{
|
||||||
BOOST_LOG_TRIVIAL(info) << invalid_token_message;
|
BOOST_LOG_TRIVIAL(error) << invalid_token_message;
|
||||||
throw session_exception(http::status::unauthorized, "Unauthorized");
|
throw session_exception(http::status::unauthorized, "Unauthorized");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,111 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <boost/log/trivial.hpp>
|
||||||
|
|
||||||
|
#include <regex>
|
||||||
|
#include <boost/json.hpp>
|
||||||
|
#include <boost/mpl/vector/vector0.hpp>
|
||||||
|
#include <mysqlx/xdevapi.h>
|
||||||
|
|
||||||
|
#include "IExecutor.h"
|
||||||
|
#include "../DAO/IAuthDAO.h"
|
||||||
|
#include "../DAO/IMedicationsDAO.h"
|
||||||
|
#include "../exceptions/session_exception.h"
|
||||||
|
|
||||||
|
namespace uad
|
||||||
|
{
|
||||||
|
template <class Body, class Allocator, class ResponseType>
|
||||||
|
class PostUserMedicationsExecutor : public IExecutor<Body, Allocator, ResponseType>
|
||||||
|
{
|
||||||
|
mysqlx::Session& session_;
|
||||||
|
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
||||||
|
const std::shared_ptr<IMedicationsDAO>& medications_dao_;
|
||||||
|
public:
|
||||||
|
PostUserMedicationsExecutor(
|
||||||
|
mysqlx::Session& session,
|
||||||
|
const std::shared_ptr<IAuthDAO>& auth_dao,
|
||||||
|
const std::shared_ptr<IMedicationsDAO>& medications_dao
|
||||||
|
): session_(session), auth_dao_(auth_dao), medications_dao_(medications_dao)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::beast::http::response<ResponseType> operator ()(
|
||||||
|
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>>&& req
|
||||||
|
) override
|
||||||
|
{
|
||||||
|
using namespace boost;
|
||||||
|
using namespace boost::json;
|
||||||
|
using namespace boost::beast;
|
||||||
|
using namespace std::string_literals;
|
||||||
|
using namespace std::string_view_literals;
|
||||||
|
|
||||||
|
constexpr std::string_view auth_prefix = "Bearer "sv;
|
||||||
|
static const std::string invalid_token_message = "POST /api/v1/User/Medications - Response 401: Unauthorized"s;
|
||||||
|
|
||||||
|
BOOST_LOG_TRIVIAL(info) << "POST /api/v1/User/Medications - Request";
|
||||||
|
|
||||||
|
if (req[http::field::authorization].size() <= auth_prefix.size())
|
||||||
|
{
|
||||||
|
BOOST_LOG_TRIVIAL(error) << invalid_token_message;
|
||||||
|
throw session_exception(http::status::unauthorized, "Unauthorized");
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string auth_token = {req[http::field::authorization].begin() + auth_prefix.size(), req[http::field::authorization].end()};
|
||||||
|
|
||||||
|
if (!auth_dao_->HasAuthorized(auth_token))
|
||||||
|
{
|
||||||
|
BOOST_LOG_TRIVIAL(error) << invalid_token_message;
|
||||||
|
throw session_exception(http::status::unauthorized, "Unauthorized");
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto body = req.body();
|
||||||
|
value req_json;
|
||||||
|
medication m;
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
req_json = json::parse(body);
|
||||||
|
}
|
||||||
|
catch (const system::system_error& err)
|
||||||
|
{
|
||||||
|
BOOST_LOG_TRIVIAL(info) << "POST /api/v1/User/Medications - Response 400: Cannot deserialize json";
|
||||||
|
throw session_exception(http::status::bad_request, "Cannot deserialize json");
|
||||||
|
}
|
||||||
|
|
||||||
|
m.name = req_json.as_object().at("name").as_string().c_str();
|
||||||
|
m.dose = req_json.as_object().at("dose").as_int64();
|
||||||
|
m.unit = req_json.as_object().at("unit").as_string().c_str();
|
||||||
|
m.is_urgent = req_json.as_object().at("is_urgent").as_bool();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
m.uuid = medications_dao_->Create(m);
|
||||||
|
}
|
||||||
|
catch (const std::runtime_error& err)
|
||||||
|
{
|
||||||
|
BOOST_LOG_TRIVIAL(error) << "POST /api/v1/User/Medications - Response 409: This medication already exists";
|
||||||
|
throw session_exception(http::status::conflict, "This medication already exists");
|
||||||
|
}
|
||||||
|
|
||||||
|
http::response<ResponseType> res{http::status::ok, req.version()};
|
||||||
|
|
||||||
|
res.body() = serialize(ToJSON(m));
|
||||||
|
res.set(http::field::content_type, "application/json");
|
||||||
|
res.content_length(res.body().size());
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
boost::json::object ToJSON(const medication& m)
|
||||||
|
{
|
||||||
|
return {
|
||||||
|
{ "uuid", m.uuid },
|
||||||
|
{ "name", m.name },
|
||||||
|
{ "dose", m.dose },
|
||||||
|
{ "unit", m.unit },
|
||||||
|
{ "is_urgent", m.is_urgent }
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -7,7 +7,8 @@
|
|||||||
#include "AuthRegistrationExecutor.h"
|
#include "AuthRegistrationExecutor.h"
|
||||||
#include "AuthLoginExecutor.h"
|
#include "AuthLoginExecutor.h"
|
||||||
#include "AuthLogoutExecutor.h"
|
#include "AuthLogoutExecutor.h"
|
||||||
#include "UserGetMedicationsExecutor.h"
|
#include "GetUserMedicationsExecutor.h"
|
||||||
|
#include "PostUserMedicationsExecutor.h"
|
||||||
#include "../DAO/IUserDAO.h"
|
#include "../DAO/IUserDAO.h"
|
||||||
#include "../DAO/IAuthDAO.h"
|
#include "../DAO/IAuthDAO.h"
|
||||||
#include "../DAO/IMedicationsDAO.h"
|
#include "../DAO/IMedicationsDAO.h"
|
||||||
@@ -26,7 +27,9 @@ class RootExecutor
|
|||||||
Body, Allocator, boost::beast::http::string_body>;
|
Body, Allocator, boost::beast::http::string_body>;
|
||||||
using RouteAuthLogoutExecutor = AuthLogoutExecutor<
|
using RouteAuthLogoutExecutor = AuthLogoutExecutor<
|
||||||
Body, Allocator, boost::beast::http::string_body>;
|
Body, Allocator, boost::beast::http::string_body>;
|
||||||
using RouteUserGetMedicationsExecutor = UserGetMedicationsExecutor<
|
using RouteUserGetMedicationsExecutor = GetUserMedicationsExecutor<
|
||||||
|
Body, Allocator, boost::beast::http::string_body>;
|
||||||
|
using RouteUserPostMedicationsExecutor = PostUserMedicationsExecutor<
|
||||||
Body, Allocator, boost::beast::http::string_body>;
|
Body, Allocator, boost::beast::http::string_body>;
|
||||||
using IRouteController = IController<Body, Allocator, boost::beast::http::string_body>;
|
using IRouteController = IController<Body, Allocator, boost::beast::http::string_body>;
|
||||||
using RouteController = Controller<Body, Allocator, boost::beast::http::string_body>;
|
using RouteController = Controller<Body, Allocator, boost::beast::http::string_body>;
|
||||||
@@ -85,7 +88,11 @@ public:
|
|||||||
{
|
{
|
||||||
boost::beast::http::verb::get,
|
boost::beast::http::verb::get,
|
||||||
std::make_shared<RouteUserGetMedicationsExecutor>(session_, auth_dao_, medications_dao_)
|
std::make_shared<RouteUserGetMedicationsExecutor>(session_, auth_dao_, medications_dao_)
|
||||||
}
|
},
|
||||||
|
{
|
||||||
|
boost::beast::http::verb::post,
|
||||||
|
std::make_shared<RouteUserPostMedicationsExecutor>(session_, auth_dao_, medications_dao_)
|
||||||
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-1
@@ -15,8 +15,10 @@ void InitLogs()
|
|||||||
logging::keywords::rotation_size = 10 * 1024 * 1024,
|
logging::keywords::rotation_size = 10 * 1024 * 1024,
|
||||||
logging::keywords::time_based_rotation =
|
logging::keywords::time_based_rotation =
|
||||||
logging::sinks::file::rotation_at_time_point(0, 0, 0),
|
logging::sinks::file::rotation_at_time_point(0, 0, 0),
|
||||||
logging::keywords::format = "[%TimeStamp%] [%Severity%]: %Message%"
|
logging::keywords::format =
|
||||||
|
"[%TimeStamp%] [%Severity%]: %Message%"
|
||||||
);
|
);
|
||||||
|
|
||||||
logging::add_common_attributes();
|
logging::add_common_attributes();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user