Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "set_couriers.hpp"
- #include <fmt/format.h>
- #include <userver/server/handlers/http_handler_base.hpp>
- #include <userver/storages/postgres/cluster.hpp>
- #include <userver/storages/postgres/component.hpp>
- #include <userver/utils/assert.hpp>
- #include <userver/formats/json/value.hpp>
- namespace enrollment_couriers{
- namespace {
- class Couriers final : public userver::server::handlers::HttpHandlerBase{
- public:
- static constexpr std::string_view kName = "handler-set-couriers";
- Couriers(const userver::components::ComponentConfig& config,
- const userver::components::ComponentContext& component_context)
- : HttpHandlerBase(config, component_context),
- pg_cluster_(
- component_context
- .FindComponent<userver::components::Postgres>("postgres-db-1")
- .GetCluster()) {}
- std::string HandleRequestThrow(
- const userver::server::http::HttpRequest& request,
- userver::server::request::RequestContext&) const override {
- const auto& courier_list = userver::formats::json::FromString(request.RequestBody());
- int courier_count = 0;
- if(courier_list.IsEmpty())
- return "Nothing to add...";
- else{
- for(const auto& value : courier_list){
- ++courier_count;
- const auto& regions = value["regions"].As<std::vector<int>>();
- std::string type = value["courier_type"].As<std::string>();
- std::string hours = value["working_hours"].As<std::string>();
- std::string hour1(hours.begin(), hours.begin() + hours.find('-')), hour2(hours.begin() + hours.find('-') + 1, hours.end());
- std::vector<std::string> working_hours = {hour1, hour2};
- auto result = pg_cluster_->Execute(
- userver::storages::postgres::ClusterHostType::kMaster,
- "INSERT INTO hello_schema.couriers(type, regions, working_hours)"
- "VALUES ($1, $2, $3)", type, regions, working_hours);
- }
- return SayAddCourier(courier_count);
- }
- }
- userver::storages::postgres::ClusterPtr pg_cluster_;
- };
- }
- std::string SayAddCourier(int value){
- return fmt::format("Added {} couriers", value);
- }
- void AppendCouriers(userver::components::ComponentList& component_list) {
- component_list.Append<Couriers>();
- }
- }
- //error
- CMake Error: Cannot open file for write: /workspaces/11114-dimon-torchila-105/build_debug/tests/CMakeFiles/start-enrollment_template.dir/depend.make.tmp
- CMake Error: : System Error: Permission denied
- make[4]: *** [tests/CMakeFiles/start-enrollment_template.dir/build.make:94: tests/CMakeFiles/start-enrollment_template.dir/depend] Error 2
- make[4]: Leaving directory '/workspaces/11114-dimon-torchila-105/build_debug'
- make[3]: *** [CMakeFiles/Makefile2:1496: tests/CMakeFiles/start-enrollment_template.dir/all] Error 2
- make[3]: Leaving directory '/workspaces/11114-dimon-torchila-105/build_debug'
- make[2]: *** [CMakeFiles/Makefile2:1504: tests/CMakeFiles/start-enrollment_template.dir/rule] Error 2
- make[2]: Leaving directory '/workspaces/11114-dimon-torchila-105/build_debug'
- make[1]: *** [Makefile:584: start-enrollment_template] Error 2
- make[1]: Leaving directory '/workspaces/11114-dimon-torchila-105/build_debug'
- make: *** [Makefile:47: service-start-debug] Error 2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement