feat: add limit/pagination to stratgame (games) endpoint (#138)

Closes #138

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Cal Corum 2026-03-24 05:32:28 -05:00
parent 6a217f97ee
commit 4f693b1228

View File

@ -8,10 +8,7 @@ from ..db_engine import StratGame, model_to_dict, fn
from ..dependencies import oauth2_scheme, valid_token from ..dependencies import oauth2_scheme, valid_token
router = APIRouter( router = APIRouter(prefix="/api/v2/games", tags=["games"])
prefix='/api/v2/games',
tags=['games']
)
class GameModel(pydantic.BaseModel): class GameModel(pydantic.BaseModel):
@ -35,13 +32,22 @@ class GameList(pydantic.BaseModel):
games: List[GameModel] games: List[GameModel]
@router.get('') @router.get("")
async def get_games( async def get_games(
season: list = Query(default=None), forfeit: Optional[bool] = None, away_team_id: list = Query(default=None), season: list = Query(default=None),
home_team_id: list = Query(default=None), team1_id: list = Query(default=None), forfeit: Optional[bool] = None,
team2_id: list = Query(default=None), game_type: list = Query(default=None), ranked: Optional[bool] = None, away_team_id: list = Query(default=None),
short_game: Optional[bool] = None, csv: Optional[bool] = False, short_output: bool = False, home_team_id: list = Query(default=None),
gauntlet_id: Optional[int] = None): team1_id: list = Query(default=None),
team2_id: list = Query(default=None),
game_type: list = Query(default=None),
ranked: Optional[bool] = None,
short_game: Optional[bool] = None,
csv: Optional[bool] = False,
short_output: bool = False,
gauntlet_id: Optional[int] = None,
limit: int = 100,
):
all_games = StratGame.select().order_by(StratGame.id) all_games = StratGame.select().order_by(StratGame.id)
if season is not None: if season is not None:
@ -68,49 +74,70 @@ async def get_games(
if short_game is not None: if short_game is not None:
all_games = all_games.where(StratGame.short_game == short_game) all_games = all_games.where(StratGame.short_game == short_game)
if gauntlet_id is not None: if gauntlet_id is not None:
all_games = all_games.where(StratGame.game_type.contains(f'gauntlet-{gauntlet_id}')) all_games = all_games.where(
StratGame.game_type.contains(f"gauntlet-{gauntlet_id}")
)
all_games = all_games.limit(max(0, min(limit, 500)))
if csv: if csv:
return_vals = [model_to_dict(x) for x in all_games] return_vals = [model_to_dict(x) for x in all_games]
for x in return_vals: for x in return_vals:
x['away_abbrev'] = x['away_team']['abbrev'] x["away_abbrev"] = x["away_team"]["abbrev"]
x['home_abbrev'] = x['home_team']['abbrev'] x["home_abbrev"] = x["home_team"]["abbrev"]
del x['away_team'], x['home_team'] del x["away_team"], x["home_team"]
output = pd.DataFrame(return_vals)[[ output = pd.DataFrame(return_vals)[
'id', 'away_abbrev', 'home_abbrev', 'away_score', 'home_score', 'away_team_value', 'home_team_value', [
'game_type', 'season', 'week', 'short_game', 'ranked' "id",
]] "away_abbrev",
"home_abbrev",
"away_score",
"home_score",
"away_team_value",
"home_team_value",
"game_type",
"season",
"week",
"short_game",
"ranked",
]
]
return Response(content=output.to_csv(index=False), media_type='text/csv') return Response(content=output.to_csv(index=False), media_type="text/csv")
return_val = {'count': all_games.count(), 'games': [ return_val = {
model_to_dict(x, recurse=not short_output) for x in all_games "count": all_games.count(),
]} "games": [model_to_dict(x, recurse=not short_output) for x in all_games],
}
return return_val return return_val
@router.get('/{game_id}') @router.get("/{game_id}")
async def get_one_game(game_id: int): async def get_one_game(game_id: int):
this_game = StratGame.get_or_none(StratGame.id == game_id) this_game = StratGame.get_or_none(StratGame.id == game_id)
if not this_game: if not this_game:
raise HTTPException(status_code=404, detail=f'StratGame ID {game_id} not found') raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
g_result = model_to_dict(this_game) g_result = model_to_dict(this_game)
return g_result return g_result
@router.patch('/{game_id}') @router.patch("/{game_id}")
async def patch_game( async def patch_game(
game_id: int, game_type: Optional[str] = None, away_score: Optional[int] = None, game_id: int,
home_score: Optional[int] = None, token: str = Depends(oauth2_scheme)): game_type: Optional[str] = None,
away_score: Optional[int] = None,
home_score: Optional[int] = None,
token: str = Depends(oauth2_scheme),
):
if not valid_token(token): if not valid_token(token):
logging.warning('patch_game - Bad Token: [REDACTED]') logging.warning("patch_game - Bad Token: [REDACTED]")
raise HTTPException(status_code=401, detail='Unauthorized') raise HTTPException(status_code=401, detail="Unauthorized")
this_game = StratGame.get_or_none(StratGame.id == game_id) this_game = StratGame.get_or_none(StratGame.id == game_id)
if not this_game: if not this_game:
raise HTTPException(status_code=404, detail=f'StratGame ID {game_id} not found') raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
if away_score is not None: if away_score is not None:
this_game.away_score = away_score this_game.away_score = away_score
@ -123,14 +150,14 @@ async def patch_game(
g_result = model_to_dict(this_game) g_result = model_to_dict(this_game)
return g_result return g_result
else: else:
raise HTTPException(status_code=500, detail=f'Unable to patch game {game_id}') raise HTTPException(status_code=500, detail=f"Unable to patch game {game_id}")
@router.post('') @router.post("")
async def post_game(this_game: GameModel, token: str = Depends(oauth2_scheme)): async def post_game(this_game: GameModel, token: str = Depends(oauth2_scheme)):
if not valid_token(token): if not valid_token(token):
logging.warning('post_games - Bad Token: [REDACTED]') logging.warning("post_games - Bad Token: [REDACTED]")
raise HTTPException(status_code=401, detail='Unauthorized') raise HTTPException(status_code=401, detail="Unauthorized")
this_game = StratGame(**this_game.dict()) this_game = StratGame(**this_game.dict())
@ -141,25 +168,25 @@ async def post_game(this_game: GameModel, token: str = Depends(oauth2_scheme)):
else: else:
raise HTTPException( raise HTTPException(
status_code=418, status_code=418,
detail='Well slap my ass and call me a teapot; I could not save that game' detail="Well slap my ass and call me a teapot; I could not save that game",
) )
@router.delete('/{game_id}') @router.delete("/{game_id}")
async def delete_game(game_id: int, token: str = Depends(oauth2_scheme)): async def delete_game(game_id: int, token: str = Depends(oauth2_scheme)):
if not valid_token(token): if not valid_token(token):
logging.warning('delete_game - Bad Token: [REDACTED]') logging.warning("delete_game - Bad Token: [REDACTED]")
raise HTTPException(status_code=401, detail='Unauthorized') raise HTTPException(status_code=401, detail="Unauthorized")
this_game = StratGame.get_or_none(StratGame.id == game_id) this_game = StratGame.get_or_none(StratGame.id == game_id)
if not this_game: if not this_game:
raise HTTPException(status_code=404, detail=f'StratGame ID {game_id} not found') raise HTTPException(status_code=404, detail=f"StratGame ID {game_id} not found")
count = this_game.delete_instance() count = this_game.delete_instance()
if count == 1: if count == 1:
return f'StratGame {game_id} has been deleted' return f"StratGame {game_id} has been deleted"
else: else:
raise HTTPException(status_code=500, detail=f'StratGame {game_id} could not be deleted') raise HTTPException(
status_code=500, detail=f"StratGame {game_id} could not be deleted"
)