major-domo-database/app/routers_v3/stratplay/common.py
Cal Corum 86f8495284
All checks were successful
Build Docker Image / build (pull_request) Successful in 2m32s
feat: Add group_by=sbaplayer to batting, pitching, and fielding endpoints
Enables career-total aggregation by real-world player identity (SbaPlayer)
across all seasons. JOINs StratPlay → Player to access Player.sbaplayer FK,
groups by that FK, and excludes players with null sbaplayer. Also refactors
stratplay router from single file into package and adds integration tests.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 17:42:14 -06:00

38 lines
1.5 KiB
Python

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