add user-related endpoints

This commit is contained in:
ІО-23 Шмуляр Олег 2024-10-28 17:19:47 +02:00
parent 8b7e93bd67
commit 3390b2e374
Signed by: hasslesstech
GPG Key ID: 09745A46126DDD4C
2 changed files with 83 additions and 1 deletions

View File

@ -1,7 +1,9 @@
from flask import Flask
from flask import Flask, request
import time
from app.local_db import LocalDB
app = Flask(__name__)
ldb = LocalDB()
@app.route("/healthcheck")
def ep_healthcheck():
@ -9,3 +11,39 @@ def ep_healthcheck():
"date": time.strftime('%Y.%m.%d %H:%M:%S'),
"status": "OK"
}
@app.route("/users", methods = ["GET"])
def ep_users_get():
return ldb.get_users()
@app.route("/user/<user_id>", methods = ["GET"])
def ep_user_get(user_id):
user = ldb.get_user(user_id)
if 'uuid' in user:
return user
else:
return user, 404
@app.route("/user", methods = ["POST"])
def ep_user_post():
body = request.json
if 'name' in body:
r = ldb.add_user(body['name'])
if 'uuid' in r:
return r
else:
return r, 403
else:
return {}, 403
@app.route("/user/<user_id>", methods = ["DELETE"])
def ep_user_delete(user_id):
r = ldb.del_user(user_id)
if 'uuid' in r:
return r
else:
return r, 403

44
app/local_db.py Normal file
View File

@ -0,0 +1,44 @@
import uuid
class LocalDB:
def __init__(self):
self.users = {}
self.categories = {}
self.records = {}
def get_users(self):
return self.users
def get_user(self, uid):
if uid in self.users:
return {
'uuid': uid,
'name': self.users[uid]
}
else:
return {}
def add_user(self, name):
if not name:
return {}
if name in set(self.users.values()):
return {}
new_uuid = uuid.uuid4().hex
self.users[new_uuid] = name
return {
'uuid': new_uuid,
'name': name
}
def del_user(self, uid):
if uid in self.users:
name = self.users[uid]
del self.users[uid]
return {
'uuid': uid,
'name': name
}
else:
return {}