Skip to content

Commit

Permalink
Rename Pastes to PastesController
Browse files Browse the repository at this point in the history
  • Loading branch information
eoan-ermine committed Aug 17, 2023
1 parent e458d41 commit a0a8ca8
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 61 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ add_subdirectory(third_party/userver)
# Common sources
add_library(${PROJECT_NAME}_objs OBJECT
src/models/paste.hpp
src/http_handlers/pastes.hpp
src/http_handlers/pastes.cpp
src/controllers/pastes.hpp
src/controllers/pastes.cpp
)
target_include_directories(${PROJECT_NAME}_objs PUBLIC src)
target_link_libraries(${PROJECT_NAME}_objs PUBLIC userver-core userver-postgresql)
Expand Down
58 changes: 12 additions & 46 deletions src/http_handlers/pastes.cpp → src/controllers/pastes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@

#include "models/paste.hpp"

#include <userver/components/component.hpp>
#include <userver/components/minimal_server_component_list.hpp>
#include <userver/server/handlers/http_handler_base.hpp>
#include <userver/utils/daemon_run.hpp>

#include <userver/storages/postgres/cluster.hpp>
#include <userver/storages/postgres/component.hpp>

using namespace userver;

namespace {
Expand Down Expand Up @@ -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<components::Postgres>("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");
Expand All @@ -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,
Expand All @@ -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()) {
Expand All @@ -122,7 +92,7 @@ std::string Pastes::GetPaste(std::string_view code,
return res.AsSingleRow<std::string>();
}

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,
Expand All @@ -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()) {
Expand All @@ -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<Pastes>();
}

} // namespace pastebin
} // namespace pastebin
38 changes: 38 additions & 0 deletions src/controllers/pastes.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once

#include <userver/components/component.hpp>
#include <userver/components/minimal_server_component_list.hpp>
#include <userver/server/handlers/http_handler_base.hpp>
#include <userver/utils/daemon_run.hpp>

#include <userver/storages/postgres/cluster.hpp>
#include <userver/storages/postgres/component.hpp>

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
9 changes: 0 additions & 9 deletions src/http_handlers/pastes.hpp

This file was deleted.

6 changes: 2 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <userver/clients/dns/component.hpp>
#include <userver/clients/http/component.hpp>
#include <userver/components/minimal_server_component_list.hpp>
#include <userver/server/handlers/ping.hpp>
#include <userver/server/handlers/tests_control.hpp>
#include <userver/storages/postgres/component.hpp>
#include <userver/testsuite/testsuite_support.hpp>
Expand All @@ -14,9 +13,8 @@ int main(int argc, char* argv[]) {
userver::components::MinimalServerComponentList()
.Append<userver::components::Postgres>("pastes-database")
.Append<userver::components::TestsuiteSupport>()
.Append<userver::clients::dns::Component>();

pastebin::AppendPastes(component_list);
.Append<userver::clients::dns::Component>()
.Append<pastebin::PastesController>();

return userver::utils::DaemonMain(argc, argv, component_list);
}

0 comments on commit a0a8ca8

Please sign in to comment.