diff --git a/app/__init__.py b/app/__init__.py index ce226be..acc86f0 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -52,3 +52,45 @@ def ep_user_delete(user_id): return r else: return r, 403 + +@app.route("/category", methods = ["GET"]) +def ep_category_get(): + body = request.json + + if 'uuid' in body: + category = ldb.get_category(body['uuid']) + + if 'uuid' in category: + return category + else: + return category, 404 + else: + return {}, 403 + +@app.route("/category", methods = ["POST"]) +def ep_category_post(): + body = request.json + + if 'name' in body: + r = ldb.add_category(body['name']) + + if 'uuid' in r: + return r + else: + return r, 403 + else: + return {}, 403 + +@app.route("/category", methods = ["DELETE"]) +def ep_category_delete(): + body = request.json + + if 'uuid' in body: + category = ldb.del_category(body['uuid']) + + if 'uuid' in category: + return category + else: + return category, 404 + else: + return {}, 403 diff --git a/app/local_db.py b/app/local_db.py index ddb0b9b..959f23a 100644 --- a/app/local_db.py +++ b/app/local_db.py @@ -11,6 +11,10 @@ class LocalDB: if v == "test1": del self.users[k] + for k, v in list(self.categories.items()): + if v == "test2": + del self.categories[k] + def get_users(self): return self.users @@ -47,3 +51,37 @@ class LocalDB: } else: return {} + + def get_category(self, uid): + if uid in self.categories: + return { + 'uuid': uid, + 'name': self.categories[uid] + } + else: + return {} + + def add_category(self, name): + if not name: + return {} + + if name in set(self.categories.values()): + return {} + + new_uuid = uuid.uuid4().hex + self.categories[new_uuid] = name + return { + 'uuid': new_uuid, + 'name': name + } + + def del_category(self, uid): + if uid in self.categories: + name = self.categories[uid] + del self.categories[uid] + return { + 'uuid': uid, + 'name': name + } + else: + return {}