Files
crypto_trader/backend/api/exchanges.py

43 lines
1.4 KiB
Python
Raw Normal View History

"""Exchange API endpoints."""
from typing import List
from fastapi import APIRouter, HTTPException
from sqlalchemy import select
from ..core.schemas import ExchangeResponse
from src.core.database import Exchange, get_database
router = APIRouter()
@router.get("/", response_model=List[ExchangeResponse])
async def list_exchanges():
"""List all exchanges."""
try:
db = get_database()
async with db.get_session() as session:
stmt = select(Exchange).order_by(Exchange.name)
result = await session.execute(stmt)
exchanges = result.scalars().all()
return [ExchangeResponse.model_validate(e) for e in exchanges]
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@router.get("/{exchange_id}", response_model=ExchangeResponse)
async def get_exchange(exchange_id: int):
"""Get exchange by ID."""
try:
db = get_database()
async with db.get_session() as session:
stmt = select(Exchange).where(Exchange.id == exchange_id)
result = await session.execute(stmt)
exchange = result.scalar_one_or_none()
if not exchange:
raise HTTPException(status_code=404, detail="Exchange not found")
return ExchangeResponse.model_validate(exchange)
except HTTPException:
raise
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))