From 185e684a4648849b35cce65a00465cb41a309292 Mon Sep 17 00:00:00 2001 From: neodarz Date: Thu, 1 Aug 2019 19:33:22 +0200 Subject: Add mongodb --- README.md | 1 + umosapi.cpp | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 299e307..8d866f1 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Install: - Pistache - json-c +- mongocxx # Build diff --git a/umosapi.cpp b/umosapi.cpp index f6f0cdd..8d5d90d 100644 --- a/umosapi.cpp +++ b/umosapi.cpp @@ -10,6 +10,16 @@ #include +#include +#include + +#include +#include + +#include +#include +#include + #include #include "clara.hpp" @@ -17,6 +27,12 @@ using namespace std; using namespace Pistache; +using bsoncxx::builder::stream::close_array; +using bsoncxx::builder::stream::close_document; +using bsoncxx::builder::stream::document; +using bsoncxx::builder::stream::finalize; +using bsoncxx::builder::stream::open_array; +using bsoncxx::builder::stream::open_document; using namespace clara; @@ -80,17 +96,30 @@ class UmosapiService { auto versionPath = desc.path("/v1"); versionPath - .route(desc.get("/:collections")) + .route(desc.get("/:mcollection")) .bind(&UmosapiService::retrieveAll, this) .produces(MIME(Application, Json)) - .parameter("collection", "Name of the collection where the uobjects are located") + .parameter("mcollection", "Name of the collection where the uobjects are located") .response(Http::Code::Ok, "List of uobjects") .response(backendErrorResponse); } - void retrieveAll(const Rest::Request&, Http::ResponseWriter response) { - response.send(Http::Code::Ok, "No fucking objects"); + void retrieveAll(const Rest::Request& request, Http::ResponseWriter response) { + mongocxx::client conn{mongocxx::uri{"mongodb://127.0.0.1:27017"}}; + + auto collection = conn["umosapi"][request.param(":mcollection").as()]; + + 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)); + json_object_put(jsonObjects); } std::shared_ptr httpEndpoint; -- cgit v1.2.1