diff options
author | neodarz <neodarz@neodarz.net> | 2019-08-08 19:07:02 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-08-08 19:07:02 +0200 |
commit | 85e1431aa382f721e3b51804ca0b00ab063553ef (patch) | |
tree | 71ea37b36a5c534e62b6d755aa03405a122e934a | |
parent | f502716912b349ea446aa52556701bbda9083de8 (diff) | |
download | umosapicpp-85e1431aa382f721e3b51804ca0b00ab063553ef.tar.xz umosapicpp-85e1431aa382f721e3b51804ca0b00ab063553ef.zip |
Add ability to delete an uobject
Diffstat (limited to '')
-rw-r--r-- | api/umosapi.cpp | 18 | ||||
-rw-r--r-- | api/umosapi.h | 2 | ||||
-rw-r--r-- | db/uobject.cpp | 33 | ||||
-rw-r--r-- | db/uobject.h | 4 |
4 files changed, 57 insertions, 0 deletions
diff --git a/api/umosapi.cpp b/api/umosapi.cpp index de2a693..6be8690 100644 --- a/api/umosapi.cpp +++ b/api/umosapi.cpp @@ -104,6 +104,15 @@ void UmosapiService::createDescription() { .parameter<Rest::Type::String>("mcollection", "Name of the collection where the uobjects are located") .response(Http::Code::Ok, "Uobject created") .response(backendErrorResponse); + + versionPath + .route(desc.del("/:mcollection/:oid")) + .bind(&UmosapiService::UmosapiService::deleteUObject, this) + .produces(MIME(Application, Json)) + .parameter<Rest::Type::String>("mcollection", "Name of the collection where the uobjects are located") + .parameter<Rest::Type::String>("oid", "MongoDB oid of the uobject") + .response(Http::Code::Ok, "Uobject deleted") + .response(backendErrorResponse); } void UmosapiService::retrieveAll(const Rest::Request& request, Http::ResponseWriter response) { @@ -122,3 +131,12 @@ void UmosapiService::addUObject(const Rest::Request& request, Http::ResponseWrit response.send(Http::Code::Ok, json_string, MIME(Application, Json)); json_object_put(jsonObject); } + +void UmosapiService::deleteUObject(const Rest::Request& request, Http::ResponseWriter response) { + + auto jsonObjet = json_object_new_object(); + + auto json_string = uobject::remove(request.param(":mcollection").as<string>(), request.param(":oid").as<string>(), jsonObjet); + + response.send(Http::Code::Ok, json_string, MIME(Application, Json)); +} diff --git a/api/umosapi.h b/api/umosapi.h index 684f9d5..2e5e343 100644 --- a/api/umosapi.h +++ b/api/umosapi.h @@ -26,6 +26,8 @@ class UmosapiService { void addUObject(const Rest::Request& request, Http::ResponseWriter response); + void deleteUObject(const Rest::Request& request, Http::ResponseWriter response); + std::shared_ptr<Http::Endpoint> httpEndpoint; Rest::Description desc; Rest::Router router; diff --git a/db/uobject.cpp b/db/uobject.cpp index 087c482..59a5b0b 100644 --- a/db/uobject.cpp +++ b/db/uobject.cpp @@ -1,5 +1,8 @@ #include "uobject.h" +using bsoncxx::builder::basic::kvp; +using bsoncxx::builder::basic::make_document; + std::string uobject::retrieveAll(std::string collection, struct json_object* jsonObjects) { auto conn = mongo.get_connection(); @@ -69,3 +72,33 @@ auto conn = mongo.get_connection(); return json_object_to_json_string_ext(jsonObject, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); } + +std::string uobject::remove(std::string collection, std::string oid, struct json_object* jsonObject) { + auto conn = mongo.get_connection(); + + auto coll = (*conn)[config["mongo_db"]][collection]; + + auto result = coll.delete_one(make_document(kvp("_id", bsoncxx::oid(oid)))); + + + if (result->deleted_count() > 0) { + /* Create an object with the following template: + * { + * "_id": { + * "$oid": "5d484d371ec4865f767d8424" + * }, + * "datas": { + * [...] + * } + * } + */ + + auto json_id = json_object_new_object(); + auto json_oid = json_object_new_string(oid.c_str()); + + json_object_object_add(json_id, "$oid", json_oid); + json_object_object_add(jsonObject, "_id", json_id); + json_object_object_add(jsonObject, "datas", {}); + } + return json_object_to_json_string_ext(jsonObject, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); +} diff --git a/db/uobject.h b/db/uobject.h index e08429f..3e0fd1e 100644 --- a/db/uobject.h +++ b/db/uobject.h @@ -2,6 +2,9 @@ #define Uobject_H_ #include <bsoncxx/builder/stream/document.hpp> +#include <bsoncxx/builder/basic/kvp.hpp> +#include <bsoncxx/oid.hpp> + #include <bsoncxx/json.hpp> #include <mongocxx/client.hpp> @@ -19,6 +22,7 @@ namespace uobject { std::string retrieveAll(std::string collection, struct json_object* jsonObjects); std::string add(std::string collection, struct json_object* jsonObjects, const char * body); + std::string remove(std::string collection, std::string oid, struct json_object* jsonObjects); } #endif |