add third route

This commit is contained in:
Антон 2024-03-21 19:30:48 +03:00
parent 3ead75baae
commit 55cb32c408
1 changed files with 43 additions and 0 deletions

View File

@ -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
{