From 2575fa628a689912dfca4292e23e4478365cff6f Mon Sep 17 00:00:00 2001 From: Adam Capparelli Date: Mon, 26 Feb 2024 15:02:27 -0500 Subject: [PATCH] Support linking of resources and configs through POST request. (#5) Signed-off-by: Adam Capparelli Co-authored-by: Adam Capparelli --- src/RESTAPI/RESTAPI_configurations_handler.cpp | 15 +++++++++++++++ src/RESTAPI/RESTAPI_db_helpers.h | 7 +++++++ 2 files changed, 22 insertions(+) diff --git a/src/RESTAPI/RESTAPI_configurations_handler.cpp b/src/RESTAPI/RESTAPI_configurations_handler.cpp index 81147d6..59600c8 100644 --- a/src/RESTAPI/RESTAPI_configurations_handler.cpp +++ b/src/RESTAPI/RESTAPI_configurations_handler.cpp @@ -138,7 +138,22 @@ namespace OpenWifi { return BadRequest(RESTAPI::Errors::ConfigBlockInvalid); } + Types::UUIDvec_t ToVariables; + if (RawObject->has("variables")) { + for (const auto &i : NewObject.variables) { + if (!i.empty() && !StorageService()->VariablesDB().Exists("id", i)) { + return BadRequest(RESTAPI::Errors::VariableMustExist); + } + } + for (const auto &i : NewObject.variables) + ToVariables.emplace_back(i); + + ToVariables = NewObject.variables; + } + if (DB_.CreateRecord(NewObject)) { + AddMembership(StorageService()->VariablesDB(), + &ProvObjects::VariableBlock::configurations, ToVariables, NewObject.info.id); MoveUsage(StorageService()->PolicyDB(), DB_, "", NewObject.managementPolicy, NewObject.info.id); AddMembership(StorageService()->VenueDB(), &ProvObjects::Venue::configurations, diff --git a/src/RESTAPI/RESTAPI_db_helpers.h b/src/RESTAPI/RESTAPI_db_helpers.h index 7db45e8..e7d9513 100644 --- a/src/RESTAPI/RESTAPI_db_helpers.h +++ b/src/RESTAPI/RESTAPI_db_helpers.h @@ -389,6 +389,13 @@ namespace OpenWifi { DB.ManipulateVectorMember(T, "id", Obj, Id, true); } + template + void AddMembership(db_type &DB, Member T, const Types::UUIDvec_t &Obj, const std::string &Id) { + for (const auto &i : Obj) { + AddMembership(DB, T, i, Id); + } + } + template void ManageMembership(db_type &DB, Member T, const std::string &From, const std::string &To, const std::string &Id) {