fix: Implement real database queries for game API endpoints
- /categories: Query categories from database instead of hardcoded list
- /today-questions: Query approved questions with date_active = today
- /question/{id}: Query specific question from database
- /board/{room_code}: Load board from database if not in Redis
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,24 +15,24 @@ router = APIRouter()
|
|||||||
|
|
||||||
|
|
||||||
@router.get("/categories")
|
@router.get("/categories")
|
||||||
async def get_game_categories():
|
async def get_game_categories(db: AsyncSession = Depends(get_db)):
|
||||||
"""Get all categories for the game board."""
|
"""Get all categories for the game board."""
|
||||||
# Return hardcoded categories for now
|
result = await db.execute(select(Category).order_by(Category.id))
|
||||||
# In production, these would come from the database
|
categories = result.scalars().all()
|
||||||
|
|
||||||
return [
|
return [
|
||||||
{"id": 1, "name": "Nintendo", "icon": "🍄", "color": "#E60012"},
|
{
|
||||||
{"id": 2, "name": "Xbox", "icon": "🎮", "color": "#107C10"},
|
"id": cat.id,
|
||||||
{"id": 3, "name": "PlayStation", "icon": "🎯", "color": "#003791"},
|
"name": cat.name,
|
||||||
{"id": 4, "name": "Anime", "icon": "⛩️", "color": "#FF6B9D"},
|
"icon": cat.icon,
|
||||||
{"id": 5, "name": "Música", "icon": "🎵", "color": "#1DB954"},
|
"color": cat.color
|
||||||
{"id": 6, "name": "Películas", "icon": "🎬", "color": "#F5C518"},
|
}
|
||||||
{"id": 7, "name": "Libros", "icon": "📚", "color": "#8B4513"},
|
for cat in categories
|
||||||
{"id": 8, "name": "Historia-Cultura", "icon": "🏛️", "color": "#6B5B95"},
|
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@router.get("/board/{room_code}")
|
@router.get("/board/{room_code}")
|
||||||
async def get_game_board(room_code: str):
|
async def get_game_board(room_code: str, db: AsyncSession = Depends(get_db)):
|
||||||
"""
|
"""
|
||||||
Get the game board with questions for today.
|
Get the game board with questions for today.
|
||||||
Returns questions grouped by category.
|
Returns questions grouped by category.
|
||||||
@@ -47,52 +47,95 @@ async def get_game_board(room_code: str):
|
|||||||
if room.get("board"):
|
if room.get("board"):
|
||||||
return room["board"]
|
return room["board"]
|
||||||
|
|
||||||
# Otherwise, this would load from database
|
# Load today's questions from database
|
||||||
# For now, return empty board structure
|
today = date.today()
|
||||||
return {}
|
result = await db.execute(
|
||||||
|
select(Question, Category)
|
||||||
|
.join(Category, Question.category_id == Category.id)
|
||||||
|
.where(Question.date_active == today)
|
||||||
|
.where(Question.status == "approved")
|
||||||
|
.order_by(Category.id, Question.difficulty)
|
||||||
|
)
|
||||||
|
rows = result.all()
|
||||||
|
|
||||||
|
board = {}
|
||||||
|
for question, category in rows:
|
||||||
|
cat_id = str(category.id)
|
||||||
|
if cat_id not in board:
|
||||||
|
board[cat_id] = []
|
||||||
|
board[cat_id].append({
|
||||||
|
"id": question.id,
|
||||||
|
"difficulty": question.difficulty,
|
||||||
|
"points": question.points,
|
||||||
|
"time_seconds": question.time_seconds,
|
||||||
|
"answered": False
|
||||||
|
})
|
||||||
|
|
||||||
|
return board
|
||||||
|
|
||||||
|
|
||||||
@router.get("/today-questions")
|
@router.get("/today-questions")
|
||||||
async def get_today_questions():
|
async def get_today_questions(db: AsyncSession = Depends(get_db)):
|
||||||
"""
|
"""
|
||||||
Get all approved questions for today, grouped by category and difficulty.
|
Get all approved questions for today, grouped by category and difficulty.
|
||||||
This is used to build the game board.
|
This is used to build the game board.
|
||||||
"""
|
"""
|
||||||
# This would query the database for questions with date_active = today
|
today = date.today()
|
||||||
# For now, return sample structure
|
|
||||||
return {
|
# Get all approved questions for today
|
||||||
"date": str(date.today()),
|
result = await db.execute(
|
||||||
"categories": {
|
select(Question, Category)
|
||||||
"1": { # Nintendo
|
.join(Category, Question.category_id == Category.id)
|
||||||
"name": "Nintendo",
|
.where(Question.date_active == today)
|
||||||
"questions": [
|
.where(Question.status == "approved")
|
||||||
{"difficulty": 1, "id": 1, "points": 100},
|
.order_by(Category.id, Question.difficulty)
|
||||||
{"difficulty": 2, "id": 2, "points": 200},
|
)
|
||||||
{"difficulty": 3, "id": 3, "points": 300},
|
rows = result.all()
|
||||||
{"difficulty": 4, "id": 4, "points": 400},
|
|
||||||
{"difficulty": 5, "id": 5, "points": 500},
|
categories = {}
|
||||||
]
|
for question, category in rows:
|
||||||
|
cat_id = str(category.id)
|
||||||
|
if cat_id not in categories:
|
||||||
|
categories[cat_id] = {
|
||||||
|
"name": category.name,
|
||||||
|
"icon": category.icon,
|
||||||
|
"color": category.color,
|
||||||
|
"questions": []
|
||||||
}
|
}
|
||||||
# ... other categories
|
categories[cat_id]["questions"].append({
|
||||||
}
|
"difficulty": question.difficulty,
|
||||||
|
"id": question.id,
|
||||||
|
"points": question.points
|
||||||
|
})
|
||||||
|
|
||||||
|
return {
|
||||||
|
"date": str(today),
|
||||||
|
"categories": categories
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@router.get("/question/{question_id}")
|
@router.get("/question/{question_id}")
|
||||||
async def get_question(question_id: int):
|
async def get_question(question_id: int, db: AsyncSession = Depends(get_db)):
|
||||||
"""
|
"""
|
||||||
Get a specific question (without the answer).
|
Get a specific question (without the answer).
|
||||||
Used when a player selects a question.
|
Used when a player selects a question.
|
||||||
"""
|
"""
|
||||||
# This would query the database
|
result = await db.execute(
|
||||||
# For now, return sample
|
select(Question).where(Question.id == question_id)
|
||||||
|
)
|
||||||
|
question = result.scalar_one_or_none()
|
||||||
|
|
||||||
|
if not question:
|
||||||
|
raise HTTPException(status_code=404, detail="Question not found")
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"id": question_id,
|
"id": question.id,
|
||||||
"question_text": "¿En qué año se lanzó la primera consola Nintendo Entertainment System (NES) en Japón?",
|
"question_text": question.question_text,
|
||||||
"difficulty": 3,
|
"difficulty": question.difficulty,
|
||||||
"points": 300,
|
"points": question.points,
|
||||||
"time_seconds": 25,
|
"time_seconds": question.time_seconds,
|
||||||
"category_id": 1
|
"category_id": question.category_id,
|
||||||
|
"fun_fact": question.fun_fact
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user