aboutsummaryrefslogtreecommitdiff
path: root/db/uobject.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'db/uobject.cpp')
-rw-r--r--db/uobject.cpp33
1 files changed, 33 insertions, 0 deletions
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);
+}