Add phần create map by upload
Some checks failed
Test / test (push) Has been cancelled

This commit is contained in:
2026-06-19 11:52:21 +07:00
parent 098e1b2b69
commit a6cf06d7eb
27 changed files with 4960 additions and 129 deletions

View File

@@ -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;
});