Spaces:
Running
Running
| """API routes for watchlist and FR history.""" | |
| from fastapi import APIRouter, HTTPException | |
| from app.services import get_watchlist_service, get_history_service | |
| from app.models import ( | |
| WatchlistResponse, | |
| AddWatchlistRequest, | |
| WatchlistItem, | |
| FRHistoryResponse, | |
| ) | |
| router = APIRouter(prefix="/watchlist", tags=["Watchlist"]) | |
| async def get_watchlist(): | |
| """ | |
| Get all watched coins with current FR data. | |
| """ | |
| service = get_watchlist_service() | |
| data = await service.get_watchlist() | |
| return WatchlistResponse( | |
| success=True, | |
| data=data, | |
| count=len(data), | |
| ) | |
| async def add_to_watchlist(request: AddWatchlistRequest): | |
| """ | |
| Add a coin to watchlist. | |
| - **symbol**: Trading pair (e.g., BTC, BTC_USDT, BTCUSDT) | |
| - **notes**: Optional notes about this coin | |
| """ | |
| service = get_watchlist_service() | |
| try: | |
| item = await service.add_to_watchlist(request.symbol, request.notes) | |
| return item | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| async def remove_from_watchlist(symbol: str): | |
| """ | |
| Remove a coin from watchlist. | |
| """ | |
| service = get_watchlist_service() | |
| removed = await service.remove_from_watchlist(symbol) | |
| if not removed: | |
| raise HTTPException(status_code=404, detail="Symbol not found in watchlist") | |
| return {"success": True, "message": f"Removed {symbol} from watchlist"} | |
| async def get_fr_history(symbol: str, hours: int = 24): | |
| """ | |
| Get FR history for a watched coin. | |
| - **symbol**: Trading pair symbol | |
| - **hours**: Number of hours to look back (default: 24) | |
| """ | |
| service = get_history_service() | |
| data = await service.get_history(symbol, hours=hours) | |
| return FRHistoryResponse( | |
| success=True, | |
| symbol=symbol.upper(), | |
| data=data, | |
| count=len(data), | |
| ) | |