Пиздец очередной
This commit is contained in:
72
app/routes/subscription_routes.py
Normal file
72
app/routes/subscription_routes.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from fastapi import APIRouter, HTTPException, Depends
|
||||
from pydantic import BaseModel
|
||||
from app.services.db_manager import DatabaseManager
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# DatabaseManager должен передаваться через Depends
|
||||
def get_database_manager():
|
||||
# Здесь должна быть логика инициализации DatabaseManager
|
||||
return DatabaseManager()
|
||||
|
||||
# Схемы запросов и ответов
|
||||
class BuySubscriptionRequest(BaseModel):
|
||||
telegram_id: int
|
||||
plan_id: str
|
||||
|
||||
class SubscriptionResponse(BaseModel):
|
||||
id: str
|
||||
plan: str
|
||||
vpn_server_id: str
|
||||
expiry_date: str
|
||||
created_at: str
|
||||
updated_at: str
|
||||
|
||||
# Эндпоинт для покупки подписки
|
||||
@router.post("/subscription/buy", response_model=dict)
|
||||
async def buy_subscription(
|
||||
request_data: BuySubscriptionRequest,
|
||||
database_manager: DatabaseManager = Depends(get_database_manager)
|
||||
):
|
||||
"""
|
||||
Покупка подписки.
|
||||
"""
|
||||
try:
|
||||
result = await database_manager.buy_sub(request_data.telegram_id, request_data.plan_id)
|
||||
|
||||
if result == "ERROR":
|
||||
raise HTTPException(status_code=500, detail="Failed to buy subscription")
|
||||
elif result == "INSUFFICIENT_FUNDS":
|
||||
raise HTTPException(status_code=400, detail="Insufficient funds")
|
||||
|
||||
return {"message": "Subscription purchased successfully"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
# Эндпоинт для получения последней подписки
|
||||
@router.get("/subscription/{user_id}/last", response_model=list[SubscriptionResponse])
|
||||
async def last_subscription(
|
||||
user_id: int,
|
||||
database_manager: DatabaseManager = Depends(get_database_manager)
|
||||
):
|
||||
"""
|
||||
Получение последней подписки пользователя.
|
||||
"""
|
||||
try:
|
||||
subscriptions = await database_manager.last_subscription(user_id)
|
||||
if subscriptions == "ERROR":
|
||||
raise HTTPException(status_code=500, detail="Failed to fetch subscriptions")
|
||||
|
||||
return [
|
||||
{
|
||||
"id": sub.id,
|
||||
"plan": sub.plan,
|
||||
"vpn_server_id": sub.vpn_server_id,
|
||||
"expiry_date": sub.expiry_date.isoformat(),
|
||||
"created_at": sub.created_at.isoformat(),
|
||||
"updated_at": sub.updated_at.isoformat(),
|
||||
} for sub in subscriptions
|
||||
]
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
Reference in New Issue
Block a user