generated from Sithas/conan_template
Delete Diary
This commit is contained in:
@@ -67,6 +67,7 @@ add_executable(App ./src/main.cpp
|
|||||||
src/endpoints_handlers/GetDiariesExecutor.h
|
src/endpoints_handlers/GetDiariesExecutor.h
|
||||||
src/endpoints_handlers/PostDiaryExecutor.h
|
src/endpoints_handlers/PostDiaryExecutor.h
|
||||||
src/endpoints_handlers/PutDiaryExecutor.h
|
src/endpoints_handlers/PutDiaryExecutor.h
|
||||||
|
src/endpoints_handlers/DeleteDiaryExecutor.h
|
||||||
src/DAO/IDiariesDAO.h
|
src/DAO/IDiariesDAO.h
|
||||||
src/dtos/diary_dto.h
|
src/dtos/diary_dto.h
|
||||||
src/DAO/MySQLDiariesDao.cpp
|
src/DAO/MySQLDiariesDao.cpp
|
||||||
|
|||||||
@@ -23,6 +23,11 @@ public:
|
|||||||
const diary_dto& dto
|
const diary_dto& dto
|
||||||
) const = 0;
|
) const = 0;
|
||||||
|
|
||||||
|
virtual void DeleteDiary(
|
||||||
|
const std::string& user_uuid,
|
||||||
|
const std::string& diary_uuid
|
||||||
|
) const = 0;
|
||||||
|
|
||||||
virtual ~IDiariesDAO() = default;
|
virtual ~IDiariesDAO() = default;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -149,4 +149,22 @@ void MySqlDiariesDAO::UpdateDiary(
|
|||||||
throw session_exception(boost::beast::http::status::not_found, "Diary not found");
|
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 std::string& diary_uuid,
|
||||||
const diary_dto& dto
|
const diary_dto& dto
|
||||||
) const override;
|
) 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 "GetDiariesExecutor.h"
|
||||||
#include "PostDiaryExecutor.h"
|
#include "PostDiaryExecutor.h"
|
||||||
#include "PutDiaryExecutor.h"
|
#include "PutDiaryExecutor.h"
|
||||||
|
#include "DeleteDiaryExecutor.h"
|
||||||
#include "../DAO/IUserDAO.h"
|
#include "../DAO/IUserDAO.h"
|
||||||
#include "../DAO/IAuthDAO.h"
|
#include "../DAO/IAuthDAO.h"
|
||||||
#include "../DAO/IDiariesDAO.h"
|
#include "../DAO/IDiariesDAO.h"
|
||||||
@@ -201,6 +202,8 @@ public:
|
|||||||
if (parts.size() == 4 &&
|
if (parts.size() == 4 &&
|
||||||
parts[0] == "api" &&
|
parts[0] == "api" &&
|
||||||
parts[2] == "Diaries")
|
parts[2] == "Diaries")
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
std::string uuid = std::string(parts[3]);
|
std::string uuid = std::string(parts[3]);
|
||||||
|
|
||||||
@@ -212,6 +215,20 @@ public:
|
|||||||
diaries_dao_
|
diaries_dao_
|
||||||
)(std::move(req), uuid));
|
)(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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (req.method() != boost::beast::http::verb::get &&
|
if (req.method() != boost::beast::http::verb::get &&
|
||||||
|
|||||||
Reference in New Issue
Block a user