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))
|
|
|
|
}
|
|
|
|
|
2024-09-15 03:31:20 +00:00
|
|
|
@router.put("/balance/{id}")
|
2024-09-14 15:37:26 +00:00
|
|
|
def update(id, balance: UpdateForm, req: Request, resp: Response):
|
2024-09-10 08:59:43 +00:00
|
|
|
started = datetime.now().microsecond / 1000
|
|
|
|
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-15 08:22:57 +00:00
|
|
|
ok = service.update(int(id), balance)
|
2024-09-10 04:14:09 +00:00
|
|
|
|
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
|
|
|
"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))
|
|
|
|
}
|