diff --git a/build b/build index cabf43b..978b4e8 100644 --- a/build +++ b/build @@ -1 +1 @@ -24 \ No newline at end of file +26 \ No newline at end of file diff --git a/src/RESTAPI/RESTAPI_simulation_handler.cpp b/src/RESTAPI/RESTAPI_simulation_handler.cpp index 70257c5..e29ef16 100644 --- a/src/RESTAPI/RESTAPI_simulation_handler.cpp +++ b/src/RESTAPI/RESTAPI_simulation_handler.cpp @@ -3,22 +3,76 @@ // #include "RESTAPI_simulation_handler.h" +#include "RESTObjects/RESTAPI_OWLSobjects.h" +#include "StorageService.h" namespace OpenWifi { void RESTAPI_simulation_handler::DoPost() { + OWLSObjects::SimulationDetails D; + auto Raw = ParseStream(); + if(!D.from_json(Raw) || + D.name.empty() || + D.gateway.empty() || + D.macPrefix.size()!=6 || + D.key.empty() || + D.certificate.empty()) { + return BadRequest(RESTAPI::Errors::InvalidJSONDocument); + } + + D.id = MicroService::instance().CreateUUID(); + if(StorageService()->SimulationDB().CreateRecord(D)) { + OWLSObjects::SimulationDetails N; + StorageService()->SimulationDB().GetRecord("id", D.id, N); + Poco::JSON::Object Answer; + N.to_json(Answer); + return ReturnObject(Answer); + } + BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); } void RESTAPI_simulation_handler::DoGet() { + std::vector Sims; + StorageService()->SimulationDB().GetRecords(1,1000,Sims); + ReturnObject("list", Sims); } void RESTAPI_simulation_handler::DoDelete() { + std::string id; + if(!HasParameter("id",id) || id.empty()) { + return BadRequest(RESTAPI::Errors::MissingOrInvalidParameters); + } + + if(!StorageService()->SimulationDB().DeleteRecord("id",id)) + return NotFound(); + + return OK(); } void RESTAPI_simulation_handler::DoPut() { + OWLSObjects::SimulationDetails D; + auto Raw = ParseStream(); + if(!D.from_json(Raw) || + D.id.empty() || + D.name.empty() || + D.gateway.empty() || + D.macPrefix.size()!=6 || + D.key.empty() || + D.certificate.empty()) { + return BadRequest(RESTAPI::Errors::InvalidJSONDocument); + } + + if(StorageService()->SimulationDB().UpdateRecord("id", D.id, D)) { + OWLSObjects::SimulationDetails N; + StorageService()->SimulationDB().GetRecord("id", D.id, N); + Poco::JSON::Object Answer; + N.to_json(Answer); + return ReturnObject(Answer); + } + NotFound(); } } \ No newline at end of file diff --git a/src/StorageService.h b/src/StorageService.h index 02db727..6e2a8c9 100644 --- a/src/StorageService.h +++ b/src/StorageService.h @@ -25,7 +25,8 @@ namespace OpenWifi { return instance_; } - + SimulationDB & SimulationDB() { return *SimulationDB_; } + SimulationResultsDB & SimulationResultsDB() { return *SimulationResultsDB_; } int Start() override; void Stop() override;