diff options
Diffstat (limited to '')
-rw-r--r-- | umosapi/api.py | 20 | ||||
-rw-r--r-- | umosapi/app_db/uobject.py | 31 |
2 files changed, 22 insertions, 29 deletions
diff --git a/umosapi/api.py b/umosapi/api.py index 72240f5..f9e90f8 100644 --- a/umosapi/api.py +++ b/umosapi/api.py @@ -35,11 +35,6 @@ class fieldsDict(fields.Raw): user_model = api.model('UObject', { - 'name': fields.String( - required=True, - description='Name of the uobject', - example='Player' - ), 'datas': fieldsDict( required=False, description='Datas of the uobject in JSON format', @@ -64,16 +59,19 @@ class Register(Resource): def post(self): """ Register new uobject """ args = request.get_json(force=True) - status = uobject.register(args.get('name'), args.get('datas')) + status = uobject.register(args.get('datas')) - return {"msg": status['msg']}, status['code'] + if '_id' in status: + return {"_id": status['_id'], "msg": status['msg']}, status['code'] + else: + return {"msg": status['msg']}, status['code'] -@api.route('/objects/<name>') -@api.doc(params={'name': 'Player'}) +@api.route('/objects/<_id>') +@api.doc(params={'_id': '5d244cc13f3d46cb739912ae'}) class Remove(Resource): - def delete(self, name): + def delete(self, _id): """ Remove an uobject """ - status = uobject.remove(name) + status = uobject.remove(_id) return {"msg": status['msg']}, status['code'] diff --git a/umosapi/app_db/uobject.py b/umosapi/app_db/uobject.py index 7f26935..df07450 100644 --- a/umosapi/app_db/uobject.py +++ b/umosapi/app_db/uobject.py @@ -1,4 +1,5 @@ from flask import current_app +from bson.objectid import ObjectId from .db import MongoDB @@ -12,42 +13,36 @@ class UObject(object): mongo = db.connection() return list(mongo.db.uobjects.find({})) - def register(self, name, datas): + def register(self, datas): db = MongoDB(self.app) mongo = db.connection() error = {} - if not name: - error = {"msg": "UObject name is required.", "code": 400} - elif type(datas) != dict: + if type(datas) != dict: error = { "msg": "UObject datas type is not dict (JSON).", "code": 400 } - elif len(list(mongo.db.uobjects.find({"name": name}))) > 0: - error = { - "msg": "UObject {} is already registered.".format(name), - "code": 409 - } + if not error: - mongo.db.uobjects.insert({"name": name, "datas": datas}) - return {"msg": "UObject {} added.".format(name), "code": 201} + _id = mongo.db.uobjects.insert({"datas": datas}) + return {"_id": str(_id), "msg": "UObject added.", "code": 201} return error - def remove(self, name): + def remove(self, _id): db = MongoDB(self.app) mongo = db.connection() error = {} - if not name: - error = {"msg": "UObject name is required", "code": 400} - elif len(list(mongo.db.uobjects.find({"name": name}))) == 0: + if not _id: + error = {"msg": "UObject _id is required", "code": 400} + elif len(list(mongo.db.uobjects.find({"_id": ObjectId(_id)}))) == 0: error = { - 'msg': "UObject {} not exist. So it's good.".format(name), + 'msg': "UObject {} not exist. So it's good.".format(_id), "code": 404 } if not error: - mongo.db.uobjects.remove({"name": name}) - return {"msg": "UObject {} deleted".format(name), "code": 200} + mongo.db.uobjects.remove({"_id": ObjectId(_id)}) + return {"msg": "UObject {} deleted".format(_id), "code": 200} return error |