110 lines
3.7 KiB
Python
110 lines
3.7 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
||
from app.services.db_manager import DatabaseManager
|
||
from instance.configdb import get_database_manager
|
||
from pydantic import BaseModel
|
||
from uuid import UUID
|
||
|
||
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))
|
||
|
||
@router.post("/user/{telegram_id}/balance/{amount}", summary="Обновить баланс")
|
||
async def update_balance(
|
||
telegram_id: int,
|
||
amount: float,
|
||
db_manager: DatabaseManager = Depends(get_database_manager)
|
||
):
|
||
"""
|
||
Обновляет баланс пользователя.
|
||
"""
|
||
try:
|
||
result = await db_manager.update_balance(telegram_id, amount)
|
||
if result == "ERROR":
|
||
raise HTTPException(status_code=500, detail="Failed to update balance")
|
||
return {"message": "Balance updated successfully"}
|
||
except Exception as e:
|
||
raise HTTPException(status_code=500, detail=str(e))
|
||
|
||
@router.get("/user/{user_id}/transactions", summary="Последние транзакции пользователя")
|
||
async def last_transactions(
|
||
user_id: UUID,
|
||
db_manager: DatabaseManager = Depends(get_database_manager)
|
||
):
|
||
"""
|
||
Возвращает список последних транзакций пользователя.
|
||
"""
|
||
try:
|
||
transactions = await db_manager.last_transaction(user_id)
|
||
if transactions == "ERROR":
|
||
raise HTTPException(status_code=500, detail="Failed to fetch transactions")
|
||
return [
|
||
{
|
||
"id": tx.id,
|
||
"amount": tx.amount,
|
||
"created_at": tx.created_at.isoformat(),
|
||
"transaction_type": tx.transaction_type,
|
||
} for tx in transactions
|
||
]
|
||
except Exception as e:
|
||
raise HTTPException(status_code=500, detail=str(e))
|