Initial RESTAPI

This commit is contained in:
stephb9959
2021-11-03 09:47:34 -07:00
parent 6407a24221
commit cdadd2add6
3 changed files with 57 additions and 2 deletions

2
build
View File

@@ -1 +1 @@
24
26

View File

@@ -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<OWLSObjects::SimulationDetails> 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();
}
}

View File

@@ -25,7 +25,8 @@ namespace OpenWifi {
return instance_;
}
SimulationDB & SimulationDB() { return *SimulationDB_; }
SimulationResultsDB & SimulationResultsDB() { return *SimulationResultsDB_; }
int Start() override;
void Stop() override;