diff --git a/src/handlers/profiles/prev_profiles_follow.cpp b/src/handlers/profiles/prev_profiles_follow.cpp deleted file mode 100644 index f8fc011..0000000 --- a/src/handlers/profiles/prev_profiles_follow.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include "profiles_follow.hpp" -#include "db/sql.hpp" -#include "dto/profile.hpp" -#include "models/profile.hpp" -#include "utils/make_error.hpp" - -#include "userver/formats/yaml/value_builder.hpp" -#include "userver/server/handlers/http_handler_base.hpp" -#include "userver/storages/postgres/cluster.hpp" -#include "userver/storages/postgres/component.hpp" -#include - -using namespace userver::formats; -using namespace userver::server::http; -using namespace userver::server::request; -using namespace userver::storages::postgres; - -namespace real_medium::handlers::profiles::post { - - Handler::Handler(const userver::components::ComponentConfig& config, - const userver::components::ComponentContext& component_context) - : HttpHandlerJsonBase(config, component_context), - pg_cluster_(component_context - .FindComponent( - "realmedium-database") - .GetCluster()) {} - - userver::formats::json::Value Handler::HandleRequestJsonThrow( - const userver::server::http::HttpRequest& request, - const userver::formats::json::Value&, - userver::server::request::RequestContext& context) const { - auto request_json = userver::formats::json::FromString(request.RequestBody()); - auto&& request_data = request_json.As(); - - if (!request_data.username.has_value()) { - auto& response = request.GetHttpResponse(); - response.SetStatus(userver::server::http::HttpStatus::kNotFound); - return utils::error::MakeError("username", "It is null."); - } - - if (!request_data.id.has_value()) { - auto& response = request.GetHttpResponse(); - response.SetStatus(userver::server::http::HttpStatus::kNotFound); - return utils::error::MakeError("id", "It is null."); - } - - auto user_id = request_data.id.value(); - const auto& username = request_data.username.value(); - const auto res_find_id_username = - pg_cluster_->Execute(userver::storages::postgres::ClusterHostType::kSlave, - sql::kFindUserIDByUsername.data(), username); - if (res_find_id_username.IsEmpty()) { - auto& response = request.GetHttpResponse(); - response.SetStatus(userver::server::http::HttpStatus::kNotFound); - return utils::error::MakeError("username", - "There is no user with this nickname."); - } - - std::string username_id = res_find_id_username.AsSingleRow(); - - if (username_id == user_id) { - auto& response = request.GetHttpResponse(); - response.SetStatus(userver::server::http::HttpStatus::kBadRequest); - return utils::error::MakeError("username", - "Username is author of the request."); - } - - const auto res_following = - pg_cluster_->Execute(userver::storages::postgres::ClusterHostType::kSlave, - sql::KFollowingUser.data(), username_id, user_id); - - const auto profile = res_following.AsSingleRow( - userver::storages::postgres::kRowTag); - - if (!profile.following) { - auto& response = request.GetHttpResponse(); - response.SetStatus(userver::server::http::HttpStatus::kBadRequest); - return utils::error::MakeError("user_id", "has already followed"); - } - - userver::formats::json::ValueBuilder builder; - builder["profile"] = profile; - - return builder.ExtractValue(); - } -} // namespace real_medium::handlers::profiles::post diff --git a/src/handlers/profiles/prev_profiles_follow.hpp b/src/handlers/profiles/prev_profiles_follow.hpp deleted file mode 100644 index c6190ef..0000000 --- a/src/handlers/profiles/prev_profiles_follow.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#pragma once - -#include "userver/components/component_config.hpp" -#include "userver/components/component_context.hpp" -#include "userver/formats/json/value.hpp" -#include "userver/server/handlers/http_handler_base.hpp" -#include "userver/server/handlers/http_handler_json_base.hpp" -#include "userver/storages/postgres/postgres_fwd.hpp" - -namespace real_medium::handlers::profiles::post { - - class Handler final : public userver::server::handlers::HttpHandlerJsonBase { - public: - static constexpr std::string_view kName = "handler-profile-follow"; - - Handler(const userver::components::ComponentConfig& config, - const userver::components::ComponentContext& context); - - userver::formats::json::Value HandleRequestJsonThrow( - const userver::server::http::HttpRequest& request, - const userver::formats::json::Value&, - userver::server::request::RequestContext& context) const override final; - - private: - const userver::storages::postgres::ClusterPtr pg_cluster_; - }; - -} // namespace real_medium::handlers::profiles::post