from fastapi import APIRouter from app.services.exchange import ExchangeRateService from datetime import datetime router = APIRouter() service = ExchangeRateService() @router.get("/convert") async def convert_currency( from_currency: str = "USD", to_currency: str = "CNY", amount: float = 1.0, ): rate = await service.get_rate(from_currency, to_currency) if rate is None: return {"error": f"No rate available for {from_currency} -> {to_currency}"} return { "from_currency": from_currency.upper(), "to_currency": to_currency.upper(), "amount": amount, "converted": round(amount * rate, 2), "rate": rate, "updated_at": datetime.utcnow().isoformat(), } @router.get("/rates") async def get_rates(base: str = "USD"): rates = await service.get_all_rates(base) return { "base": base.upper(), "rates": rates, "updated_at": datetime.utcnow().isoformat(), }