From 37ec04f9eed4879e77526e18686ac0c66ca21f75 Mon Sep 17 00:00:00 2001 From: Danil Sidoruk Date: Thu, 17 Aug 2023 19:31:49 +0300 Subject: [PATCH] Rename Pastes to PastesController --- src/{http_handlers => controllers}/pastes.cpp | 58 ++++--------------- src/controllers/pastes.hpp | 38 ++++++++++++ src/http_handlers/pastes.hpp | 9 --- src/main.cpp | 6 +- 4 files changed, 52 insertions(+), 59 deletions(-) rename src/{http_handlers => controllers}/pastes.cpp (65%) create mode 100644 src/controllers/pastes.hpp delete mode 100644 src/http_handlers/pastes.hpp diff --git a/src/http_handlers/pastes.cpp b/src/controllers/pastes.cpp similarity index 65% rename from src/http_handlers/pastes.cpp rename to src/controllers/pastes.cpp index 131ca6f..95c164f 100644 --- a/src/http_handlers/pastes.cpp +++ b/src/controllers/pastes.cpp @@ -2,14 +2,6 @@ #include "models/paste.hpp" -#include -#include -#include -#include - -#include -#include - using namespace userver; namespace { @@ -48,37 +40,15 @@ const auto authentication_error = server::handlers::Unauthorized{}; namespace pastebin { -class Pastes final : public server::handlers::HttpHandlerBase { - public: - static constexpr std::string_view kName = "handler-pastes"; - - Pastes(const components::ComponentConfig& config, - const components::ComponentContext& context); - - std::string HandleRequestThrow( - const server::http::HttpRequest& request, - server::request::RequestContext&) const override; - - private: - std::string CreatePaste(const server::http::HttpRequest&) const; - std::string GetPaste(std::string_view code, - const server::http::HttpRequest&) const; - std::string UpdatePaste(std::string_view token, - const server::http::HttpRequest&) const; - std::string DeletePaste(std::string_view token, - const server::http::HttpRequest&) const; - - storages::postgres::ClusterPtr pg_cluster_; -}; - -Pastes::Pastes(const components::ComponentConfig& config, - const components::ComponentContext& context) +PastesController::PastesController(const components::ComponentConfig& config, + const components::ComponentContext& context) : HttpHandlerBase(config, context), pg_cluster_(context.FindComponent("pastes-database") .GetCluster()) {} -std::string Pastes::HandleRequestThrow(const server::http::HttpRequest& request, - server::request::RequestContext&) const { +std::string PastesController::HandleRequestThrow( + const server::http::HttpRequest& request, + server::request::RequestContext&) const { auto method = request.GetMethod(); if (method == server::http::HttpMethod::kGet) { auto code = GetArgOrThrow(request, "code"); @@ -97,7 +67,7 @@ std::string Pastes::HandleRequestThrow(const server::http::HttpRequest& request, } } -std::string Pastes::CreatePaste( +std::string PastesController::CreatePaste( const server::http::HttpRequest& request) const { const auto& content = request.RequestBody(); auto res = pg_cluster_->Execute(storages::postgres::ClusterHostType::kMaster, @@ -112,8 +82,8 @@ std::string Pastes::CreatePaste( return ToString(response); } -std::string Pastes::GetPaste(std::string_view code, - const server::http::HttpRequest&) const { +std::string PastesController::GetPaste(std::string_view code, + const server::http::HttpRequest&) const { storages::postgres::ResultSet res = pg_cluster_->Execute( storages::postgres::ClusterHostType::kSlave, kSelectPasteContent, code); if (res.IsEmpty()) { @@ -122,7 +92,7 @@ std::string Pastes::GetPaste(std::string_view code, return res.AsSingleRow(); } -std::string Pastes::UpdatePaste( +std::string PastesController::UpdatePaste( std::string_view token, const server::http::HttpRequest& request) const { const auto& content = request.RequestBody(); auto res = pg_cluster_->Execute(storages::postgres::ClusterHostType::kMaster, @@ -134,8 +104,8 @@ std::string Pastes::UpdatePaste( throw authentication_error; } -std::string Pastes::DeletePaste(std::string_view token, - const server::http::HttpRequest&) const { +std::string PastesController::DeletePaste( + std::string_view token, const server::http::HttpRequest&) const { auto res = pg_cluster_->Execute(storages::postgres::ClusterHostType::kMaster, kDeletePaste, token); if (res.RowsAffected()) { @@ -145,8 +115,4 @@ std::string Pastes::DeletePaste(std::string_view token, throw authentication_error; } -void AppendPastes(userver::components::ComponentList& component_list) { - component_list.Append(); -} - -} // namespace pastebin +} // namespace pastebin \ No newline at end of file diff --git a/src/controllers/pastes.hpp b/src/controllers/pastes.hpp new file mode 100644 index 0000000..fc56923 --- /dev/null +++ b/src/controllers/pastes.hpp @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include +#include + +#include +#include + +namespace pastebin { + +using namespace userver; + +class PastesController final : public server::handlers::HttpHandlerBase { + public: + static constexpr std::string_view kName = "handler-pastes"; + + PastesController(const components::ComponentConfig& config, + const components::ComponentContext& context); + + std::string HandleRequestThrow( + const server::http::HttpRequest& request, + server::request::RequestContext&) const override; + + private: + std::string CreatePaste(const server::http::HttpRequest&) const; + std::string GetPaste(std::string_view code, + const server::http::HttpRequest&) const; + std::string UpdatePaste(std::string_view token, + const server::http::HttpRequest&) const; + std::string DeletePaste(std::string_view token, + const server::http::HttpRequest&) const; + + storages::postgres::ClusterPtr pg_cluster_; +}; + +} // namespace pastebin \ No newline at end of file diff --git a/src/http_handlers/pastes.hpp b/src/http_handlers/pastes.hpp deleted file mode 100644 index f2ce3e1..0000000 --- a/src/http_handlers/pastes.hpp +++ /dev/null @@ -1,9 +0,0 @@ -#pragma once - -#include - -namespace pastebin { - -void AppendPastes(userver::components::ComponentList& component_list); - -} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 995832e..d8dd268 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -14,9 +13,8 @@ int main(int argc, char* argv[]) { userver::components::MinimalServerComponentList() .Append("pastes-database") .Append() - .Append(); - - pastebin::AppendPastes(component_list); + .Append() + .Append(); return userver::utils::DaemonMain(argc, argv, component_list); }