From 55cb32c40800b5aba2ed741f48986dd775910d3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BD=D1=82=D0=BE=D0=BD?= Date: Thu, 21 Mar 2024 19:30:48 +0300 Subject: [PATCH] add third route --- main.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/main.cpp b/main.cpp index 48142f7..e3aa53e 100644 --- a/main.cpp +++ b/main.cpp @@ -68,7 +68,50 @@ StringResponse HandleRequest(StringRequest&& req) route.begin(), route.begin() + k_MapsPattern.size())) { + ifstream stream("./config.json"s); + string config; + string buf; + while (std::getline(stream, buf)) + { + config += buf; + } + + string_view config_ref {config.begin(), config.end()}; + auto parsed_config = json::parse(config_ref); + string_view map_id {route.begin() + k_MapsPattern.size() + 1, route.end()}; + StringResponse res(http::status::ok, 1); + res.set(http::field::content_type, content_type::k_JSON); + + const auto& maps = parsed_config.as_object().at("maps"sv).as_array(); + + const auto finded = find_if(maps.begin(), maps.end(), [&map_id](auto& elem) -> bool + { + const auto& value = elem.as_object().at("id"sv).as_string(); + + return value == map_id; + }); + + if (finded == maps.end()) + { + StringResponse res(http::status::bad_request, 1); + res.set(http::field::content_type, content_type::k_JSON); + string body = "{\n" + " \"code\": \"mapNotFound\",\n" + " \"message\": \"Map not found\"\n" + "}"s; + res.body() = body; + res.content_length(body.size()); + res.keep_alive(true); + + return res; + } + + res.body() = json::serialize(*finded); + res.content_length(res.body().size()); + res.keep_alive(true); + + return res; } else {