226 lines
8.2 KiB
Python
226 lines
8.2 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from typing import List, Optional, Literal
|
|
import copy
|
|
import logging
|
|
import pydantic
|
|
|
|
from ..db_engine import db, StratGame, Team, model_to_dict, chunked, fn
|
|
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
|
|
|
|
logging.basicConfig(
|
|
filename=LOG_DATA['filename'],
|
|
format=LOG_DATA['format'],
|
|
level=LOG_DATA['log_level']
|
|
)
|
|
|
|
router = APIRouter(
|
|
prefix='/api/v3/games',
|
|
tags=['games']
|
|
)
|
|
|
|
|
|
class GameModel(pydantic.BaseModel):
|
|
season: int
|
|
week: int
|
|
game_num: Optional[int] = None
|
|
season_type: Optional[str] = 'regular'
|
|
away_team_id: int
|
|
home_team_id: int
|
|
away_score: Optional[int] = None
|
|
home_score: Optional[int] = None
|
|
away_manager_id: Optional[int] = None
|
|
home_manager_id: Optional[int] = None
|
|
scorecard_url: Optional[str] = None
|
|
|
|
|
|
class GameList(pydantic.BaseModel):
|
|
games: List[GameModel]
|
|
|
|
|
|
@router.get('')
|
|
async def get_games(
|
|
season: list = Query(default=None), week: list = Query(default=None), game_num: list = Query(default=None),
|
|
season_type: Literal['regular', 'post', 'all'] = 'all', away_team_id: list = Query(default=None),
|
|
home_team_id: list = Query(default=None), week_start: Optional[int] = None, week_end: Optional[int] = None,
|
|
team1_id: list = Query(default=None), team2_id: list = Query(default=None), played: Optional[bool] = None,
|
|
away_manager_id: list = Query(default=None), home_manager_id: list = Query(default=None),
|
|
manager1_id: list = Query(default=None), manager2_id: list = Query(default=None),
|
|
division_id: Optional[int] = None, short_output: Optional[bool] = False, sort: Optional[str] = None):
|
|
all_games = StratGame.select()
|
|
|
|
if season is not None:
|
|
all_games = all_games.where(StratGame.season << season)
|
|
if week is not None:
|
|
all_games = all_games.where(StratGame.week << week)
|
|
if game_num is not None:
|
|
all_games = all_games.where(StratGame.game_num << game_num)
|
|
if season_type != 'all':
|
|
all_games = all_games.where(StratGame.season_type == season_type)
|
|
if away_team_id is not None:
|
|
all_games = all_games.where(StratGame.away_team_id << away_team_id)
|
|
if home_team_id is not None:
|
|
all_games = all_games.where(StratGame.home_team_id << home_team_id)
|
|
if week_start is not None:
|
|
all_games = all_games.where(StratGame.week >= week_start)
|
|
if week_end is not None:
|
|
all_games = all_games.where(StratGame.week <= week_end)
|
|
if team1_id is not None:
|
|
all_games = all_games.where(
|
|
(StratGame.away_team_id << team1_id) | (StratGame.home_team_id << team1_id)
|
|
)
|
|
if team2_id is not None:
|
|
all_games = all_games.where(
|
|
(StratGame.away_team_id << team2_id) | (StratGame.home_team_id << team2_id)
|
|
)
|
|
if division_id is not None:
|
|
all_teams = Team.select().where(Team.division == division_id)
|
|
all_games = all_games.where(
|
|
(StratGame.away_team << all_teams) | (StratGame.home_team << all_teams)
|
|
)
|
|
if away_manager_id is not None:
|
|
all_games = all_games.where(StratGame.away_manager_id << away_manager_id)
|
|
if home_manager_id is not None:
|
|
all_games = all_games.where(StratGame.home_manager_id << home_manager_id)
|
|
if manager1_id is not None:
|
|
all_games = all_games.where(
|
|
(StratGame.away_manager_id << manager1_id) | (StratGame.home_manager_id << manager1_id)
|
|
)
|
|
if manager2_id is not None:
|
|
all_games = all_games.where(
|
|
(StratGame.away_manager_id << manager2_id) | (StratGame.home_manager_id << manager2_id)
|
|
)
|
|
if played is not None:
|
|
all_games = all_games.where(StratGame.game_num.is_null(not played))
|
|
if game_num is not None:
|
|
all_games = all_games.where(StratGame.game_num << game_num)
|
|
|
|
if sort == 'recent-first':
|
|
all_games.order_by(-StratGame.season, -StratGame.week, -StratGame.game_num)
|
|
else:
|
|
all_games.order_by(StratGame.season, StratGame.week, StratGame.game_num)
|
|
|
|
return_games = {
|
|
'count': all_games.count(),
|
|
'games': [model_to_dict(x, recurse=not short_output) for x in all_games]
|
|
}
|
|
db.close()
|
|
return return_games
|
|
|
|
|
|
@router.get('/{game_id}')
|
|
async def get_one_game(game_id: int):
|
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
|
if not this_game:
|
|
db.close()
|
|
raise HTTPException(status_code=404, detail=f'StratGame ID {game_id} not found')
|
|
|
|
g_result = model_to_dict(this_game)
|
|
db.close()
|
|
return g_result
|
|
|
|
|
|
@router.patch('/{game_id}')
|
|
async def patch_game(
|
|
game_id: int, game_num: Optional[int] = None, away_score: Optional[int] = None,
|
|
home_score: Optional[int] = None, away_manager_id: Optional[int] = None, home_manager_id: Optional[int] = None,
|
|
token: str = Depends(oauth2_scheme), scorecard_url: Optional[str] = None):
|
|
if not valid_token(token):
|
|
logging.warning(f'patch_game - Bad Token: {token}')
|
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
|
|
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
|
if not this_game:
|
|
db.close()
|
|
raise HTTPException(status_code=404, detail=f'StratGame ID {game_id} not found')
|
|
|
|
if game_num is not None:
|
|
this_game.game_num = game_num
|
|
if away_score is not None:
|
|
this_game.away_score = away_score
|
|
if home_score is not None:
|
|
this_game.home_score = home_score
|
|
if away_manager_id is not None:
|
|
this_game.away_manager_id = away_manager_id
|
|
if home_manager_id is not None:
|
|
this_game.home_manager_id = home_manager_id
|
|
if scorecard_url is not None:
|
|
this_game.scorecard_url = scorecard_url
|
|
|
|
if this_game.save() == 1:
|
|
g_result = model_to_dict(this_game)
|
|
db.close()
|
|
return g_result
|
|
else:
|
|
db.close()
|
|
raise HTTPException(status_code=500, detail=f'Unable to patch game {game_id}')
|
|
|
|
|
|
@router.post('')
|
|
async def post_games(game_list: GameList, token: str = Depends(oauth2_scheme)):
|
|
if not valid_token(token):
|
|
logging.warning(f'post_games - Bad Token: {token}')
|
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
|
|
|
new_games = []
|
|
for x in game_list.games:
|
|
if Team.get_or_none(Team.id == x.away_team_id) is None:
|
|
raise HTTPException(status_code=404, detail=f'Team ID {x.away_team_id} not found')
|
|
if Team.get_or_none(Team.id == x.home_team_id) is None:
|
|
raise HTTPException(status_code=404, detail=f'Team ID {x.home_team_id} not found')
|
|
|
|
new_games.append(x.dict())
|
|
|
|
with db.atomic():
|
|
for batch in chunked(new_games, 16):
|
|
StratGame.insert_many(batch).on_conflict_replace().execute()
|
|
db.close()
|
|
|
|
return f'Inserted {len(new_games)} games'
|
|
|
|
|
|
@router.post('/wipe/{game_id}')
|
|
async def wipe_game(game_id: int, token: str = Depends(oauth2_scheme)):
|
|
if not valid_token(token):
|
|
logging.warning(f'wipe_game - Bad Token: {token}')
|
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
|
|
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
|
if not this_game:
|
|
db.close()
|
|
raise HTTPException(status_code=404, detail=f'StratGame ID {game_id} not found')
|
|
|
|
this_game.away_score = None
|
|
this_game.home_score = None
|
|
this_game.game_num = None
|
|
this_game.away_manager = None
|
|
this_game.home_manager = None
|
|
|
|
if this_game.save() == 1:
|
|
g_result = model_to_dict(this_game)
|
|
db.close()
|
|
return g_result
|
|
else:
|
|
db.close()
|
|
raise HTTPException(status_code=500, detail=f'Unable to wipe game {game_id}')
|
|
|
|
|
|
@router.delete('/{game_id}')
|
|
async def delete_game(game_id: int, token: str = Depends(oauth2_scheme)):
|
|
if not valid_token(token):
|
|
logging.warning(f'delete_game - Bad Token: {token}')
|
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
|
|
|
this_game = StratGame.get_or_none(StratGame.id == game_id)
|
|
if not this_game:
|
|
db.close()
|
|
raise HTTPException(status_code=404, detail=f'StratGame ID {game_id} not found')
|
|
|
|
count = this_game.delete_instance()
|
|
db.close()
|
|
|
|
if count == 1:
|
|
return f'StratGame {game_id} has been deleted'
|
|
else:
|
|
raise HTTPException(status_code=500, detail=f'StratGame {game_id} could not be deleted')
|
|
|