Added /draftlist
This commit is contained in:
parent
63491a7b62
commit
9bfa9c86dc
@ -3,7 +3,7 @@ from fastapi import Depends, FastAPI, Request
|
|||||||
# from fastapi.openapi.utils import get_openapi
|
# from fastapi.openapi.utils import get_openapi
|
||||||
|
|
||||||
from .routers_v3 import current, players, results, schedules, standings, teams, transactions, battingstats, \
|
from .routers_v3 import current, players, results, schedules, standings, teams, transactions, battingstats, \
|
||||||
pitchingstats, fieldingstats, draftpicks
|
pitchingstats, fieldingstats, draftpicks, draftlist
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
responses={404: {'description': 'Not found'}}
|
responses={404: {'description': 'Not found'}}
|
||||||
@ -21,6 +21,7 @@ app.include_router(battingstats.router)
|
|||||||
app.include_router(pitchingstats.router)
|
app.include_router(pitchingstats.router)
|
||||||
app.include_router(fieldingstats.router)
|
app.include_router(fieldingstats.router)
|
||||||
app.include_router(draftpicks.router)
|
app.include_router(draftpicks.router)
|
||||||
|
app.include_router(draftlist.router)
|
||||||
|
|
||||||
|
|
||||||
# @app.get("/docs", include_in_schema=False)
|
# @app.get("/docs", include_in_schema=False)
|
||||||
|
|||||||
82
app/routers_v3/draftlist.py
Normal file
82
app/routers_v3/draftlist.py
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
|
from typing import List, Optional
|
||||||
|
import logging
|
||||||
|
import pydantic
|
||||||
|
|
||||||
|
from ..db_engine import db, DraftList, Team, model_to_dict, chunked
|
||||||
|
from ..dependencies import oauth2_scheme, valid_token
|
||||||
|
|
||||||
|
router = APIRouter(
|
||||||
|
prefix='/api/v3/draftlist',
|
||||||
|
tags=['draftlist']
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class DraftListModel(pydantic.BaseModel):
|
||||||
|
season: int
|
||||||
|
team_id: int
|
||||||
|
rank: int
|
||||||
|
player_id: int
|
||||||
|
|
||||||
|
|
||||||
|
class DraftListList(pydantic.BaseModel):
|
||||||
|
count: int
|
||||||
|
draft_list: List[DraftListModel]
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('')
|
||||||
|
async def get_draftlist(season: Optional[int], team_id: list = Query(default=None)):
|
||||||
|
all_list = DraftList.select()
|
||||||
|
|
||||||
|
if season is not None:
|
||||||
|
all_list = all_list.where(DraftList.season == season)
|
||||||
|
if team_id is not None:
|
||||||
|
all_list = all_list.where(DraftList.team_id << team_id)
|
||||||
|
|
||||||
|
r_list = {
|
||||||
|
'count': all_list.count(),
|
||||||
|
'picks': [model_to_dict(x) for x in all_list]
|
||||||
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
return r_list
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/team/{team_id}')
|
||||||
|
async def get_team_draftlist(team_id: int):
|
||||||
|
this_team = Team.get_or_none(Team.id == team_id)
|
||||||
|
if this_team is None:
|
||||||
|
raise HTTPException(status_code=404, detail=f'Team ID {team_id} not found')
|
||||||
|
|
||||||
|
this_list = DraftList.select().where(DraftList.team == this_team)
|
||||||
|
r_list = {
|
||||||
|
'count': this_list.count(),
|
||||||
|
'picks': [model_to_dict(x) for x in this_list]
|
||||||
|
}
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
return r_list
|
||||||
|
|
||||||
|
|
||||||
|
@router.post('')
|
||||||
|
async def post_draftlist(draft_list: DraftListList, token: str = Depends(oauth2_scheme)):
|
||||||
|
if not valid_token(token):
|
||||||
|
logging.warning(f'post_draftlist - Bad Token: {token}')
|
||||||
|
raise HTTPException(status_code=401, detail='Unauthorized')
|
||||||
|
|
||||||
|
new_list = []
|
||||||
|
this_team = Team.get_or_none(Team.id == draft_list.draft_list[0].team_id)
|
||||||
|
if this_team is None:
|
||||||
|
raise HTTPException(status_code=404, detail=f'Team ID {draft_list.draft_list[0].team_id} not found')
|
||||||
|
|
||||||
|
DraftList.delete().where(DraftList.team == this_team).execute()
|
||||||
|
|
||||||
|
for x in draft_list.draft_list:
|
||||||
|
new_list.append(x.dict())
|
||||||
|
|
||||||
|
with db.atomic():
|
||||||
|
for batch in chunked(new_list, 15):
|
||||||
|
DraftList.insert_many(batch).on_conflict_replace().execute()
|
||||||
|
|
||||||
|
db.close()
|
||||||
|
return f'Inserted {len(new_list)} list values'
|
||||||
Loading…
Reference in New Issue
Block a user