More highscore endpoint functionality & tests

This commit is contained in:
lvrossem
2023-04-12 12:02:25 -06:00
parent 73ce1bf2e0
commit f9aad400e0
3 changed files with 117 additions and 23 deletions

View File

@@ -7,20 +7,41 @@ from src.models import HighScore, User
from src.schemas.highscores import HighScoreBase
from src.schemas.users import UserHighScore
DEFAULT_NR_HIGH_SCORES = 10
def get_high_scores(db: Session, minigame: MinigameEnum, nr_highest: int):
def get_high_scores(
db: Session, minigame: MinigameEnum, user: User, nr_highest: int, mine_only: bool
):
"""Get the n highest scores of a given minigame"""
if nr_highest:
if nr_highest < 1:
raise HTTPException(status_code=400, detail="Invalid number of high scores")
if nr_highest < 1:
raise HTTPException(status_code=400, detail="Invalid number of high scores")
if mine_only:
if nr_highest > 1:
raise HTTPException(
status_code=400,
detail="nr_highest should be 1 when requesting high score of current user only",
)
else:
high_score = (
db.query(HighScore)
.filter(
HighScore.minigame == minigame, HighScore.owner_id == user.user_id
)
.first()
)
if high_score:
return [
UserHighScore(
username=user.username,
score_value=high_score.score_value,
avatar=user.avatar,
)
]
else:
return []
user_high_scores = []
if not nr_highest:
nr_highest = DEFAULT_NR_HIGH_SCORES
if not minigame:
minigame = MinigameEnum.SpellingBee

View File

@@ -64,11 +64,15 @@ async def login(user: users.UserCreate, db: Session = Depends(get_db)):
@app.get("/highscores/{minigame}", response_model=List[users.UserHighScore])
async def get_high_scores(
minigame: MinigameEnum,
nr_highest: Optional[int] = None,
nr_highest: Optional[int] = 1,
mine_only: Optional[bool] = True,
current_user_name: str = Depends(crud_authentication.get_current_user_name),
db: Session = Depends(get_db),
):
return crud_highscores.get_high_scores(db, minigame, nr_highest)
print(str(nr_highest))
print(str(mine_only))
user = crud_users.get_user_by_username(db, current_user_name)
return crud_highscores.get_high_scores(db, minigame, user, nr_highest, mine_only)
@app.put("/highscores/{minigame}", response_model=highscores.HighScore)
@@ -107,13 +111,3 @@ async def patch_course_progress(
return crud_courseprogress.patch_course_progress(
db, current_user, course, course_progress
)
#### TESTING!! DELETE LATER
@app.get("/protected")
async def protected_route(
current_user_name: str = Depends(crud_authentication.get_current_user_name),
):
return {"message": f"Hello, {current_user_name}!"}