-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.py
More file actions
37 lines (29 loc) · 973 Bytes
/
backend.py
File metadata and controls
37 lines (29 loc) · 973 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from fastapi import FastAPI
from pydantic import BaseModel
from typing import Dict
import database as db
import logging
log=logging.getLogger()
class Discount(BaseModel):
discount:Dict[str,int]
class AddDiscount(BaseModel):
discount:Dict[str,int]
class RemoveDiscount(BaseModel):
discount:str
app=FastAPI()
@app.get("/client/discount/{id}/")
async def get_discount(id:int):
discounts=await db.discount_get(id)
discounts=Discount.model_validate(discounts)
log.info(f"Got discount of user:{id}")
return {id:discounts}
@app.patch("/client/{id}/discount/")
async def add_discount(id:int,data:AddDiscount):
await db.add_discount(id,data.discount)
log.info(f"Added discount to user:{id}")
return{'status':'ok'}
@app.patch("/client/{id}/discount_remove/")
async def remove_discount(id:int,data:RemoveDiscount):
await db.remove_discount(id,data.discount)
log.info(f"Removed discount from user:{id}")
return{'status':'ok'}