Rewrite commands to nlohmann-json

This commit is contained in:
Lorow
2025-10-09 23:12:03 +02:00
parent cf9eecc822
commit 44b5fe157a
24 changed files with 441 additions and 657 deletions

View File

@@ -1,27 +1,5 @@
#include "config_commands.hpp"
std::optional<ResetConfigPayload> parseResetConfigCommandPayload(std::string_view jsonPayload)
{
ResetConfigPayload payload;
cJSON *parsedJson = cJSON_Parse(jsonPayload.data());
if (parsedJson == nullptr)
return std::nullopt;
cJSON *section = cJSON_GetObjectItem(parsedJson, "section");
if (section == nullptr)
{
cJSON_Delete(parsedJson);
return std::nullopt;
}
payload.section = std::string(section->valuestring);
cJSON_Delete(parsedJson);
return payload;
}
CommandResult saveConfigCommand(std::shared_ptr<DependencyRegistry> registry)
{
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
@@ -38,29 +16,27 @@ CommandResult getConfigCommand(std::shared_ptr<DependencyRegistry> registry)
return CommandResult::getSuccessResult(configRepresentation);
}
CommandResult resetConfigCommand(std::shared_ptr<DependencyRegistry> registry, std::string_view jsonPayload)
CommandResult resetConfigCommand(std::shared_ptr<DependencyRegistry> registry, const nlohmann::json &json)
{
std::array<std::string, 4> supported_sections = {
"all",
};
auto payload = parseResetConfigCommandPayload(jsonPayload);
if (!payload.has_value())
if (!json.contains("section"))
{
return CommandResult::getErrorResult("Invalid payload or missing section");
return CommandResult::getErrorResult("Invalid payload - missing section");
}
auto sectionPayload = payload.value();
auto section = json["section"].get<std::string>();
if (std::find(supported_sections.begin(), supported_sections.end(), sectionPayload.section) == supported_sections.end())
if (std::find(supported_sections.begin(), supported_sections.end(), section) == supported_sections.end())
{
return CommandResult::getErrorResult("Selected section is unsupported");
}
// we cannot match on string, and making a map would be overkill right now, sooo
// todo, add more granual control for other sections, like only reset camera, or only reset wifi
if (sectionPayload.section == "all")
// todo, add more granular control for other sections, like only reset camera, or only reset wifi
if (section == "all")
{
std::shared_ptr<ProjectConfig> projectConfig = registry->resolve<ProjectConfig>(DependencyType::project_config);
projectConfig->reset();