'Generic Commit'
Some checks failed
Build-Test-Publish / Build (push) Failing after 1m47s

This commit is contained in:
Your Name
2025-06-02 00:27:48 +12:00
parent 4259f6d960
commit d8e15f3f19
6 changed files with 141 additions and 0 deletions

65
src/update_handler.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include "update_handler.hpp"
#include <nlohmann/json.hpp>
#include <iostream>
namespace simple_object_storage {
UpdateHandler::UpdateHandler(Server& server) : server_(server) {}
void UpdateHandler::handle_update_object(const httplib::Request& req, httplib::Response& res) {
std::map<std::string, std::string> params;
// Validate authentication and rate limit (no required query params, just auth)
if (!server_.validate_write_request(req, res, {}, params)) {
return;
}
// Parse JSON body
nlohmann::json body;
try {
body = nlohmann::json::parse(req.body);
} catch (const nlohmann::json::parse_error& e) {
res.status = 400;
nlohmann::json response = {{"result", "error"}, {"error", "Invalid JSON body"}};
res.set_content(response.dump(), "application/json");
return;
}
// Check for required fields
if (!body.contains("hash") || !body.contains("metadata")) {
res.status = 400;
nlohmann::json response = {{"result", "error"}, {"error", "Missing 'hash' or 'metadata' field in request body"}};
res.set_content(response.dump(), "application/json");
return;
}
std::string hash = body["hash"].get<std::string>();
nlohmann::json new_metadata = body["metadata"];
// Get the object entry
dbEntry entry;
if (!server_.db_->get(hash, entry)) {
res.status = 404;
nlohmann::json response = {{"result", "error"}, {"error", "Object not found for hash: " + hash}};
res.set_content(response.dump(), "application/json");
return;
}
// Prepare updated entry (keep hash and labeltags, update metadata)
dbEntry updated_entry = entry;
updated_entry.metadata = new_metadata;
// Ensure labeltags and hash are preserved in metadata
updated_entry.metadata["labeltags"] = updated_entry.labeltags;
updated_entry.metadata["hash"] = updated_entry.hash;
if (!server_.db_->update_or_insert(updated_entry)) {
res.status = 500;
nlohmann::json response = {{"result", "error"}, {"error", "Failed to update metadata for hash: " + hash}};
res.set_content(response.dump(), "application/json");
return;
}
nlohmann::json response = {{"result", "success"}, {"hash", hash}};
res.set_content(response.dump(), "application/json");
}
} // namespace simple_object_storage