diff options
author | neodarz <neodarz@neodarz.net> | 2019-08-08 17:18:09 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-08-08 17:18:09 +0200 |
commit | f502716912b349ea446aa52556701bbda9083de8 (patch) | |
tree | a8d4dd1c7f828cde7e44b56a36e905aba01732f8 | |
parent | 0ce9ff7103a8303cbf7bbefe22adbc2b9505ea17 (diff) | |
download | umosapicpp-f502716912b349ea446aa52556701bbda9083de8.tar.xz umosapicpp-f502716912b349ea446aa52556701bbda9083de8.zip |
Split addUobject function
Diffstat (limited to '')
-rw-r--r-- | api/umosapi.cpp | 52 | ||||
-rw-r--r-- | db/uobject.cpp | 55 | ||||
-rw-r--r-- | db/uobject.h | 1 |
3 files changed, 58 insertions, 50 deletions
diff --git a/api/umosapi.cpp b/api/umosapi.cpp index 5473e5a..de2a693 100644 --- a/api/umosapi.cpp +++ b/api/umosapi.cpp @@ -115,58 +115,10 @@ void UmosapiService::retrieveAll(const Rest::Request& request, Http::ResponseWri } void UmosapiService::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; + auto json_string = uobject::add(request.param(":mcollection").as<string>(), jsonObject, request.body().c_str()); - 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)); + response.send(Http::Code::Ok, json_string, MIME(Application, Json)); json_object_put(jsonObject); } diff --git a/db/uobject.cpp b/db/uobject.cpp index 769631e..087c482 100644 --- a/db/uobject.cpp +++ b/db/uobject.cpp @@ -14,3 +14,58 @@ std::string uobject::retrieveAll(std::string collection, struct json_object* jso return json_object_to_json_string_ext(jsonObjects, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY); } + +std::string uobject::add(std::string collection, struct json_object* jsonObject, const char * body) { +auto conn = mongo.get_connection(); + + auto coll = (*conn)[config["mongo_db"]][collection]; + + auto document = bsoncxx::from_json(body); + + auto result = coll.insert_one(document.view()); + + std::string oid = result->inserted_id().get_oid().value.to_string(); + + + int stringlen = 0; + stringlen = strlen( body ); + 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, body, 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 { + std::cout << typeid(json_datas).name() << std::endl; + std::cout << "json_datas type: " << json_type_to_name(json_object_get_type(json_datas)) << std::endl; + } + json_tokener_reset(tok); + + 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 b4c2ef0..e08429f 100644 --- a/db/uobject.h +++ b/db/uobject.h @@ -18,6 +18,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); } #endif |