This commit is contained in:
@@ -7,6 +7,61 @@ namespace lm {
|
||||
|
||||
void ApiServer::registerMediaRoutes(httplib::Server& svr)
|
||||
{
|
||||
svr.Get("/api/sites", [this](const httplib::Request&, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = nlohmann::json({{"sites", site_store_.list()}}).dump();
|
||||
});
|
||||
|
||||
svr.Post("/api/sites", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
nlohmann::json body;
|
||||
try
|
||||
{
|
||||
body = nlohmann::json::parse(req.body);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return HttpUtil::jsonError(res, 400, "invalid JSON");
|
||||
}
|
||||
std::string err;
|
||||
const auto created = site_store_.create(body, err);
|
||||
if (!created)
|
||||
return HttpUtil::jsonError(res, 400, err);
|
||||
res.status = 201;
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = created->dump();
|
||||
});
|
||||
|
||||
svr.Put(R"(/api/sites/([^/]+)$)", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
const std::string id = req.matches[1];
|
||||
nlohmann::json body;
|
||||
try
|
||||
{
|
||||
body = nlohmann::json::parse(req.body);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return HttpUtil::jsonError(res, 400, "invalid JSON");
|
||||
}
|
||||
std::string err;
|
||||
if (!site_store_.update(id, body, err))
|
||||
return HttpUtil::jsonError(res, 404, err);
|
||||
const auto updated = site_store_.find(id);
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = updated ? updated->dump() : nlohmann::json::object().dump();
|
||||
});
|
||||
|
||||
svr.Delete(R"(/api/sites/([^/]+)$)", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
const std::string id = req.matches[1];
|
||||
std::string err;
|
||||
if (!site_store_.remove(id, err))
|
||||
return HttpUtil::jsonError(res, 400, err);
|
||||
res.status = 204;
|
||||
});
|
||||
|
||||
svr.Get("/api/maps", [this](const httplib::Request&, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
@@ -69,6 +124,7 @@ void ApiServer::registerMediaRoutes(httplib::Server& svr)
|
||||
std::string err;
|
||||
if (!map_store_.remove(id, err))
|
||||
return HttpUtil::jsonError(res, 404, err);
|
||||
robot_runtime_.clearActiveMapIf(id);
|
||||
res.status = 204;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user