aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-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