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:
2026-01-26 09:12:00 +00:00
parent 720432702f
commit 0642b559df

View File

@@ -15,24 +15,24 @@ router = APIRouter()
@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."""
# Return hardcoded categories for now
# In production, these would come from the database
result = await db.execute(select(Category).order_by(Category.id))
categories = result.scalars().all()
return [
{"id": 1, "name": "Nintendo", "icon": "🍄", "color": "#E60012"},
{"id": 2, "name": "Xbox", "icon": "🎮", "color": "#107C10"},
{"id": 3, "name": "PlayStation", "icon": "🎯", "color": "#003791"},
{"id": 4, "name": "Anime", "icon": "⛩️", "color": "#FF6B9D"},
{"id": 5, "name": "Música", "icon": "🎵", "color": "#1DB954"},
{"id": 6, "name": "Películas", "icon": "🎬", "color": "#F5C518"},
{"id": 7, "name": "Libros", "icon": "📚", "color": "#8B4513"},
{"id": 8, "name": "Historia-Cultura", "icon": "🏛️", "color": "#6B5B95"},
{
"id": cat.id,
"name": cat.name,
"icon": cat.icon,
"color": cat.color
}
for cat in categories
]
@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.
Returns questions grouped by category.
@@ -47,52 +47,95 @@ async def get_game_board(room_code: str):
if room.get("board"):
return room["board"]
# Otherwise, this would load from database
# For now, return empty board structure
return {}
# Load today's questions from database
today = date.today()
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")
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.
This is used to build the game board.
"""
# This would query the database for questions with date_active = today
# For now, return sample structure
return {
"date": str(date.today()),
"categories": {
"1": { # Nintendo
"name": "Nintendo",
"questions": [
{"difficulty": 1, "id": 1, "points": 100},
{"difficulty": 2, "id": 2, "points": 200},
{"difficulty": 3, "id": 3, "points": 300},
{"difficulty": 4, "id": 4, "points": 400},
{"difficulty": 5, "id": 5, "points": 500},
]
today = date.today()
# Get all approved questions for today
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()
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}")
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).
Used when a player selects a question.
"""
# This would query the database
# For now, return sample
result = await db.execute(
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 {
"id": question_id,
"question_text": "¿En qué año se lanzó la primera consola Nintendo Entertainment System (NES) en Japón?",
"difficulty": 3,
"points": 300,
"time_seconds": 25,
"category_id": 1
"id": question.id,
"question_text": question.question_text,
"difficulty": question.difficulty,
"points": question.points,
"time_seconds": question.time_seconds,
"category_id": question.category_id,
"fun_fact": question.fun_fact
}