57 lines
1.4 KiB
C++
57 lines
1.4 KiB
C++
#include <fstream>
|
|
|
|
#include "../routes.h"
|
|
#include "GetMapByIdHandler.h"
|
|
|
|
namespace http_server
|
|
{
|
|
StringResponse GetMapByIdHandler(const StringRequest& req)
|
|
{
|
|
const auto route = req.target();
|
|
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_MapsRoute.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;
|
|
}
|
|
}
|