Fix infinite sql query bug
This commit is contained in:
parent
38eb9027d6
commit
6a8cb2c3bd
@ -15,23 +15,22 @@ def get_most_recent_high_scores(db: Session, minigame: MinigameEnum, amount: int
|
||||
"""Get the n most recent high scores of a given minigame"""
|
||||
if amount < 1:
|
||||
raise HTTPException(status_code=400, detail="Invalid number of high scores")
|
||||
|
||||
high_scores = []
|
||||
|
||||
if not minigame:
|
||||
minigame = MinigameEnum.SpellingBee
|
||||
|
||||
high_scores = (
|
||||
high_scores_query = (
|
||||
db.query(HighScore)
|
||||
.filter(HighScore.minigame == minigame)
|
||||
.order_by(desc(HighScore.score_value))
|
||||
.order_by(desc(HighScore.time))
|
||||
.limit(amount)
|
||||
.all()
|
||||
)
|
||||
for high_score in high_scores:
|
||||
|
||||
for high_score in high_scores_query:
|
||||
high_scores.append(
|
||||
Score(score_value=high_score.score_value, time=high_score.time)
|
||||
)
|
||||
|
||||
return high_scores
|
||||
|
||||
|
||||
@ -41,7 +40,6 @@ def get_highest_high_scores(
|
||||
"""Get the n highest scores of a given minigame"""
|
||||
if amount < 1:
|
||||
raise HTTPException(status_code=400, detail="Invalid number of high scores")
|
||||
|
||||
if mine_only:
|
||||
if amount > 1:
|
||||
raise HTTPException(
|
||||
@ -70,16 +68,15 @@ def get_highest_high_scores(
|
||||
|
||||
if not minigame:
|
||||
minigame = MinigameEnum.SpellingBee
|
||||
|
||||
high_scores = (
|
||||
high_scores_query = (
|
||||
db.query(HighScore)
|
||||
.filter(HighScore.minigame == minigame)
|
||||
.order_by(asc(HighScore.time))
|
||||
.order_by(desc(HighScore.score_value))
|
||||
.limit(amount)
|
||||
.all()
|
||||
)
|
||||
for high_score in high_scores:
|
||||
user_high_scores.append(
|
||||
|
||||
for high_score in high_scores_query:
|
||||
high_scores.append(
|
||||
Score(score_value=high_score.score_value, time=high_score.time)
|
||||
)
|
||||
return high_scores
|
||||
|
||||
0
src/crud/learnableprogress.py
Normal file
0
src/crud/learnableprogress.py
Normal file
@ -1,15 +1,16 @@
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from src.crud.highscores import get_highest_high_scores, get_most_recent_high_scores
|
||||
from src.crud.highscores import (get_highest_high_scores,
|
||||
get_most_recent_high_scores)
|
||||
from src.crud.users import get_user_by_username
|
||||
from src.enums import CourseEnum, MinigameEnum
|
||||
from src.models import CourseProgress, LearnableProgress
|
||||
from src.schemas.saved_data import *
|
||||
from src.enums import MinigameEnum
|
||||
|
||||
|
||||
def get_saved_data(db: Session, username: str):
|
||||
def get_saved_data(db: Session, username: str):
|
||||
"""Fetches all saved progress for the current user from the database"""
|
||||
user = get_user_by_username(db, username)
|
||||
|
||||
minigames = []
|
||||
courses = []
|
||||
|
||||
@ -17,8 +18,55 @@ def get_saved_data(db: Session, username: str):
|
||||
minigames.append(
|
||||
SavedMinigameProgress(
|
||||
minigame_index=minigame,
|
||||
latest_scores = get_most_recent_high_scores(db, minigame, 10),
|
||||
highest_scores = get_highest_high_scores(db, minigame, user, 10, False)
|
||||
latest_scores=[
|
||||
score for score in get_most_recent_high_scores(db, minigame, 10)
|
||||
],
|
||||
highest_scores=[
|
||||
score
|
||||
for score in get_highest_high_scores(db, minigame, user, 10, False)
|
||||
],
|
||||
)
|
||||
)
|
||||
|
||||
course_progress_query = (
|
||||
db.query(CourseProgress).filter(CourseProgress.owner_id == user.user_id).all()
|
||||
)
|
||||
|
||||
for course_progress in course_progress_query:
|
||||
learnable_progress_query = (
|
||||
db.query(LearnableProgress)
|
||||
.filter(
|
||||
LearnableProgress.course_progress_id
|
||||
== course_progress.course_progress_id
|
||||
)
|
||||
.all()
|
||||
)
|
||||
|
||||
learnables = [
|
||||
SavedLearnableProgress(
|
||||
index=lp.index, in_use=lp.in_use, name=lp.name, progress=lp.progress
|
||||
)
|
||||
for lp in learnable_progress_query
|
||||
]
|
||||
|
||||
completed_learnables = sum(
|
||||
[1 if learnable.progress == 5.0 else 0 for learnable in learnables]
|
||||
)
|
||||
|
||||
in_use_learnables = sum(
|
||||
[1 if learnable.in_use else 0 for learnable in learnables]
|
||||
)
|
||||
|
||||
total_learnables = len(learnables)
|
||||
|
||||
courses.append(
|
||||
SavedCourseProgress(
|
||||
course_index=course_progress.course,
|
||||
progress=course_progress.progress,
|
||||
completed_learnables=completed_learnables,
|
||||
in_use_learnables=in_use_learnables,
|
||||
total_learnables=total_learnables,
|
||||
learnables=learnables,
|
||||
)
|
||||
)
|
||||
|
||||
@ -26,8 +74,8 @@ def get_saved_data(db: Session, username: str):
|
||||
username=user.username,
|
||||
avatar_index=user.avatar_index,
|
||||
playtime=user.playtime,
|
||||
minigames = minigames,
|
||||
courses=courses
|
||||
minigames=minigames,
|
||||
courses=courses,
|
||||
)
|
||||
|
||||
return user_progress
|
||||
|
||||
@ -13,7 +13,7 @@ from src.crud import saved_data as crud_saved_data
|
||||
from src.crud import users as crud_users
|
||||
from src.database import Base, engine, get_db
|
||||
from src.enums import CourseEnum, MinigameEnum
|
||||
from src.schemas import courseprogress, highscores, users, saved_data
|
||||
from src.schemas import courseprogress, highscores, saved_data, users
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
@ -54,6 +54,7 @@ async def read_saved_data(
|
||||
):
|
||||
return crud_saved_data.get_saved_data(db, current_user_name)
|
||||
|
||||
|
||||
@app.post("/register")
|
||||
async def register(user: users.UserCreate, db: Session = Depends(get_db)):
|
||||
access_token = crud_authentication.register(
|
||||
|
||||
@ -58,7 +58,9 @@ class LearnableProgress(Base):
|
||||
learnable_progress_id = Column(Integer, primary_key=True, index=True)
|
||||
index = Column(Integer, nullable=False)
|
||||
in_use = Column(Boolean, nullable=False)
|
||||
name = Column(String, nullable=False)
|
||||
name = Column(String, unique=True, nullable=False)
|
||||
progress = Column(Float, nullable=False)
|
||||
course_progress_id = Column(Integer, ForeignKey("course_progress.course_progress_id"))
|
||||
course_progress_id = Column(
|
||||
Integer, ForeignKey("course_progress.course_progress_id")
|
||||
)
|
||||
course = relationship("CourseProgress", back_populates="learnables")
|
||||
|
||||
0
src/schemas/learnableprogress.py
Normal file
0
src/schemas/learnableprogress.py
Normal file
@ -208,7 +208,11 @@ async def test_get_highscores_returns_sorted_list_with_correct_length():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers=headers,
|
||||
json={"username": user, "password": password, "avatar": avatar},
|
||||
json={
|
||||
"username": user,
|
||||
"password": password,
|
||||
"avatar_index": avatar_index,
|
||||
},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user