From e6954e57104080e3d8a73588047ae89fb2168dc2 Mon Sep 17 00:00:00 2001 From: hasslesstech Date: Mon, 28 Oct 2024 18:18:28 +0200 Subject: [PATCH] add category-related endpoints --- app/__init__.py | 42 ++++++++++++++++++++++++++++++++++++++++++ app/local_db.py | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) 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 {}