/injuries fully functional

This commit is contained in:
Cal Corum 2023-07-27 23:41:57 -05:00
parent 5a5146c1cf
commit 5718ce1af9
4 changed files with 75 additions and 7 deletions

View File

@ -1915,7 +1915,6 @@ class StratPlay(BaseModel):
is_new_inning = BooleanField(default=False)
# class Streak(BaseModel):
# player = ForeignKeyField(Player)
# streak_type = CharField()
@ -1940,6 +1939,7 @@ class StratPlay(BaseModel):
db.create_tables([
Current, Division, Manager, Team, Result, Player, Schedule, Transaction, BattingStat, PitchingStat, Standings,
BattingCareer, PitchingCareer, FieldingCareer, Manager, Award, DiceRoll, DraftList, Keeper, StratGame, StratPlay
BattingCareer, PitchingCareer, FieldingCareer, Manager, Award, DiceRoll, DraftList, Keeper, StratGame, StratPlay,
Injury
])
db.close()

View File

@ -7,7 +7,8 @@ from fastapi import Depends, FastAPI, Request
# from fastapi.openapi.utils import get_openapi
from .routers_v3 import current, players, results, schedules, standings, teams, transactions, battingstats, \
pitchingstats, fieldingstats, draftpicks, draftlist, managers, awards, draftdata, keepers, stratgame, stratplay
pitchingstats, fieldingstats, draftpicks, draftlist, managers, awards, draftdata, keepers, stratgame, stratplay, \
injuries
date = f'{datetime.datetime.now().year}-{datetime.datetime.now().month}-{datetime.datetime.now().day}'
log_level = logging.INFO if os.environ.get('LOG_LEVEL') == 'INFO' else 'WARN'
@ -40,6 +41,7 @@ app.include_router(draftdata.router)
app.include_router(keepers.router)
app.include_router(stratgame.router)
app.include_router(stratplay.router)
app.include_router(injuries.router)
# @app.get("/docs", include_in_schema=False)

View File

@ -3,7 +3,7 @@ from typing import List, Optional
import logging
import pydantic
from ..db_engine import db, Injury, Current, model_to_dict, fn
from ..db_engine import db, Injury, Player, model_to_dict, fn
from ..dependencies import oauth2_scheme, valid_token, LOG_DATA
logging.basicConfig(
@ -32,8 +32,8 @@ class InjuryModel(pydantic.BaseModel):
@router.get('')
async def get_injuries(
season: list = Query(default=None), player_id: list = Query(default=None), min_games: int = None,
max_games: int = None, is_active: bool = None, short_output: bool = False):
logging.info(f'getting injuries')
max_games: int = None, team_id: list = Query(default=None), is_active: bool = None,
short_output: bool = False, sort: Optional[str] = 'start-asc'):
all_injuries = Injury.select()
if season is not None:
@ -46,6 +46,18 @@ async def get_injuries(
all_injuries = all_injuries.where(Injury.total_games <= max_games)
if is_active is not None:
all_injuries = all_injuries.where(Injury.is_active == is_active)
if team_id is not None:
all_players = Player.select().where(Player.team_id << team_id)
all_injuries = all_injuries.where(Injury.player << all_players)
if sort == 'return-asc':
all_injuries = all_injuries.order_by(Injury.end_week, Injury.end_game)
elif sort == 'return-desc':
all_injuries = all_injuries.order_by(-Injury.end_week, -Injury.end_game)
elif sort == 'start-asc':
all_injuries = all_injuries.order_by(Injury.start_week, Injury.start_game)
elif sort == 'start-desc':
all_injuries = all_injuries.order_by(-Injury.start_week, -Injury.start_game)
return_injuries = {
'count': all_injuries.count(),
@ -61,4 +73,58 @@ async def patch_injury(injury_id: int, is_active: Optional[bool] = None, token:
logging.warning(f'patch_injury - Bad Token: {token}')
raise HTTPException(status_code=401, detail='Unauthorized')
this_injury = Injury.get_or_none(Injury.id == injury_id)
if this_injury is None:
db.close()
raise HTTPException(status_code=404, detail=f'Injury ID {injury_id} not found')
if is_active is not None:
this_injury.is_active = is_active
if this_injury.save() == 1:
r_injury = model_to_dict(this_injury)
db.close()
return r_injury
else:
db.close()
raise HTTPException(status_code=500, detail=f'Unable to patch injury {injury_id}')
@router.post(f'')
async def post_injury(new_injury: InjuryModel, token: str = Depends(oauth2_scheme)):
if not valid_token(token):
logging.warning(f'post_injury - Bad Token: {token}')
raise HTTPException(status_code=401, detail='Unauthorized')
this_injury = Injury(**new_injury.dict())
if this_injury.save():
r_injury = model_to_dict(this_injury)
db.close()
return r_injury
else:
db.close()
raise HTTPException(status_code=500, detail=f'Unable to post injury')
@router.delete('/{injury_id}')
async def delete_injury(injury_id: int, token: str = Depends(oauth2_scheme)):
if not valid_token(token):
logging.warning(f'delete_injury - Bad Token: {token}')
raise HTTPException(status_code=401, detail='Unauthorized')
this_injury = Injury.get_or_none(Injury.id == injury_id)
if this_injury is None:
db.close()
raise HTTPException(status_code=404, detail=f'Injury ID {injury_id} not found')
count = this_injury.delete_instance()
db.close()
if count == 1:
return f'Injury {injury_id} has been deleted'
else:
raise HTTPException(status_code=500, detail=f'Unable to delete injury {injury_id}')

View File

@ -239,7 +239,7 @@ async def delete_plays_game(game_id: int, token: str = Depends(oauth2_scheme)):
if count > 0:
return f'Deleted {count} plays matching Game ID {game_id}'
else:
raise HTTPException(status_code=500, detail=f'Plays matching Game ID {game_id} could not be deleted')
raise HTTPException(status_code=500, detail=f'No plays matching Game ID {game_id} were deleted')