aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2019-08-05 18:41:36 +0200
committerneodarz <neodarz@neodarz.net>2019-08-05 18:41:36 +0200
commit289a3d0dcba30ad4a58a55af33956a5d210ef36d (patch)
tree4725e5c538f71ed35c30388cbf9ad8868881f332
parent4926674edceb5c03adf0daadc12f6949626f9a14 (diff)
downloadumosapicpp-289a3d0dcba30ad4a58a55af33956a5d210ef36d.tar.xz
umosapicpp-289a3d0dcba30ad4a58a55af33956a5d210ef36d.zip
Add ability to add uobject in database
Diffstat (limited to '')
-rw-r--r--umosapi.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/umosapi.cpp b/umosapi.cpp
index c10c79f..95831d9 100644
--- a/umosapi.cpp
+++ b/umosapi.cpp
@@ -160,6 +160,13 @@ class UmosapiService {
.response(Http::Code::Ok, "List of uobjects")
.response(backendErrorResponse);
+ versionPath
+ .route(desc.post("/:mcollection"))
+ .bind(&UmosapiService::addUObject, this)
+ .produces(MIME(Application, Json))
+ .parameter<Rest::Type::String>("mcollection", "Name of the collection where the uobjects are located")
+ .response(Http::Code::Ok, "Uobject created")
+ .response(backendErrorResponse);
}
void retrieveAll(const Rest::Request& request, Http::ResponseWriter response) {
@@ -179,6 +186,63 @@ class UmosapiService {
json_object_put(jsonObjects);
}
+ void addUObject(const Rest::Request& request, Http::ResponseWriter response) {
+ mongocxx::client conn{mongocxx::uri{config["mongoURI"]}};
+
+ auto collection = conn[config["mongo_db"]][request.param(":mcollection").as<string>()];
+
+ auto document = bsoncxx::from_json(request.body().c_str());
+
+ auto result = collection.insert_one(document.view());
+
+ std::string oid = result->inserted_id().get_oid().value.to_string();
+
+ auto jsonObject = json_object_new_object();
+
+ int stringlen = 0;
+ stringlen = strlen( request.body().c_str());
+ struct json_tokener *tok = json_tokener_new();
+ enum json_tokener_error jerr;
+
+ json_object *json_datas = NULL;
+
+ do {
+ json_datas = json_tokener_parse_ex(tok, request.body().c_str(), stringlen);
+ } while ((jerr = json_tokener_get_error(tok)) == json_tokener_continue);
+
+ if (jerr != json_tokener_success)
+ {
+ fprintf(stderr, "Error: %s\n", json_tokener_error_desc(jerr));
+ }
+
+ if (json_object_get_type(json_datas) == json_type_object) {
+ /* 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", json_datas);
+ } else {
+ cout << typeid(json_datas).name() << endl;
+ cout << "json_datas type: " << json_type_to_name(json_object_get_type(json_datas)) << endl;
+ }
+ json_tokener_reset(tok);
+
+ response.send(Http::Code::Ok, json_object_to_json_string_ext(jsonObject, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY), MIME(Application, Json));
+ json_object_put(jsonObject);
+ }
+
std::shared_ptr<Http::Endpoint> httpEndpoint;
Rest::Description desc;
Rest::Router router;