aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2019-08-08 15:50:31 +0200
committerneodarz <neodarz@neodarz.net>2019-08-08 15:50:31 +0200
commit0ce9ff7103a8303cbf7bbefe22adbc2b9505ea17 (patch)
treee924b3f1d325fe961f19a62f1aeaf038eaa3a4ef
parentdb5f789c5aeaee55ee0b54cb251eb5e5fbc0ec5b (diff)
downloadumosapicpp-0ce9ff7103a8303cbf7bbefe22adbc2b9505ea17.tar.xz
umosapicpp-0ce9ff7103a8303cbf7bbefe22adbc2b9505ea17.zip
Split some codes
-rw-r--r--CMakeLists.txt2
-rw-r--r--api/umosapi.cpp16
-rw-r--r--db/uobject.cpp16
-rw-r--r--db/uobject.h23
4 files changed, 44 insertions, 13 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 56af362..db27ade 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -41,7 +41,7 @@ or
endif(NOT JSONC_FOUND)
-add_executable(umosapi main.cpp config.cpp api/umosapi.cpp db/mongo_access.cpp api/umosapi.h config.h db/mongo_access.h shared.h)
+add_executable(umosapi main.cpp config.cpp api/umosapi.cpp db/mongo_access.cpp db/uobject.cpp api/umosapi.h config.h db/mongo_access.h db/uobject.h shared.h)
set_property(TARGET umosapi PROPERTY CXX_STANDARD 17)
diff --git a/api/umosapi.cpp b/api/umosapi.cpp
index de2a2c3..5473e5a 100644
--- a/api/umosapi.cpp
+++ b/api/umosapi.cpp
@@ -15,6 +15,7 @@
#include "umosapi.h"
#include "../shared.h"
#include "../db/mongo_access.h"
+#include "../db/uobject.h"
mongo_access mongo;
@@ -106,20 +107,11 @@ void UmosapiService::createDescription() {
}
void UmosapiService::retrieveAll(const Rest::Request& request, Http::ResponseWriter response) {
- auto conn = mongo.get_connection();
-
- auto collection = (*conn)[config["mongo_db"]][request.param(":mcollection").as<string>()];
-
- auto cursor = collection.find({});
-
auto jsonObjects = json_object_new_array();
-
- for (auto&& doc : cursor) {
- json_object_array_add(jsonObjects, json_tokener_parse(bsoncxx::to_json(doc).c_str()));
- }
-
- response.send(Http::Code::Ok, json_object_to_json_string_ext(jsonObjects, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY), MIME(Application, Json));
+ auto json_string = uobject::retrieveAll(request.param(":mcollection").as<string>(), jsonObjects);
json_object_put(jsonObjects);
+
+ response.send(Http::Code::Ok, json_string, MIME(Application, Json));
}
void UmosapiService::addUObject(const Rest::Request& request, Http::ResponseWriter response) {
diff --git a/db/uobject.cpp b/db/uobject.cpp
new file mode 100644
index 0000000..769631e
--- /dev/null
+++ b/db/uobject.cpp
@@ -0,0 +1,16 @@
+#include "uobject.h"
+
+std::string uobject::retrieveAll(std::string collection, struct json_object* jsonObjects) {
+
+ auto conn = mongo.get_connection();
+
+ auto coll = (*conn)[config["mongo_db"]][collection];
+
+ auto cursor = coll.find({});
+
+ for (auto&& doc : cursor) {
+ json_object_array_add(jsonObjects, json_tokener_parse(bsoncxx::to_json(doc).c_str()));
+ }
+
+ return json_object_to_json_string_ext(jsonObjects, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
+}
diff --git a/db/uobject.h b/db/uobject.h
new file mode 100644
index 0000000..b4c2ef0
--- /dev/null
+++ b/db/uobject.h
@@ -0,0 +1,23 @@
+#ifndef Uobject_H_
+#define Uobject_H_
+
+#include <bsoncxx/builder/stream/document.hpp>
+#include <bsoncxx/json.hpp>
+
+#include <mongocxx/client.hpp>
+#include <mongocxx/instance.hpp>
+
+#include <mongocxx/client.hpp>
+#include <mongocxx/stdx.hpp>
+#include <mongocxx/uri.hpp>
+
+#include <json-c/json.h>
+
+#include "../shared.h"
+#include "../db/mongo_access.h"
+
+namespace uobject {
+ std::string retrieveAll(std::string collection, struct json_object* jsonObjects);
+}
+
+#endif