from fastapi import HTTPException from ...db_engine import StratGame def build_season_games(season, week, s_type, week_start, week_end, manager_id=None): """Build the filtered StratGame subquery used by all stats endpoints.""" season_games = StratGame.select() if season is not None: season_games = season_games.where(StratGame.season << season) if week is not None and s_type is not None: raise HTTPException( status_code=400, detail="Week and s_type parameters cannot be used in the same query", ) if week is not None and (week_start is not None or week_end is not None): raise HTTPException( status_code=400, detail="Week and week_start/week_end parameters cannot be used in the same query", ) if week is not None: season_games = season_games.where(StratGame.week << week) if week_start is not None: season_games = season_games.where(StratGame.week >= week_start) if week_end is not None: season_games = season_games.where(StratGame.week <= week_end) if s_type is not None: if s_type == "regular": season_games = season_games.where(StratGame.week <= 18) elif s_type == "post": season_games = season_games.where(StratGame.week > 18) if manager_id is not None: season_games = season_games.where( (StratGame.away_manager_id << manager_id) | (StratGame.home_manager_id << manager_id) ) return season_games