65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from typing import List, Optional
|
|
import logging
|
|
import pydantic
|
|
|
|
from ..db_engine import db, Injury, Current, model_to_dict, 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/injuries',
|
|
tags=['injuries']
|
|
)
|
|
|
|
|
|
class InjuryModel(pydantic.BaseModel):
|
|
season: int
|
|
player_id: int
|
|
total_games: int
|
|
start_week: int
|
|
start_game: int
|
|
end_week: int
|
|
end_game: int
|
|
is_active: bool = True
|
|
|
|
|
|
@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')
|
|
all_injuries = Injury.select()
|
|
|
|
if season is not None:
|
|
all_injuries = all_injuries.where(Injury.season << season)
|
|
if player_id is not None:
|
|
all_injuries = all_injuries.where(Injury.player_id << player_id)
|
|
if min_games is not None:
|
|
all_injuries = all_injuries.where(Injury.total_games >= min_games)
|
|
if max_games is not None:
|
|
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)
|
|
|
|
return_injuries = {
|
|
'count': all_injuries.count(),
|
|
'injuries': [model_to_dict(x, recurse=not short_output) for x in all_injuries]
|
|
}
|
|
db.close()
|
|
return return_injuries
|
|
|
|
|
|
@router.patch('/{injury_id}')
|
|
async def patch_injury(injury_id: int, is_active: Optional[bool] = None, token: str = Depends(oauth2_scheme)):
|
|
if not valid_token(token):
|
|
logging.warning(f'patch_injury - Bad Token: {token}')
|
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
|
|
|
|