Пиздец очередной
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
from .payment_routes import router as payment_router
|
||||
from .user_routes import router as user_router
|
||||
from .subscription_routes import router as subscription_router
|
||||
|
||||
# Экспорт всех маршрутов
|
||||
__all__ = ["payment_router", "user_router", "subscription_router"]
|
||||
|
||||
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))
|
||||
@@ -0,0 +1,68 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from app.services.db_manager import DatabaseManager
|
||||
from instance.configdb import get_database_manager
|
||||
from pydantic import BaseModel
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
# Модели запросов и ответов
|
||||
class CreateUserRequest(BaseModel):
|
||||
telegram_id: int
|
||||
|
||||
class UserResponse(BaseModel):
|
||||
id: str
|
||||
telegram_id: int
|
||||
username: str
|
||||
balance: float
|
||||
created_at: str
|
||||
updated_at: str
|
||||
|
||||
|
||||
@router.post("/user/create", response_model=UserResponse, summary="Создать пользователя")
|
||||
async def create_user(
|
||||
request: CreateUserRequest,
|
||||
db_manager: DatabaseManager = Depends(get_database_manager)
|
||||
):
|
||||
"""
|
||||
Создание пользователя через Telegram ID.
|
||||
"""
|
||||
try:
|
||||
user = await db_manager.create_user(request.telegram_id)
|
||||
if user == "ERROR":
|
||||
raise HTTPException(status_code=500, detail="Failed to create user")
|
||||
|
||||
return UserResponse(
|
||||
id=user.id,
|
||||
telegram_id=user.telegram_id,
|
||||
username=user.username,
|
||||
balance=user.balance,
|
||||
created_at=user.created_at.isoformat(),
|
||||
updated_at=user.updated_at.isoformat()
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
|
||||
@router.get("/user/{telegram_id}", response_model=UserResponse, summary="Получить информацию о пользователе")
|
||||
async def get_user(
|
||||
telegram_id: int,
|
||||
db_manager: DatabaseManager = Depends(get_database_manager)
|
||||
):
|
||||
"""
|
||||
Получение информации о пользователе.
|
||||
"""
|
||||
try:
|
||||
user = await db_manager.get_user_by_telegram_id(telegram_id)
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
return UserResponse(
|
||||
id=user.id,
|
||||
telegram_id=user.telegram_id,
|
||||
username=user.username,
|
||||
balance=user.balance,
|
||||
created_at=user.created_at.isoformat(),
|
||||
updated_at=user.updated_at.isoformat()
|
||||
)
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
Reference in New Issue
Block a user