Add managers to /games
This commit is contained in:
parent
2d235eb6f0
commit
cc3ff1d071
@ -1846,6 +1846,8 @@ class StratGame(BaseModel):
|
||||
home_team = ForeignKeyField(Team)
|
||||
away_score = IntegerField(null=True)
|
||||
home_score = IntegerField(null=True)
|
||||
away_manager = ForeignKeyField(Manager, null=True)
|
||||
home_manager = ForeignKeyField(Manager, null=True)
|
||||
|
||||
|
||||
class StratPlay(BaseModel):
|
||||
|
||||
@ -28,6 +28,8 @@ class GameModel(pydantic.BaseModel):
|
||||
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
|
||||
|
||||
|
||||
class GameList(pydantic.BaseModel):
|
||||
@ -40,6 +42,8 @@ async def get_games(
|
||||
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),
|
||||
short_output: Optional[bool] = False):
|
||||
all_games = StratGame.select()
|
||||
|
||||
@ -67,8 +71,22 @@ async def get_games(
|
||||
all_games = all_games.where(
|
||||
(StratGame.away_team_id << team2_id) | (StratGame.home_team_id << team2_id)
|
||||
)
|
||||
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(played))
|
||||
if game_num is not None:
|
||||
all_games = all_games.where(StratGame.game_num << game_num)
|
||||
|
||||
return_games = {
|
||||
'count': all_games.count(),
|
||||
@ -78,10 +96,23 @@ async def get_games(
|
||||
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, token: str = Depends(oauth2_scheme)):
|
||||
home_score: Optional[int] = None, away_manager_id: Optional[int] = None, home_manager_id: Optional[int] = None,
|
||||
token: str = Depends(oauth2_scheme)):
|
||||
if not valid_token(token):
|
||||
logging.warning(f'patch_game - Bad Token: {token}')
|
||||
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||
@ -92,20 +123,15 @@ async def patch_game(
|
||||
raise HTTPException(status_code=404, detail=f'StratGame ID {game_id} not found')
|
||||
|
||||
if game_num is not None:
|
||||
if not game_num:
|
||||
this_game.game_num = None
|
||||
else:
|
||||
this_game.game_num = game_num
|
||||
this_game.game_num = game_num
|
||||
if away_score is not None:
|
||||
if not away_score:
|
||||
this_game.away_score = None
|
||||
else:
|
||||
this_game.away_score = away_score
|
||||
this_game.away_score = away_score
|
||||
if home_score is not None:
|
||||
if not home_score:
|
||||
this_game.home_score = None
|
||||
else:
|
||||
this_game.home_score = home_score
|
||||
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 this_game.save() == 1:
|
||||
g_result = model_to_dict(this_game)
|
||||
@ -153,6 +179,8 @@ async def wipe_game(game_id: int, token: str = Depends(oauth2_scheme)):
|
||||
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)
|
||||
|
||||
43
migrations.py
Normal file
43
migrations.py
Normal file
@ -0,0 +1,43 @@
|
||||
from playhouse.migrate import *
|
||||
import app.db_engine as db_engine
|
||||
|
||||
migrator = SqliteMigrator(db_engine.db)
|
||||
|
||||
|
||||
# pubdate_field = DateTimeField(null=True)
|
||||
# comment_field = TextField(default='')
|
||||
|
||||
|
||||
# pitcher_injury = IntegerField(null=True)
|
||||
# pos_1 = CharField(default='None')
|
||||
# pos_2 = CharField(null=True)
|
||||
# last_game = CharField(null=True)
|
||||
# last_game2 = CharField(null=True)
|
||||
# division = ForeignKeyField(db_engine.Division, field=db_engine.Division.id, null=True) # for division migration
|
||||
# manager = ForeignKeyField(db_engine.Manager, field=db_engine.Manager.id, null=True) # for manager table
|
||||
# p_career = ForeignKeyField(db_engine.PitchingCareer, field=db_engine.PitchingCareer.id, null=True) # for careers
|
||||
# b_career = ForeignKeyField(db_engine.BattingCareer, field=db_engine.BattingCareer.id, null=True) # for careers
|
||||
# f_career = ForeignKeyField(db_engine.FieldingCareer, field=db_engine.FieldingCareer.id, null=True) # for careers
|
||||
away_manager = ForeignKeyField(db_engine.Manager, field=db_engine.Manager.id, null=True) # to add Manager to games
|
||||
home_manager = ForeignKeyField(db_engine.Manager, field=db_engine.Manager.id, null=True) # to add Manager to games
|
||||
|
||||
migrate(
|
||||
# migrator.add_column('team', 'division_id', division), # for division migration
|
||||
# migrator.add_column('team', 'manager1_id', manager), # for manager table
|
||||
# migrator.add_column('team', 'manager2_id', manager), # for manager table
|
||||
# migrator.add_column('battingseason', 'career_id', b_career), # for career stats
|
||||
# migrator.add_column('pitchingseason', 'career_id', p_career), # for career stats
|
||||
# migrator.add_column('fieldingseason', 'career_id', f_career), # for career stats
|
||||
# migrator.add_column('player', 'last_game2', last_game2),
|
||||
# migrator.add_column('player', 'pos_1', pos_1),
|
||||
# migrator.add_column('comment_tbl', 'comment', comment_field),
|
||||
# migrator.rename_column('team', 'division', 'division_legacy'),
|
||||
# migrator.drop_column('story', 'some_old_field'),
|
||||
# migrator.drop_not_null('team', 'abbrev'),
|
||||
# migrator.add_not_null('story', 'modified_date'),
|
||||
# migrator.rename_table('story', 'stories_tbl'),
|
||||
# migrator.drop_index('team', 'team_abbrev'),
|
||||
# migrator.drop_index('player', 'player_name')
|
||||
# migrator.add_column('stratgame', 'away_manager_id', away_manager),
|
||||
migrator.add_column('stratgame', 'home_manager_id', home_manager)
|
||||
)
|
||||
Loading…
Reference in New Issue
Block a user