add category-related endpoints
This commit is contained in:
parent
5d9da36bc5
commit
e6954e5710
|
@ -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
|
||||
|
|
|
@ -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 {}
|
||||
|
|
Loading…
Reference in New Issue