64 lines
2.2 KiB
Python
64 lines
2.2 KiB
Python
import logging
|
|
from typing import Optional
|
|
import pydantic
|
|
|
|
from api_calls.player import Player
|
|
from api_calls.team import Team
|
|
from db_calls import db_get, db_patch
|
|
from exceptions import log_exception, ApiException
|
|
|
|
logger = logging.getLogger('discord_app')
|
|
|
|
class DraftPick(pydantic.BaseModel):
|
|
id: int = 0
|
|
season: int = 0
|
|
overall: int = 0
|
|
round: int = 0
|
|
origowner: Optional[Team] = None
|
|
owner: Optional[Team] = None
|
|
player: Optional[Player] = None
|
|
|
|
|
|
async def get_one_draftpick(pick_id: Optional[int] = None, season: Optional[int] = None, overall: Optional[int] = None) -> DraftPick:
|
|
if not pick_id and not season and not overall:
|
|
log_exception(KeyError('Either pick_id or season + overall must be provided to get_one_draftpick'))
|
|
elif (season is not None and overall is None) or (season is None and overall is not None):
|
|
log_exception(KeyError('Both season and overall must be provided to get_one_draftpick'))
|
|
|
|
if pick_id is not None:
|
|
data = await db_get(f'draftpicks/{pick_id}')
|
|
if data is None:
|
|
log_exception(ApiException(f'No pick found with ID {pick_id}'))
|
|
return DraftPick(**data)
|
|
|
|
data = await db_get('draftpicks', params=[('season', season), ('overall', overall)])
|
|
if not data or data.get('count', 0) != 1 or len(data.get('picks', [])) != 1:
|
|
log_exception(ApiException(f'No pick found in season {season} with overall {overall}'))
|
|
|
|
return DraftPick(**data['picks'][0])
|
|
|
|
async def patch_draftpick(updated_pick: DraftPick) -> DraftPick:
|
|
if updated_pick.origowner is None or updated_pick.owner is None:
|
|
log_exception(ValueError('To patch draftpicks, owner and origowner may not be None'))
|
|
|
|
logger.info(f'Patching pick id {updated_pick.id}')
|
|
pick_data = updated_pick.model_dump(exclude={'player', 'origowner', 'owner'})
|
|
|
|
|
|
pick_data['origowner_id'] = updated_pick.origowner.id
|
|
pick_data['owner_id'] = updated_pick.owner.id
|
|
if updated_pick.player:
|
|
pick_data['player_id'] = updated_pick.player.id
|
|
else:
|
|
pick_data['player_id'] = None
|
|
|
|
new_pick = await db_patch(
|
|
'draftpicks',
|
|
object_id=updated_pick.id,
|
|
params=[],
|
|
payload=pick_data
|
|
)
|
|
|
|
return DraftPick(**new_pick)
|
|
|