add category-related endpoints

This commit is contained in:
ІО-23 Шмуляр Олег 2024-10-28 18:18:28 +02:00
parent 5d9da36bc5
commit e6954e5710
Signed by: hasslesstech
GPG Key ID: 09745A46126DDD4C
2 changed files with 80 additions and 0 deletions

View File

@ -52,3 +52,45 @@ def ep_user_delete(user_id):
return r return r
else: else:
return r, 403 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

View File

@ -11,6 +11,10 @@ class LocalDB:
if v == "test1": if v == "test1":
del self.users[k] del self.users[k]
for k, v in list(self.categories.items()):
if v == "test2":
del self.categories[k]
def get_users(self): def get_users(self):
return self.users return self.users
@ -47,3 +51,37 @@ class LocalDB:
} }
else: else:
return {} 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 {}