This commit is contained in:
stephb9959
2021-08-18 08:42:10 -07:00
parent 2d81f18bf0
commit 97f66fbc37
10 changed files with 138 additions and 29 deletions

View File

@@ -77,7 +77,7 @@ add_executable(owprov
src/storage_location.cpp src/storage_location.h
src/storage_contact.cpp src/storage_contact.h
src/RESTAPI_entity_handler.cpp src/RESTAPI_entity_handler.h
src/storage_inventory.cpp src/storage_inventory.h src/RESTAPI_contact_handler.cpp src/RESTAPI_contact_handler.h src/RESTAPI_location_handler.cpp src/RESTAPI_location_handler.h src/RESTAPI_venue_handler.cpp src/RESTAPI_venue_handler.h src/RESTAPI_inventory_handler.cpp src/RESTAPI_inventory_handler.h src/RESTAPI_managementPolicy_handler.cpp src/RESTAPI_managementPolicy_handler.h src/RESTAPI_inventory_list_handler.cpp src/RESTAPI_inventory_list_handler.h)
src/storage_inventory.cpp src/storage_inventory.h src/RESTAPI_contact_handler.cpp src/RESTAPI_contact_handler.h src/RESTAPI_location_handler.cpp src/RESTAPI_location_handler.h src/RESTAPI_venue_handler.cpp src/RESTAPI_venue_handler.h src/RESTAPI_inventory_handler.cpp src/RESTAPI_inventory_handler.h src/RESTAPI_managementPolicy_handler.cpp src/RESTAPI_managementPolicy_handler.h src/RESTAPI_inventory_list_handler.cpp src/RESTAPI_inventory_list_handler.h src/dbhelpers.h)
target_link_libraries(owprov PUBLIC
${Poco_LIBRARIES} ${MySQL_LIBRARIES}

View File

@@ -264,7 +264,11 @@ components:
type: string
country:
type: string
telephones:
phones:
type: array
items:
type: string
mobiles:
type: array
items:
type: string
@@ -301,11 +305,11 @@ components:
type: string
visual:
type: string
mobiles:
phones:
type: array
items:
type: string
phones:
mobiles:
type: array
items:
type: string

View File

@@ -173,7 +173,7 @@ namespace OpenWifi::ProvObjects {
uCentral::RESTAPI_utils::field_to_json( Obj,"state",state);
uCentral::RESTAPI_utils::field_to_json( Obj,"postal",postal);
uCentral::RESTAPI_utils::field_to_json( Obj,"country",country);
uCentral::RESTAPI_utils::field_to_json( Obj,"telephones",telephones);
uCentral::RESTAPI_utils::field_to_json( Obj,"phones",phones);
uCentral::RESTAPI_utils::field_to_json( Obj,"contact",contact);
}
@@ -189,8 +189,9 @@ namespace OpenWifi::ProvObjects {
uCentral::RESTAPI_utils::field_from_json( Obj,"state",state);
uCentral::RESTAPI_utils::field_from_json( Obj,"postal",postal);
uCentral::RESTAPI_utils::field_from_json( Obj,"country",country);
uCentral::RESTAPI_utils::field_from_json( Obj,"telephones",telephones);
uCentral::RESTAPI_utils::field_from_json( Obj,"phones",phones);
uCentral::RESTAPI_utils::field_from_json( Obj,"contact",contact);
uCentral::RESTAPI_utils::field_from_json( Obj,"mobiles",mobiles);
return true;
} catch (...) {

View File

@@ -139,8 +139,9 @@ namespace OpenWifi::ProvObjects {
std::string state;
std::string postal;
std::string country;
uCentral::Types::StringVec telephones;
uCentral::Types::StringVec phones;
uCentral::Types::UUID_t contact;
uCentral::Types::StringVec mobiles;
void to_json(Poco::JSON::Object &Obj) const;
bool from_json(const Poco::JSON::Object::Ptr &Obj);

View File

@@ -88,32 +88,22 @@ namespace OpenWifi{
void RESTAPI_entity_handler::DoPost(Poco::Net::HTTPServerRequest &Request,
Poco::Net::HTTPServerResponse &Response) {
try {
std::cout << __LINE__ << std::endl;
std::string UUID = GetBinding("uuid", "");
std::cout << __LINE__ << std::endl;
if(UUID.empty()) {
BadRequest(Request, Response, "Missing UUID");
std::cout << __LINE__ << std::endl;
return;
}
std::cout << __LINE__ << std::endl;
if(!Storage()->EntityDB().RootExists() && UUID != EntityDB::RootUUID()) {
BadRequest(Request, Response, "Root entity must be created first.");
std::cout << __LINE__ << std::endl;
return;
}
std::cout << __LINE__ << std::endl;
Poco::JSON::Parser IncomingParser;
std::cout << __LINE__ << std::endl;
Poco::JSON::Object::Ptr Obj = IncomingParser.parse(Request.stream()).extract<Poco::JSON::Object::Ptr>();
std::cout << __LINE__ << std::endl;
ProvObjects::Entity E;
std::cout << __LINE__ << std::endl;
if (!E.from_json(Obj)) {
std::cout << __LINE__ << std::endl;
BadRequest(Request, Response);
return;
}
@@ -128,25 +118,20 @@ namespace OpenWifi{
E.managers.clear();
E.contacts.clear();
E.locations.clear();
std::cout << __LINE__ << std::endl;
if(Storage()->EntityDB().CreateRecord(E)) {
std::cout << __LINE__ << std::endl;
if(UUID==EntityDB::RootUUID())
Storage()->EntityDB().CheckForRoot();
std::cout << __LINE__ << std::endl;
Storage()->EntityDB().AddChild(std::string("id"),UUID,UUID);
OK(Request, Response);
return;
}
std::cout << __LINE__ << std::endl;
NotFound(Request,Response);
return;
} catch (const Poco::Exception &E) {
std::cout << __LINE__ << std::endl;
Logger_.log(E);
}
std::cout << __LINE__ << std::endl;
BadRequest(Request, Response);
}

30
src/dbhelpers.h Normal file
View File

@@ -0,0 +1,30 @@
//
// Created by stephane bourque on 2021-08-17.
//
#ifndef OWPROV_DBHELPERS_H
#define OWPROV_DBHELPERS_H
#include "storage_entity.h"
#include "storage_venue.h"
#include "storage_inventory.h"
#include "storage_contact.h"
#include "storage_location.h"
#include "storage_policies.h"
namespace OpenWifi {
template <typename DB> bool AddChild(DB &db, const std::string &parent_uuid, const std::string &child_uuid) {
}
// DB.getrecord("id",parent_uuid,P);
// P.children.push_back(child);
// DB.updaterecord(Parent_uuid,P);
}
}
#endif //OWPROV_DBHELPERS_H

View File

@@ -449,6 +449,90 @@ namespace ORM {
return false;
}
template <typename X> bool ManipulateVectorMember( X T, std::string &FieldName, std::string & ParentUUID, std::string & ChildUUID, bool Add) {
try {
RecordType R;
if(GetRecord(FieldName, ParentUUID, R)) {
if((R.*T).empty()) {
// if we are supposed to delete and the vector is empty, nothing to do
if(!Add)
return false;
// we are affing to an empty vector
(R.*T).push_back(ChildUUID);
} else {
auto it = std::find((R.*T).begin(),(R.*T).end(),ChildUUID);
// if we found the UUID we are trying to add/delete
if(it!=(R.*T).end() && *it == ChildUUID) {
// UUID is already there, so no need t oadd
if(Add)
return false;
// Just delete the entry
(R.*T).erase(it);
} else {
// we did not find the child, but we found where it should go
if(!Add)
return false;
(R.*T).insert(it,ChildUUID);
}
}
// we are only updating the record if we actually needed to change anything.
UpdateRecord(FieldName, ParentUUID, R);
return true;
}
} catch (const Poco::Exception &E) {
Logger_.log(E);
}
return false;
}
inline bool AddChild( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::children, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteChild( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::children, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddManager( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::managers, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteManager( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::managers, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddLocation( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::locations, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteLocation( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::locations, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddContact( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::contacts, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteContact( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::contacts, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddVenue( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::venues, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteVenue( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::venues, FieldName, ParentUUID, ChildUUID, false);
}
inline bool AddDevice( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::devices, FieldName, ParentUUID, ChildUUID, true);
}
inline bool DeleteDevice( std::string FieldName, std::string & ParentUUID, std::string & ChildUUID) {
return ManipulateVectorMember(&RecordType::devices, FieldName, ParentUUID, ChildUUID, false);
}
[[nodiscard]] inline std::string ComputeRange(uint64_t From, uint64_t HowMany) {
if(From<1) From=1;
if(Type==ORM::sqlite) {

View File

@@ -25,8 +25,9 @@ namespace OpenWifi {
ORM::Field{"state",ORM::FieldType::FT_TEXT},
ORM::Field{"postal",ORM::FieldType::FT_TEXT},
ORM::Field{"country",ORM::FieldType::FT_TEXT},
ORM::Field{"telephones",ORM::FieldType::FT_TEXT},
ORM::Field{"contact",ORM::FieldType::FT_TEXT}
ORM::Field{"phones",ORM::FieldType::FT_TEXT},
ORM::Field{"contact",ORM::FieldType::FT_TEXT},
ORM::Field{"mobiles",ORM::FieldType::FT_TEXT}
};
static ORM::IndexVec LocationDB_Indexes{
@@ -55,8 +56,9 @@ template<> void ORM::DB< OpenWifi::LocationDBRecordType, OpenWifi::ProvObject
Out.state = In.get<10>();
Out.postal = In.get<11>();
Out.country = In.get<12>();
uCentral::Types::from_string(In.get<13>(), Out.telephones);
uCentral::Types::from_string(In.get<13>(), Out.phones);
Out.contact = In.get<14>();
uCentral::Types::from_string(In.get<13>(), Out.mobiles);
}
template<> void ORM::DB< OpenWifi::LocationDBRecordType, OpenWifi::ProvObjects::Location>::Convert(OpenWifi::ProvObjects::Location &In, OpenWifi::LocationDBRecordType &Out) {
@@ -73,6 +75,7 @@ template<> void ORM::DB< OpenWifi::LocationDBRecordType, OpenWifi::ProvObject
Out.set<10>(In.state);
Out.set<11>(In.postal);
Out.set<12>(In.country);
Out.set<13>(uCentral::Types::to_string(In.telephones));
Out.set<13>(uCentral::Types::to_string(In.phones));
Out.set<14>(In.contact);
Out.set<13>(uCentral::Types::to_string(In.mobiles));
}

View File

@@ -24,6 +24,7 @@ namespace OpenWifi {
std::string,
std::string,
std::string,
std::string,
std::string
> LocationDBRecordType;

View File

@@ -120,7 +120,7 @@ getroot() {
}
setroot() {
payload="{ \"name\" : \"Arilia Root\", \"description\" ; \"This is the top level entry in the hierarchy.\" }";
payload="{ \"name\" : \"Arilia Root\", \"description\" : \"This is the top level entry in the hierarchy.\" }";
curl ${FLAGS} -X POST "https://${OWPROV}/api/v1/entity/0000-0000-0000" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer ${token}" \