aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorneodarz <neodarz@neodarz.net>2019-08-09 16:28:59 +0200
committerneodarz <neodarz@neodarz.net>2019-08-09 16:28:59 +0200
commitcf86e3de524ff247f496c34cd9e8df61cd4e6845 (patch)
treec7070f754e7d2d7f238bcc776161628f18998c21
parenta6408962010c1880a54d2f0f3a1e35b0ca65531d (diff)
downloadumosapicpp-cf86e3de524ff247f496c34cd9e8df61cd4e6845.tar.xz
umosapicpp-cf86e3de524ff247f496c34cd9e8df61cd4e6845.zip
Add ability to search uobjects
-rw-r--r--api/umosapi.cpp19
-rw-r--r--api/umosapi.h2
-rw-r--r--db/uobject.cpp22
-rw-r--r--db/uobject.h1
4 files changed, 44 insertions, 0 deletions
diff --git a/api/umosapi.cpp b/api/umosapi.cpp
index 6be8690..9db650f 100644
--- a/api/umosapi.cpp
+++ b/api/umosapi.cpp
@@ -113,6 +113,16 @@ void UmosapiService::createDescription() {
.parameter<Rest::Type::String>("oid", "MongoDB oid of the uobject")
.response(Http::Code::Ok, "Uobject deleted")
.response(backendErrorResponse);
+
+ versionPath
+ .route(desc.get("/:mcollection/:key/:value"))
+ .bind(&UmosapiService::UmosapiService::searchUObjectByKeyValue, this)
+ .produces(MIME(Application, Json))
+ .parameter<Rest::Type::String>("mcollection", "Name of the collection where the uobjects are located")
+ .parameter<Rest::Type::String>("key", "Key of uobject to search, ex.: kil or total.kill")
+ .parameter<Rest::Type::String>("value", "Value of uobject to search, ex.: 12")
+ .response(Http::Code::Ok, "Uobject found")
+ .response(backendErrorResponse);
}
void UmosapiService::retrieveAll(const Rest::Request& request, Http::ResponseWriter response) {
@@ -140,3 +150,12 @@ void UmosapiService::deleteUObject(const Rest::Request& request, Http::ResponseW
response.send(Http::Code::Ok, json_string, MIME(Application, Json));
}
+
+void UmosapiService::searchUObjectByKeyValue(const Rest::Request& request, Http::ResponseWriter response) {
+ auto jsonObject = json_object_new_array();
+
+ auto json_string = uobject::searchKeyValue(request.param(":mcollection").as<string>(), request.param(":key").as<string>(), request.param(":value").as<string>(), jsonObject);
+
+ response.send(Http::Code::Ok, json_string, MIME(Application, Json));
+ json_object_put(jsonObject);
+}
diff --git a/api/umosapi.h b/api/umosapi.h
index 2e5e343..20c6d7b 100644
--- a/api/umosapi.h
+++ b/api/umosapi.h
@@ -28,6 +28,8 @@ class UmosapiService {
void deleteUObject(const Rest::Request& request, Http::ResponseWriter response);
+ void searchUObjectByKeyValue(const Rest::Request& request, Http::ResponseWriter response);
+
std::shared_ptr<Http::Endpoint> httpEndpoint;
Rest::Description desc;
Rest::Router router;
diff --git a/db/uobject.cpp b/db/uobject.cpp
index 0b76daf..bfaf7df 100644
--- a/db/uobject.cpp
+++ b/db/uobject.cpp
@@ -102,3 +102,25 @@ std::string uobject::remove(std::string collection, std::string oid, struct json
}
return json_object_to_json_string_ext(jsonObject, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
}
+
+std::string uobject::searchKeyValue(std::string collection, std::string key, std::string value, struct json_object* jsonArray) {
+ auto conn = mongo.get_connection();
+
+ auto coll = (*conn)[config["mongo_db"]][collection];
+
+ // Generate a string with the folowing format:
+ // {"datas.key":"value"}
+ std::string json_string = "{\"datas.";
+ json_string += key;
+ json_string += "\":\"";
+ json_string += value;
+ json_string += "\"}";
+
+ auto cursor = coll.find(bsoncxx::from_json(json_string));
+
+ for (auto&& doc : cursor) {
+ json_object_array_add(jsonArray, json_tokener_parse(bsoncxx::to_json(doc).c_str()));
+ }
+
+ return json_object_to_json_string_ext(jsonArray, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY);
+}
diff --git a/db/uobject.h b/db/uobject.h
index 3e0fd1e..0533661 100644
--- a/db/uobject.h
+++ b/db/uobject.h
@@ -23,6 +23,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);
std::string remove(std::string collection, std::string oid, struct json_object* jsonObjects);
+ std::string searchKeyValue(std::string collection, std::string key, std::string value, struct json_object* jsonArray);
}
#endif