Advertisement
dimon-torchila

Untitled

May 8th, 2023
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include "set_couriers.hpp"
  2.  
  3. #include <fmt/format.h>
  4.  
  5. #include <userver/server/handlers/http_handler_base.hpp>
  6. #include <userver/storages/postgres/cluster.hpp>
  7. #include <userver/storages/postgres/component.hpp>
  8. #include <userver/utils/assert.hpp>
  9. #include <userver/formats/json/value.hpp>
  10.  
  11. namespace enrollment_couriers{
  12. namespace {
  13. class Couriers final : public userver::server::handlers::HttpHandlerBase{
  14.  public:
  15.   static constexpr std::string_view kName = "handler-set-couriers";
  16.  
  17.   Couriers(const userver::components::ComponentConfig& config,
  18.        const userver::components::ComponentContext& component_context)
  19.       : HttpHandlerBase(config, component_context),
  20.         pg_cluster_(
  21.             component_context
  22.                 .FindComponent<userver::components::Postgres>("postgres-db-1")
  23.                 .GetCluster()) {}
  24.  
  25.   std::string HandleRequestThrow(
  26.     const userver::server::http::HttpRequest& request,
  27.         userver::server::request::RequestContext&) const override {
  28.       const auto& courier_list = userver::formats::json::FromString(request.RequestBody());
  29.       int courier_count = 0;
  30.       if(courier_list.IsEmpty())
  31.         return "Nothing to add...";
  32.       else{
  33.         for(const auto& value : courier_list){
  34.           ++courier_count;
  35.           const auto& regions = value["regions"].As<std::vector<int>>();
  36.            std::string type = value["courier_type"].As<std::string>();
  37.            std::string hours = value["working_hours"].As<std::string>();
  38.            std::string hour1(hours.begin(), hours.begin() + hours.find('-')), hour2(hours.begin() + hours.find('-') + 1, hours.end());
  39.            std::vector<std::string> working_hours = {hour1, hour2};
  40.            auto result = pg_cluster_->Execute(
  41.                userver::storages::postgres::ClusterHostType::kMaster,
  42.                "INSERT INTO hello_schema.couriers(type, regions, working_hours)"
  43.                "VALUES ($1, $2, $3)", type, regions, working_hours);
  44.         }
  45.         return SayAddCourier(courier_count);
  46.       }
  47.   }
  48.   userver::storages::postgres::ClusterPtr pg_cluster_;
  49.  
  50. };
  51. }
  52.  
  53.   std::string SayAddCourier(int value){
  54.     return fmt::format("Added {} couriers", value);
  55.   }
  56.  
  57.   void AppendCouriers(userver::components::ComponentList& component_list) {
  58.     component_list.Append<Couriers>();
  59.   }
  60. }
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68. //error
  69. CMake Error: Cannot open file for write: /workspaces/11114-dimon-torchila-105/build_debug/tests/CMakeFiles/start-enrollment_template.dir/depend.make.tmp
  70. CMake Error: : System Error: Permission denied
  71. make[4]: *** [tests/CMakeFiles/start-enrollment_template.dir/build.make:94: tests/CMakeFiles/start-enrollment_template.dir/depend] Error 2
  72. make[4]: Leaving directory '/workspaces/11114-dimon-torchila-105/build_debug'
  73. make[3]: *** [CMakeFiles/Makefile2:1496: tests/CMakeFiles/start-enrollment_template.dir/all] Error 2
  74. make[3]: Leaving directory '/workspaces/11114-dimon-torchila-105/build_debug'
  75. make[2]: *** [CMakeFiles/Makefile2:1504: tests/CMakeFiles/start-enrollment_template.dir/rule] Error 2
  76. make[2]: Leaving directory '/workspaces/11114-dimon-torchila-105/build_debug'
  77. make[1]: *** [Makefile:584: start-enrollment_template] Error 2
  78. make[1]: Leaving directory '/workspaces/11114-dimon-torchila-105/build_debug'
  79. make: *** [Makefile:47: service-start-debug] Error 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement