Added /schedules and /results

This commit is contained in:
Cal Corum 2023-03-22 10:42:55 -05:00
parent 7ac8b752ec
commit 0c7712cd78
6 changed files with 310 additions and 1821 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
from fastapi import Depends, FastAPI
from routers_v3 import current, players
from routers_v3 import current, players, results, schedules
app = FastAPI(
responses={404: {'description': 'Not found'}}
@ -9,6 +9,8 @@ app = FastAPI(
app.include_router(current.router)
app.include_router(players.router)
app.include_router(results.router)
app.include_router(schedules.router)
# @app.get("/api")

View File

@ -3,7 +3,7 @@ from typing import Optional
import logging
import pydantic
from db_engine import db, Current, model_to_dict, DatabaseError
from db_engine import db, Current, model_to_dict
from dependencies import oauth2_scheme, valid_token, logging
router = APIRouter(

View File

@ -3,7 +3,7 @@ from typing import List, Optional
import logging
import pydantic
from db_engine import db, Player, model_to_dict, DatabaseError, chunked
from db_engine import db, Player, model_to_dict, chunked
from dependencies import oauth2_scheme, valid_token, logging
router = APIRouter(
@ -158,7 +158,7 @@ def patch_player(
raise HTTPException(status_code=500, detail=f'Unable to patch player {player_id}')
@router.post('/')
@router.post('')
def post_players(p_list: PlayerList, token: str = Depends(oauth2_scheme)):
if not valid_token(token):
logging.warning(f'post_players - Bad Token: {token}')

161
app/routers_v3/results.py Normal file
View File

@ -0,0 +1,161 @@
from fastapi import APIRouter, Depends, HTTPException, Query
from typing import List, Optional
import logging
import pydantic
from db_engine import db, Result, Team, model_to_dict, DatabaseError, chunked
from dependencies import oauth2_scheme, valid_token, logging
router = APIRouter(
prefix='/api/v3/results',
tags=['results']
)
class ResultModel(pydantic.BaseModel):
week: int
game: int
awayteam_id: int
hometeam_id: int
awayscore: int
homescore: int
season: int
scorecard_url: Optional[str] = None
class ResultList(pydantic.BaseModel):
results: List[ResultModel]
@router.get('')
def get_results(
season: int, team_abbrev: list = Query(default=None), week_start: list = Query(default=None),
week_end: list = Query(default=None), game_num: list = Query(default=None),
away_abbrev: list = Query(default=None), home_abbrev: list = Query(default=None)):
all_results = Result.select_season(season)
if team_abbrev is not None:
team_list = []
for x in team_abbrev:
team_list.append(Team.get_season(x, season))
all_results = all_results.where(
(Result.awayteam << team_list) | (Result.hometeam << team_list)
)
if away_abbrev is not None:
team_list = []
for x in away_abbrev:
team_list.append(Team.get_season(x, season))
all_results = all_results.where(Result.awayteam << team_list)
if home_abbrev is not None:
team_list = []
for x in home_abbrev:
team_list.append(Team.get_season(x, season))
all_results = all_results.where(Result.hometeam << team_list)
if game_num is not None:
all_results = all_results.where(Result.game << game_num)
if week_start is not None:
all_results = all_results.where(Result.week >= week_start)
if week_end is not None:
all_results = all_results.where(Result.week <= week_end)
return_results = {
'count': all_results.count(),
'results': [model_to_dict(x) for x in all_results]
}
db.close()
return return_results
@router.get('/{result_id}')
def get_one_result(result_id: int):
this_result = Result.get_or_none(Result.id == result_id)
if this_result is not None:
r_result = model_to_dict(this_result)
else:
r_result = None
db.close()
return r_result
@router.patch('/{result_id}')
def patch_result(
result_id: int, week_num: Optional[int] = None, game_num: Optional[int] = None,
away_team_id: Optional[int] = None, home_team_id: Optional[int] = None, away_score: Optional[int] = None,
home_score: Optional[int] = None, season: Optional[int] = None, scorecard_url: Optional[str] = None):
this_result = Result.get_or_none(Result.id == result_id)
if this_result is None:
raise KeyError(f'Result ID {result_id} not found')
if week_num is not None:
this_result.week = week_num
if game_num is not None:
this_result.game = game_num
if away_team_id is not None:
this_result.awayteam_id = away_team_id
if home_team_id is not None:
this_result.hometeam_id = home_team_id
if away_score is not None:
this_result.awayscore = away_score
if home_score is not None:
this_result.homescore = home_score
if season is not None:
this_result.season = season
if scorecard_url is not None:
this_result.scorecard_url = scorecard_url
if this_result.save() == 1:
r_result = model_to_dict(this_result)
db.close()
return r_result
else:
db.close()
raise DatabaseError(f'Unable to patch result {result_id}')
@router.post('')
def post_results(result_list: ResultList):
new_results = []
for x in result_list.results:
if Team.get_or_none(Team.id == x.awayteam_id) is None:
raise KeyError(f'Team ID {x.awayteam_id} not found')
if Team.get_or_none(Team.id == x.hometeam_id) is None:
raise KeyError(f'Team ID {x.hometeam_id} not found')
new_results.append(x.dict())
with db.atomic():
for batch in chunked(new_results, 15):
Result.insert_many(batch).on_conflict_replace().execute()
db.close()
return f'Inserted {len(new_results)} results'
@router.delete('/{result_id}')
def delete_result(result_id: int):
this_result = Result.get_or_none(Result.id == result_id)
if not this_result:
db.close()
raise KeyError(f'Result ID {result_id} not found')
count = this_result.delete_instance()
db.close()
if count == 1:
return f'Result {result_id} has been deleted'
else:
raise DatabaseError(f'Result {result_id} could not be deleted')

143
app/routers_v3/schedules.py Normal file
View File

@ -0,0 +1,143 @@
from fastapi import APIRouter, Depends, HTTPException, Query
from typing import List, Optional
import logging
import pydantic
from db_engine import db, Schedule, Team, model_to_dict, DatabaseError, chunked
from dependencies import oauth2_scheme, valid_token, logging
router = APIRouter(
prefix='/api/v3/schedules',
tags=['schedules']
)
class ScheduleModel(pydantic.BaseModel):
week: int
awayteam_id: int
hometeam_id: int
gamecount: int
season: int
class ScheduleList(pydantic.BaseModel):
schedules: List[ScheduleModel]
@router.get('')
def get_schedules(
season: int, team_abbrev: list = Query(default=None), away_abbrev: list = Query(default=None),
home_abbrev: list = Query(default=None), week_start: Optional[int] = None, week_end: Optional[int] = None):
all_sched = Schedule.select_season(season)
if team_abbrev is not None:
team_list = []
for x in team_abbrev:
team_list.append(Team.get_season(x, season))
all_sched = all_sched.where(
(Schedule.awayteam << team_list) | (Schedule.hometeam << team_list)
)
if away_abbrev is not None:
team_list = []
for x in away_abbrev:
team_list.append(Team.get_season(x, season))
all_sched = all_sched.where(Schedule.awayteam << team_list)
if home_abbrev is not None:
team_list = []
for x in home_abbrev:
team_list.append(Team.get_season(x, season))
all_sched = all_sched.where(Schedule.hometeam << team_list)
if week_start is not None:
all_sched = all_sched.where(Schedule.week >= week_start)
if week_end is not None:
all_sched = all_sched.where(Schedule.week <= week_end)
all_sched = all_sched.order_by(Schedule.id)
return_sched = {
'count': all_sched.count(),
'schedules': [model_to_dict(x) for x in all_sched]
}
db.close()
return return_sched
@router.get('/{schedule_id}')
def get_one_schedule(schedule_id: int):
this_sched = Schedule.get_or_none(Schedule.id == schedule_id)
if this_sched is not None:
r_sched = model_to_dict(this_sched)
else:
r_sched = None
db.close()
return r_sched
@router.patch('/{schedule_id}')
def patch_schedule(
schedule_id: int, week: list = Query(default=None), awayteam_id: Optional[int] = None,
hometeam_id: Optional[int] = None, gamecount: Optional[int] = None, season: Optional[int] = None):
this_sched = Schedule.get_or_none(Schedule.id == schedule_id)
if this_sched is None:
raise KeyError(f'Schedule ID {schedule_id} not found')
if week is not None:
this_sched.week = week
if awayteam_id is not None:
this_sched.awayteam_id = awayteam_id
if hometeam_id is not None:
this_sched.hometeam_id = hometeam_id
if gamecount is not None:
this_sched.gamecount = gamecount
if season is not None:
this_sched.season = season
if this_sched.save() == 1:
r_sched = model_to_dict(this_sched)
db.close()
return r_sched
else:
db.close()
raise DatabaseError(f'Unable to patch schedule {schedule_id}')
@router.post('')
def post_schedules(sched_list: ScheduleList):
new_sched = []
for x in sched_list.schedules:
if Team.get_or_none(Team.id == x.awayteam_id) is None:
raise KeyError(f'Team ID {x.awayteam_id} not found')
if Team.get_or_none(Team.id == x.hometeam_id) is None:
raise KeyError(f'Team ID {x.hometeam_id} not found')
new_sched.append(x.dict())
with db.atomic():
for batch in chunked(new_sched, 15):
Schedule.insert_many(batch).on_conflict_replace().execute()
db.close()
return f'Inserted {len(new_sched)} schedules'
@router.delete('/{schedule_id}')
def delete_schedule(schedule_id: int):
this_sched = Schedule.get_or_none(Schedule.id == schedule_id)
if this_sched is None:
raise KeyError(f'Schedule ID {schedule_id} not found')
count = this_sched.delete_instance()
db.close()
if count == 1:
return f'Schedule {this_sched} has been deleted'
else:
raise DatabaseError(f'Schedule {this_sched} could not be deleted')