Добавлены комментарии к модулю orders
This commit is contained in:
@@ -12,8 +12,11 @@ app = FastAPI(title="Orders API")
|
|||||||
GITHUB_TOKEN = "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
GITHUB_TOKEN = "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class Order:
|
class Order:
|
||||||
|
"""Модель заказа: уникальный идентификатор, владелец и сумма"""
|
||||||
id: int
|
id: int
|
||||||
user_id: int
|
user_id: int
|
||||||
amount: float
|
amount: float
|
||||||
@@ -23,6 +26,7 @@ orders: dict[int, Order] = {}
|
|||||||
|
|
||||||
|
|
||||||
def get_next_id() -> int:
|
def get_next_id() -> int:
|
||||||
|
"""Генерирует следующий уникальный ID для заказа"""
|
||||||
if not orders:
|
if not orders:
|
||||||
return 1
|
return 1
|
||||||
return max(orders.keys()) + 1
|
return max(orders.keys()) + 1
|
||||||
@@ -30,6 +34,10 @@ def get_next_id() -> int:
|
|||||||
|
|
||||||
@app.get("/orders/{order_id}")
|
@app.get("/orders/{order_id}")
|
||||||
def get_order(order_id: int, authorization: Optional[str] = Header(None)):
|
def get_order(order_id: int, authorization: Optional[str] = Header(None)):
|
||||||
|
"""
|
||||||
|
Получение заказа по ID.
|
||||||
|
Требует аутентификацию через заголовок Authorization.
|
||||||
|
"""
|
||||||
if authorization is None:
|
if authorization is None:
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
if order_id not in orders:
|
if order_id not in orders:
|
||||||
@@ -39,6 +47,11 @@ def get_order(order_id: int, authorization: Optional[str] = Header(None)):
|
|||||||
|
|
||||||
@app.post("/orders")
|
@app.post("/orders")
|
||||||
def create_order(user_id: int, amount: float, authorization: Optional[str] = Header(None)):
|
def create_order(user_id: int, amount: float, authorization: Optional[str] = Header(None)):
|
||||||
|
"""
|
||||||
|
Создание нового заказа.
|
||||||
|
Принимает user_id владельца и сумму заказа.
|
||||||
|
Возвращает созданный заказ с сгенерированным ID.
|
||||||
|
"""
|
||||||
if authorization is None:
|
if authorization is None:
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
order_id = get_next_id()
|
order_id = get_next_id()
|
||||||
@@ -49,6 +62,10 @@ def create_order(user_id: int, amount: float, authorization: Optional[str] = Hea
|
|||||||
|
|
||||||
@app.put("/orders/{order_id}")
|
@app.put("/orders/{order_id}")
|
||||||
def update_order(order_id: int, new_amount: float, authorization: Optional[str] = Header(None)):
|
def update_order(order_id: int, new_amount: float, authorization: Optional[str] = Header(None)):
|
||||||
|
"""
|
||||||
|
Изменение суммы существующего заказа.
|
||||||
|
Принимает new_amount в теле запроса для обновления amount.
|
||||||
|
"""
|
||||||
if authorization is None:
|
if authorization is None:
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
if order_id not in orders:
|
if order_id not in orders:
|
||||||
@@ -59,6 +76,11 @@ def update_order(order_id: int, new_amount: float, authorization: Optional[str]
|
|||||||
|
|
||||||
@app.post("/admin/backup")
|
@app.post("/admin/backup")
|
||||||
def create_backup(host: str, authorization: Optional[str] = Header(None)):
|
def create_backup(host: str, authorization: Optional[str] = Header(None)):
|
||||||
|
"""
|
||||||
|
Служебный эндпоинт для создания бэкапа данных.
|
||||||
|
Выполняет rsync-копирование orders.py на указанный хост в /backup/.
|
||||||
|
Использует GITHUB_TOKEN для уведомлений (пока захардкожен).
|
||||||
|
"""
|
||||||
if authorization is None:
|
if authorization is None:
|
||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user