From 08bafaf3bc42e64f8402e27c226ec37e7eaf264a Mon Sep 17 00:00:00 2001
From: neodarz <neodarz@neodarz.net>
Date: Wed, 10 Jul 2019 16:32:00 +0200
Subject: Add collections choice support

---
 umosapi/api.py            | 42 +++++++++++++++++++++++-------------------
 umosapi/app_db/uobject.py | 28 ++++++++++++++--------------
 2 files changed, 37 insertions(+), 33 deletions(-)

diff --git a/umosapi/api.py b/umosapi/api.py
index 9729735..effd610 100644
--- a/umosapi/api.py
+++ b/umosapi/api.py
@@ -24,7 +24,7 @@ api = Api(
         Unity Mongo Save Api is a simple API for save Unity
         object in Mongo database. The terme uobject means Unity Object.
         """,
-        version=0.1
+        version=0.3
     )
 
 uobject = UObject(app)
@@ -60,61 +60,65 @@ uobject_model = api.model('UObject', {
 })
 
 
-@api.route('/objects')
+@api.route('/<collections>')
 class ObjectsList(Resource):
+    @api.doc(params={'collections': 'players'})
     @api.response(404, 'No UObjects in collection.')
     @api.marshal_list_with(uobject_model, mask=None)
-    def get(self):
+    def get(self, collections):
         """ Get uobjects list """
-        return sanitize(uobject.all())
+        return sanitize(uobject.all(collections))
 
     @api.expect(uobject_model)
+    @api.doc(params={'collections': 'players'})
     @api.marshal_with(uobject_model, code=201, mask=None)
-    def post(self):
+    def post(self, collections):
         """ Register new uobject """
         args = request.get_json(force=True)
-        return uobject.register(args.get('datas')), 201
+        return uobject.register(collections, args.get('datas')), 201
 
 
-@api.route("/objects/<_id>")
+@api.route("/<collections>/<_id>")
 class Objects(Resource):
     @api.expect(uobject_model)
-    @api.doc(params={'_id': '5d25a0f9e396fac104529444'})
+    @api.doc(params={'collections': 'players', '_id': '5d25a0f9e396fac104529444'})
     @api.response(200, 'UObjects updated.')
     @api.response(404, 'UObjects does not exist.')
     @api.marshal_with(uobject_model, mask=None)
-    def patch(self, _id):
+    def patch(self, collections, _id):
         """ Edit an uobject. """
         args = request.get_json(force=True)
-        return uobject.update(_id, args.get('datas')),
+        return uobject.update(collections, _id, args.get('datas')),
 
-    @api.doc(params={'_id': '5d244cc13f3d46cb739912ae'})
+    @api.doc(params={'collections': 'players', '_id': '5d244cc13f3d46cb739912ae'})
     @api.response(200, 'UObjects deleted.')
     @api.response(400, 'Something strange append, check msg value\
                 in response.')
     @api.response(404, 'UObjects does not exist.')
     @api.marshal_with(uobject_model, mask=None)
-    def delete(self, _id):
+    def delete(self, collections, _id):
         """ Remove an uobject """
-        return uobject.remove(_id)
+        return uobject.remove(collections, _id)
 
 
-@api.route("/objects/<key>/<value>")
+@api.route("/<collections>/<key>/<value>")
 class ObjectsEq(Resource):
     @api.doc(params={
+        'collections': 'players',
         'key': 'kill or total.kill',
         'value': '12',
         })
     @api.response(404, 'No UObjects in collection.')
     @api.marshal_list_with(uobject_model, mask=None)
-    def get(self, key, value):
+    def get(self, collections, key, value):
         """ Get uobjects matching key/value """
-        return sanitize(uobject.eq(key, value))
+        return sanitize(uobject.eq(collections, key, value))
 
 
-@api.route("/objects/reset")
+@api.route("/<collections>/reset")
 class ObjectsClean(Resource):
-    def get(self):
+    @api.doc(params={'collections': 'players'})
+    def get(self, collections):
         """ Reset collection """
-        uobject.reset()
+        uobject.reset(collections)
         return "done"
diff --git a/umosapi/app_db/uobject.py b/umosapi/app_db/uobject.py
index cffd9ba..5d282a5 100644
--- a/umosapi/app_db/uobject.py
+++ b/umosapi/app_db/uobject.py
@@ -9,12 +9,12 @@ class UObject(object):
     def __init__(self, app=None):
         self.app = app
 
-    def all(self):
+    def all(self, collections):
         db = MongoDB(self.app)
         mongo = db.connection()
-        return list(mongo.db.uobjects.find({}))
+        return list(mongo.db[collections].find({}))
 
-    def register(self, datas):
+    def register(self, collections, datas):
         db = MongoDB(self.app)
         mongo = db.connection()
 
@@ -24,10 +24,10 @@ class UObject(object):
             }
             return error, 400
 
-        _id = mongo.db.uobjects.insert({"datas": datas})
+        _id = mongo.db[collections].insert({"datas": datas})
         return {"_id": str(_id)}
 
-    def remove(self, _id):
+    def remove(self, collections, _id):
         db = MongoDB(self.app)
         mongo = db.connection()
 
@@ -36,20 +36,20 @@ class UObject(object):
                 "msg": "UObject _id is must match following regex [0-9a-z]{24}"
             }
             return error, 400
-        if len(list(mongo.db.uobjects.find({"_id": ObjectId(_id)}))) == 0:
+        if len(list(mongo.db[collections].find({"_id": ObjectId(_id)}))) == 0:
             return '', 404
 
-        mongo.db.uobjects.remove({"_id": ObjectId(_id)})
+        mongo.db[collections].remove({"_id": ObjectId(_id)})
         return {"_id": _id}, 200
 
-    def update(self, _id, datas):
+    def update(self, collections, _id, datas):
         db = MongoDB(self.app)
         mongo = db.connection()
 
         if not _id:
             error = {"msg": "UObject _id is required"}
             return error, 400
-        elif len(list(mongo.db.uobjects.find({"_id": ObjectId(_id)}))) == 0:
+        elif len(list(mongo.db[collections].find({"_id": ObjectId(_id)}))) == 0:
             error = {
                 "msg": "UObject {} does not exist.".format(_id)
             }
@@ -58,21 +58,21 @@ class UObject(object):
         datas = dict(
             ("datas.{}".format(key), value) for (key, value) in datas.items()
         )
-        mongo.db.uobjects.update_one(
+        mongo.db[collections].update_one(
             {"_id": ObjectId(_id)}, {"$set": datas}
         )
         return {"msg": "UObject updated.", "_id": _id}
 
-    def eq(self, key, value):
+    def eq(self, collections, key, value):
         db = MongoDB(self.app)
         mongo = db.connection()
 
-        return list(mongo.db.uobjects.find(
+        return list(mongo.db[collections].find(
             {"datas.{}".format(key): {"$eq": value}}
         ))
 
-    def reset(self):
+    def reset(self, collections):
         db = MongoDB(self.app)
         mongo = db.connection()
-        mongo.db.uobjects.remove()
+        mongo.db[collections].remove()
         return "done"
-- 
cgit v1.2.1