Update players.py

Migrate patch players to put and add new patch players endpoint
This commit is contained in:
Cal Corum 2024-07-03 11:19:39 -05:00
parent 0d13fc939f
commit a935688518

View File

@ -133,8 +133,8 @@ async def get_one_player(player_id: int, short_output: Optional[bool] = False):
return r_player
@router.patch('/{player_id}')
async def patch_player(
@router.put('/{player_id}')
async def put_player(
player_id: int, new_player: PlayerModel, token: str = Depends(oauth2_scheme)):
if not valid_token(token):
logging.warning(f'patch_player - Bad Token: {token}')
@ -150,6 +150,86 @@ async def patch_player(
return r_player
@router.patch('/{player_id}')
async def patch_player(
player_id: int, token: str = Depends(oauth2_scheme), name: Optional[str] = None,
wara: Optional[float] = None, image: Optional[str] = None, image2: Optional[str] = None,
team_id: Optional[int] = None, season: Optional[int] = None, pos_1: Optional[str] = None,
pos_2: Optional[str] = None, pos_3: Optional[str] = None, pos_4: Optional[str] = None,
pos_5: Optional[str] = None, pos_6: Optional[str] = None, pos_7: Optional[str] = None,
pos_8: Optional[str] = None, vanity_card: Optional[str] = None, headshot: Optional[str] = None,
il_return: Optional[str] = None, demotion_week: Optional[int] = None, strat_code: Optional[str] = None,
bbref_id: Optional[str] = None, injury_rating: Optional[str] = None):
if not valid_token(token):
logging.warning(f'patch_player - Bad Token: {token}')
raise HTTPException(status_code=401, detail='Unauthorized')
if Player.get_or_none(Player.id == player_id) is None:
db.close()
raise HTTPException(status_code=404, detail=f'Player ID {player_id} not found')
this_player = Player.get_or_none(Player.id == player_id)
if this_player is None:
db.close()
raise HTTPException(status_code=404, detail=f'Player ID {player_id} not found')
if name is not None:
this_player.name = name
if wara is not None:
this_player.wara = wara
if image is not None:
this_player.image = image
if image2 is not None:
this_player.image2 = image2
if team_id is not None:
this_player.team_id = team_id
if season is not None:
this_player.season = season
if pos_1 is not None:
this_player.pos_1 = pos_1
if pos_2 is not None:
this_player.pos_2 = pos_2
if pos_3 is not None:
this_player.pos_3 = pos_3
if pos_4 is not None:
this_player.pos_4 = pos_4
if pos_5 is not None:
this_player.pos_5 = pos_5
if pos_6 is not None:
this_player.pos_6 = pos_6
if pos_7 is not None:
this_player.pos_7 = pos_7
if pos_8 is not None:
this_player.pos_8 = pos_8
if pos_8 is not None:
this_player.pos_8 = pos_8
if vanity_card is not None:
this_player.vanity_card = vanity_card
if headshot is not None:
this_player.headshot = headshot
if il_return is not None:
this_player.il_return = il_return
if demotion_week is not None:
this_player.demotion_week = demotion_week
if strat_code is not None:
this_player.strat_code = strat_code
if bbref_id is not None:
this_player.bbref_id = bbref_id
if injury_rating is not None:
this_player.injury_rating = injury_rating
if this_player.save() == 1:
r_player = model_to_dict(this_player)
db.close()
return r_player
else:
db.close()
raise HTTPException(status_code=500, detail=f'Unable to patch player {player_id}')
@router.post('')
async def post_players(p_list: PlayerList, token: str = Depends(oauth2_scheme)):
if not valid_token(token):