generated from Sithas/conan_template
Delete Diary
This commit is contained in:
@@ -23,6 +23,11 @@ public:
|
||||
const diary_dto& dto
|
||||
) const = 0;
|
||||
|
||||
virtual void DeleteDiary(
|
||||
const std::string& user_uuid,
|
||||
const std::string& diary_uuid
|
||||
) const = 0;
|
||||
|
||||
virtual ~IDiariesDAO() = default;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -149,4 +149,22 @@ void MySqlDiariesDAO::UpdateDiary(
|
||||
throw session_exception(boost::beast::http::status::not_found, "Diary not found");
|
||||
}
|
||||
}
|
||||
|
||||
void MySqlDiariesDAO::DeleteDiary(
|
||||
const std::string& user_uuid,
|
||||
const std::string& diary_uuid) const
|
||||
{
|
||||
auto stmt = session_.sql(R"(
|
||||
DELETE FROM `up_and_down`.`diaries`
|
||||
WHERE uuid = ? AND user_uuid = ?
|
||||
)");
|
||||
|
||||
stmt.bind(diary_uuid, user_uuid);
|
||||
|
||||
auto res = stmt.execute();
|
||||
|
||||
if (res.getAffectedItemsCount() == 0) {
|
||||
throw session_exception(boost::beast::http::status::not_found, "Diary not found");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,5 +27,10 @@ public:
|
||||
const std::string& diary_uuid,
|
||||
const diary_dto& dto
|
||||
) const override;
|
||||
|
||||
void DeleteDiary(
|
||||
const std::string& user_uuid,
|
||||
const std::string& diary_uuid
|
||||
) const override;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
#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/IDiariesDAO.h"
|
||||
#include "../exceptions/session_exception.h"
|
||||
|
||||
namespace uad
|
||||
{
|
||||
template <class Body, class Allocator, class ResponseType>
|
||||
class DeleteDiaryExecutor
|
||||
{
|
||||
mysqlx::Session& session_;
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao_;
|
||||
const std::shared_ptr<IDiariesDAO>& diaries_dao_;
|
||||
|
||||
public:
|
||||
DeleteDiaryExecutor(
|
||||
mysqlx::Session& session,
|
||||
const std::shared_ptr<IAuthDAO>& auth_dao,
|
||||
const std::shared_ptr<IDiariesDAO>& diaries_dao
|
||||
) : session_(session), auth_dao_(auth_dao), diaries_dao_(diaries_dao)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] boost::beast::http::response<ResponseType> operator ()(
|
||||
boost::beast::http::request<Body, boost::beast::http::basic_fields<Allocator>>&& req,
|
||||
std::string diary_uuid
|
||||
)
|
||||
{
|
||||
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 =
|
||||
"DELETE /api/v1/Diary - Response 401: Unauthorized"s;
|
||||
|
||||
BOOST_LOG_TRIVIAL(info) << "DELETE /api/v1/Diary - 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");
|
||||
}
|
||||
|
||||
std::string_view user_uuid_ref = auth_dao_->GetUUID(auth_token);
|
||||
const std::string user_uuid{user_uuid_ref.begin(), user_uuid_ref.end()};
|
||||
|
||||
diaries_dao_->DeleteDiary(user_uuid, diary_uuid);
|
||||
|
||||
http::response<ResponseType> res{http::status::no_content, req.version()};
|
||||
res.keep_alive(req.keep_alive());
|
||||
|
||||
BOOST_LOG_TRIVIAL(info) << "DELETE /api/v1/Diary - Response 204: Diary deleted";
|
||||
|
||||
return res;
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -17,6 +17,7 @@
|
||||
#include "GetDiariesExecutor.h"
|
||||
#include "PostDiaryExecutor.h"
|
||||
#include "PutDiaryExecutor.h"
|
||||
#include "DeleteDiaryExecutor.h"
|
||||
#include "../DAO/IUserDAO.h"
|
||||
#include "../DAO/IAuthDAO.h"
|
||||
#include "../DAO/IDiariesDAO.h"
|
||||
@@ -202,15 +203,31 @@ public:
|
||||
parts[0] == "api" &&
|
||||
parts[2] == "Diaries")
|
||||
{
|
||||
std::string uuid = std::string(parts[3]);
|
||||
|
||||
if (req.method() == boost::beast::http::verb::put)
|
||||
try
|
||||
{
|
||||
return send(PutDiaryExecutor<Body, Allocator, ResponseType>(
|
||||
session_,
|
||||
auth_dao_,
|
||||
diaries_dao_
|
||||
)(std::move(req), uuid));
|
||||
std::string uuid = std::string(parts[3]);
|
||||
|
||||
if (req.method() == boost::beast::http::verb::put)
|
||||
{
|
||||
return send(PutDiaryExecutor<Body, Allocator, ResponseType>(
|
||||
session_,
|
||||
auth_dao_,
|
||||
diaries_dao_
|
||||
)(std::move(req), uuid));
|
||||
}
|
||||
|
||||
if (req.method() == boost::beast::http::verb::delete_)
|
||||
{
|
||||
return send(DeleteDiaryExecutor<Body, Allocator, ResponseType>(
|
||||
session_,
|
||||
auth_dao_,
|
||||
diaries_dao_
|
||||
)(std::move(req), uuid));
|
||||
}
|
||||
}
|
||||
catch (const session_exception& e)
|
||||
{
|
||||
return send(SendSessionExceptionError(std::move(req), e));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user