generated from Sithas/conan_template
База для ручки PostUserMedicationsExecutor.h
This commit is contained in:
@@ -16,6 +16,8 @@ public:
|
|||||||
|
|
||||||
virtual bool HasAuthorized(const std::string& auth_token) = 0;
|
virtual bool HasAuthorized(const std::string& auth_token) = 0;
|
||||||
|
|
||||||
|
virtual std::string_view GetLogin(const std::string& auth_token) = 0;
|
||||||
|
|
||||||
virtual bool Logout(const std::string& user_token) = 0;
|
virtual bool Logout(const std::string& user_token) = 0;
|
||||||
|
|
||||||
virtual ~IAuthDAO() = default;
|
virtual ~IAuthDAO() = default;
|
||||||
|
|||||||
@@ -25,6 +25,11 @@ bool MemoryAuthDAO::HasAuthorized(const std::string& auth_token)
|
|||||||
return auth_tokens_to_users_uuids_.count(auth_token) > 0;
|
return auth_tokens_to_users_uuids_.count(auth_token) > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string_view MemoryAuthDAO::GetLogin(const std::string& auth_token)
|
||||||
|
{
|
||||||
|
return auth_tokens_to_users_uuids_.at(auth_token);
|
||||||
|
}
|
||||||
|
|
||||||
bool MemoryAuthDAO::Logout(const std::string& auth_token)
|
bool MemoryAuthDAO::Logout(const std::string& auth_token)
|
||||||
{
|
{
|
||||||
if (!HasAuthorized(auth_token)) return false;
|
if (!HasAuthorized(auth_token)) return false;
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ public:
|
|||||||
|
|
||||||
bool HasAuthorized(const std::string& auth_token) override;
|
bool HasAuthorized(const std::string& auth_token) override;
|
||||||
|
|
||||||
|
std::string_view GetLogin(const std::string& auth_token) override;
|
||||||
|
|
||||||
bool Logout(const std::string& auth_token) override;
|
bool Logout(const std::string& auth_token) override;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ private:
|
|||||||
arr.emplace_back(ToJSON(m));
|
arr.emplace_back(ToJSON(m));
|
||||||
}
|
}
|
||||||
|
|
||||||
return arr;
|
return std::move(arr);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,14 +19,14 @@ class GetUserTreatmentSchemeExecutor : public IExecutor<Body, Allocator, Respons
|
|||||||
mysqlx::Session& session_;
|
mysqlx::Session& session_;
|
||||||
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
||||||
const std::shared_ptr<IUserTreatmentSchemeDAO>& user_treatment_scheme_dao_;
|
const std::shared_ptr<IUserTreatmentSchemeDAO>& user_treatment_scheme_dao_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GetUserTreatmentSchemeExecutor(
|
GetUserTreatmentSchemeExecutor(
|
||||||
mysqlx::Session& session,
|
mysqlx::Session& session,
|
||||||
const std::shared_ptr<IAuthDAO>& auth_dao,
|
const std::shared_ptr<IAuthDAO>& auth_dao,
|
||||||
const std::shared_ptr<IUserTreatmentSchemeDAO>& user_treatment_scheme_dao
|
const std::shared_ptr<IUserTreatmentSchemeDAO>& user_treatment_scheme_dao
|
||||||
): session_(session), auth_dao_(auth_dao), user_treatment_scheme_dao_(user_treatment_scheme_dao)
|
) : session_(session), auth_dao_(auth_dao), user_treatment_scheme_dao_(user_treatment_scheme_dao)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boost::beast::http::response<ResponseType> operator ()(
|
boost::beast::http::response<ResponseType> operator ()(
|
||||||
@@ -40,7 +40,8 @@ public:
|
|||||||
using namespace std::string_view_literals;
|
using namespace std::string_view_literals;
|
||||||
|
|
||||||
constexpr std::string_view auth_prefix = "Bearer "sv;
|
constexpr std::string_view auth_prefix = "Bearer "sv;
|
||||||
static const std::string invalid_token_message = "GET /api/v1/User/Medications - Response 401: Unauthorized"s;
|
static const std::string invalid_token_message =
|
||||||
|
"GET /api/v1/User/Medications - Response 401: Unauthorized"s;
|
||||||
|
|
||||||
BOOST_LOG_TRIVIAL(info) << "GET /api/v1/User/Medications - Request";
|
BOOST_LOG_TRIVIAL(info) << "GET /api/v1/User/Medications - Request";
|
||||||
|
|
||||||
@@ -50,7 +51,10 @@ public:
|
|||||||
throw session_exception(http::status::unauthorized, "Unauthorized");
|
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()};
|
const std::string auth_token = {
|
||||||
|
req[http::field::authorization].begin() + auth_prefix.size(),
|
||||||
|
req[http::field::authorization].end()
|
||||||
|
};
|
||||||
|
|
||||||
if (!auth_dao_->HasAuthorized(auth_token))
|
if (!auth_dao_->HasAuthorized(auth_token))
|
||||||
{
|
{
|
||||||
@@ -58,9 +62,109 @@ public:
|
|||||||
throw session_exception(http::status::unauthorized, "Unauthorized");
|
throw session_exception(http::status::unauthorized, "Unauthorized");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string_view user_login_ref = auth_dao_->GetLogin(auth_token);
|
||||||
|
const std::string user_uuid{user_login_ref.begin(), user_login_ref.end()};
|
||||||
|
|
||||||
|
static const std::string query = R"(
|
||||||
|
SELECT
|
||||||
|
uts.uuid,
|
||||||
|
uts.treatment_name,
|
||||||
|
uts.instructions,
|
||||||
|
ts.medication_uuid
|
||||||
|
FROM up_and_down.users u
|
||||||
|
JOIN up_and_down.user_treatment_schemes uts
|
||||||
|
ON uts.user_uuid = u.uuid
|
||||||
|
LEFT JOIN up_and_down.treatment_schemes ts
|
||||||
|
ON ts.user_treatment_schemes_uuid = uts.uuid
|
||||||
|
WHERE u.uuid = ?
|
||||||
|
ORDER BY uts.uuid
|
||||||
|
)";
|
||||||
|
|
||||||
|
mysqlx::SqlResult result = session_
|
||||||
|
.sql(query)
|
||||||
|
.bind(user_uuid)
|
||||||
|
.execute();
|
||||||
|
|
||||||
|
std::unordered_map<std::string, user_treatment_scheme_dto> scheme_map;
|
||||||
|
|
||||||
|
for (const mysqlx::Row& row : result)
|
||||||
|
{
|
||||||
|
const std::string scheme_uuid = row[0].get<std::string>();
|
||||||
|
|
||||||
|
if (scheme_map.find(scheme_uuid) == scheme_map.end())
|
||||||
|
{
|
||||||
|
user_treatment_scheme_dto dto;
|
||||||
|
dto.uuid = scheme_uuid;
|
||||||
|
dto.treatment_name = row[1].isNull() ? "" : row[1].get<std::string>();
|
||||||
|
dto.instructions = row[2].isNull() ? "" : row[2].get<std::string>();
|
||||||
|
|
||||||
|
scheme_map.emplace(scheme_uuid, std::move(dto));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!row[3].isNull())
|
||||||
|
{
|
||||||
|
scheme_map[scheme_uuid]
|
||||||
|
.medication_uuids
|
||||||
|
.push_back(row[3].get<std::string>());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<user_treatment_scheme_dto> schemes;
|
||||||
|
schemes.reserve(scheme_map.size());
|
||||||
|
|
||||||
|
for (auto& [_, dto] : scheme_map)
|
||||||
|
{
|
||||||
|
schemes.push_back(std::move(dto));
|
||||||
|
}
|
||||||
|
|
||||||
http::response<ResponseType> res{http::status::ok, req.version()};
|
http::response<ResponseType> res{http::status::ok, req.version()};
|
||||||
|
value response_body;
|
||||||
|
|
||||||
|
response_body.emplace_object();
|
||||||
|
response_body.as_object().emplace("user_treatment_schemes", toJSONArray(schemes));
|
||||||
|
|
||||||
|
res.body() = serialize(response_body);
|
||||||
|
res.set(http::field::content_type, "application/json");
|
||||||
|
res.content_length(res.body().size());
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
boost::json::object ToJSON(const user_treatment_scheme_dto& scheme)
|
||||||
|
{
|
||||||
|
boost::json::array medications_json;
|
||||||
|
medications_json.reserve(scheme.medication_uuids.size());
|
||||||
|
|
||||||
|
for (const auto& medication_uuid : scheme.medication_uuids) {
|
||||||
|
medications_json.emplace_back(medication_uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::json::object scheme_json;
|
||||||
|
scheme_json["uuid"] = scheme.uuid;
|
||||||
|
scheme_json["treatment_name"] = scheme.treatment_name;
|
||||||
|
scheme_json["instructions"] = scheme.instructions;
|
||||||
|
scheme_json["medications"] = std::move(medications_json);
|
||||||
|
|
||||||
|
return scheme_json;
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::json::array toJSONArray(const std::vector<user_treatment_scheme_dto>& schemes)
|
||||||
|
{
|
||||||
|
using namespace boost;
|
||||||
|
using namespace boost::json;
|
||||||
|
using namespace boost::beast;
|
||||||
|
using namespace std::string_literals;
|
||||||
|
using namespace std::string_view_literals;
|
||||||
|
|
||||||
|
json::array arr;
|
||||||
|
|
||||||
|
for (const auto& m : schemes)
|
||||||
|
{
|
||||||
|
arr.emplace_back(ToJSON(m));
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::move(arr);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -105,7 +105,7 @@ public:
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
routes_pathes_["api/v1/UserTreatmentSchemes"] = std::make_unique<RouteController>(
|
routes_pathes_["/api/v1/UserTreatmentSchemes"] = std::make_unique<RouteController>(
|
||||||
typename RouteController::HTTPMethodsToExecutors{
|
typename RouteController::HTTPMethodsToExecutors{
|
||||||
{
|
{
|
||||||
boost::beast::http::verb::get,
|
boost::beast::http::verb::get,
|
||||||
|
|||||||
Reference in New Issue
Block a user