From 2ec8ef353eeaf133f87177cca2dae828454ba83b Mon Sep 17 00:00:00 2001
From: neodarz <neodarz@neodarz.net>
Date: Wed, 10 Jul 2019 11:03:39 +0200
Subject: Add search equal function

---
 umosapi/api.py            | 16 ++++++++++++++++
 umosapi/app_db/uobject.py | 20 ++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/umosapi/api.py b/umosapi/api.py
index 112960f..8a0945f 100644
--- a/umosapi/api.py
+++ b/umosapi/api.py
@@ -82,3 +82,19 @@ class Objects(Resource):
         status = uobject.remove(_id)
 
         return {"msg": status['msg']}, status['code']
+
+
+@api.route("/objects/<key>/<value>")
+class ObjectsEq(Resource):
+    @api.doc(params={
+        'key': 'kill or total.kill',
+        'value': '12',
+        })
+    def get(self, key, value):
+        """ Get uobjects matching key/value """
+        status = uobject.eq(key, value)
+
+        return {
+            "msg": status['msg'],
+            "datas": sanitize(status['datas'])
+        }, status['code']
diff --git a/umosapi/app_db/uobject.py b/umosapi/app_db/uobject.py
index 9830187..60094c7 100644
--- a/umosapi/app_db/uobject.py
+++ b/umosapi/app_db/uobject.py
@@ -65,3 +65,23 @@ class UObject(object):
             mongo.db.uobjects.update_one({"_id": ObjectId(_id)}, {"$set": datas})
             return {"msg": "UObject updated.", "code": 200}
         return error
+
+    def eq(self, key, value):
+        db = MongoDB(self.app)
+        mongo = db.connection()
+
+        datas = list(mongo.db.uobjects.find(
+            {"datas.{}".format(key): {"$eq": value}}
+        ))
+        if datas:
+            return {
+                "msg": "UObjects found",
+                "datas": datas,
+                "code": 200
+            }
+        else:
+            return {
+                "msg": "UObjects not found",
+                "datas": datas,
+                "code": 404
+            }
-- 
cgit v1.2.1