balance-application/routes/balance.py

182 lines
4.2 KiB
Python
Raw Normal View History

2024-09-10 04:14:09 +00:00
from datetime import datetime
2024-09-10 08:59:43 +00:00
from fastapi import APIRouter, Response, Request
from service.auth_service import AuthService
2024-09-10 04:14:09 +00:00
from service.balance_service import Balance, BalanceService, UpdateForm
router = APIRouter()
@router.post("/balance", status_code=201)
2024-09-10 08:59:43 +00:00
def insert(balance: Balance, req: Request, resp: Response):
2024-09-10 04:14:09 +00:00
started = datetime.now().microsecond / 1000
2024-09-10 08:59:43 +00:00
auth = AuthService()
if not auth.check_auth(req):
resp.status_code = 403
return {
"ok": 0,
"errno": "permission denied"
}
info = auth.get_data(req)
2024-09-10 04:14:09 +00:00
service = BalanceService()
2024-09-10 08:59:43 +00:00
ok = service.create(info["username"], balance=balance)
2024-09-10 05:20:11 +00:00
if not ok == 1:
resp.status_code = 500
return {
"ok": 0,
"errno": "error occurred to running transaction"
}
2024-09-10 04:14:09 +00:00
return {
"ok": 1,
2024-09-10 05:20:11 +00:00
"name": balance.name,
2024-09-10 07:36:03 +00:00
"is_buy": balance.buy,
2024-09-10 04:14:09 +00:00
"respond_time": "{}ms".format(round((datetime.now().microsecond / 1000) - started))
}
2024-09-10 08:59:43 +00:00
@router.get("/balance")
def query(req: Request, resp: Response):
started = datetime.now().microsecond / 1000
auth = AuthService()
if not auth.check_auth(req):
resp.status_code = 403
return {
"ok": 0,
"errno": "permission denied"
}
service = BalanceService()
data = service.query()
if data == None:
resp.status_code = 204
return {
"ok": 0,
"errno": "no content"
}
return {
"ok": 1,
"data": data,
"respond_time": "{}ms".format(round((datetime.now().microsecond / 1000) - started))
}
2024-09-10 04:14:09 +00:00
@router.get("/balance/{id}")
2024-09-10 08:59:43 +00:00
def find(id, req: Request, resp: Response):
2024-09-10 04:14:09 +00:00
started = datetime.now().microsecond / 1000
2024-09-10 08:59:43 +00:00
auth = AuthService()
if not auth.check_auth(req):
resp.status_code = 403
return {
"ok": 0,
"errno": "permission denied"
}
2024-09-10 04:14:09 +00:00
service = BalanceService()
2024-09-10 07:36:03 +00:00
data = service.read(int(id))
if data == None:
2024-09-10 04:14:09 +00:00
resp.status_code = 204
return {"ok": 0, "errno": "id '{}' result is not found".format(id)}
return {
"ok": 1,
"id": int(id),
"data": data,
"respond_time": "{}ms".format(round((datetime.now().microsecond / 1000) - started))
}
@router.patch("/balance/{action}/{id}")
2024-09-10 08:59:43 +00:00
def update(action, id, balance: UpdateForm, req: Request, resp: Response):
started = datetime.now().microsecond / 1000
auth = AuthService()
print(auth.check_auth(req))
if not auth.check_auth(req):
resp.status_code = 403
return {
"ok": 0,
"errno": "permission denied"
}
2024-09-10 04:14:09 +00:00
service = BalanceService()
2024-09-10 07:36:03 +00:00
if action != "name" and action != "date" and action != "price" and action != "buy" and action != "memo":
2024-09-10 04:14:09 +00:00
print(action)
print(id)
resp.status_code = 400
return {"ok": 0, "errno": "action must be to name, date, price or memo"}
if action == "name" and balance.name == "":
resp.status_code = 400
return {"ok": 0, "action": action, "errno": "name value cannot be empty"}
if action == "date" and balance.date <= 0:
resp.status_code = 400
return {"ok": 0, "action": action, "errno": "date value cannot be 0 or minus"}
if action == "price" and balance.price <= 0:
resp.status_code = 400
return {"ok": 0, "action": action, "errno": "price value cannot be 0 or minus"}
if action == "memo" and len(balance.memo) > 300:
resp.status_code = 400
return {
"ok": 0,
"action": action,
"errno": "memo value size is too long: (maximum size: 300 bytes, your size: {} bytes)".format(len(balance.memo))
}
2024-09-10 05:20:11 +00:00
ok = service.update(
2024-09-10 04:14:09 +00:00
int(id),
2024-09-10 07:36:03 +00:00
action,
{
2024-09-10 04:14:09 +00:00
"name": balance.name,
"date": balance.date,
"price": balance.price,
2024-09-10 07:36:03 +00:00
"buy": balance.buy,
2024-09-10 04:14:09 +00:00
"memo": balance.memo
}
)
2024-09-10 05:20:11 +00:00
if not ok == 1:
resp.status_code = 500
return {
"ok": 0,
"errno": "error occurred to running transaction"
}
2024-09-10 04:14:09 +00:00
return {
"ok": 1,
"id": int(id),
2024-09-10 08:59:43 +00:00
"action": action,
"respond_time": "{}ms".format(round((datetime.now().microsecond / 1000) - started))
2024-09-10 04:14:09 +00:00
}
@router.delete("/balance/{id}")
2024-09-10 08:59:43 +00:00
def delete(id, req: Request, resp: Response):
2024-09-10 04:14:09 +00:00
started = datetime.now().microsecond / 1000
2024-09-10 08:59:43 +00:00
auth = AuthService()
if not auth.check_auth(req):
resp.status_code = 403
return {
"ok": 0,
"errno": "permission denied"
}
2024-09-10 04:14:09 +00:00
service = BalanceService()
2024-09-10 05:20:11 +00:00
ok = service.delete(int(id))
if not ok == 1:
resp.status_code = 500
return {
"ok": 0,
"errno": "error occurred to running transaction"
}
2024-09-10 04:14:09 +00:00
return {
"ok": 1,
"id": int(id),
"respond_time": "{}ms".format((round(datetime.now().microsecond / 1000) - started))
}