Skip to content

Commit

Permalink
adding games with details
Browse files Browse the repository at this point in the history
  • Loading branch information
justinstoner2 committed May 23, 2024
1 parent 5272684 commit bfaf87a
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions trivia-forge/backend/endpoints/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,53 @@ def post_get_games():
return {"error": "No games found"}
return jsonify([game for game in games])

@bp.route('/games_with_details', methods=['GET'])
def get_games_with_details():
try:
games_query = supabase.table("Games").select("*").execute()
games = games_query.data
print(games)

if not games:
return {"error": "No games found"}

# Fetch related data
game_ids = [game['id'] for game in games]
try:
categories_query = supabase.table("Categories").select("*").execute()
categories = categories_query.data
except Exception as e:
return {"error": "No categories found while fetching details"}

try:
questions_query = supabase.table("Questions").select("*").execute()
questions = questions_query.data
except Exception as e:
return {"error": "No questions found while fetching details"}

try:
choices_query = supabase.table("Choices").select("*").execute()
choices = choices_query.data
except Exception as e:
return {"error": "No choices found while fetching details"}

# Organize data into a nested structure
game_details = []
for game in games:
game_detail = game
game_categories = [category for category in categories if category['game_id'] == game['id']]
for category in game_categories:
category_questions = [question for question in questions if question['category_id'] == category['id']]
for question in category_questions:
question_choices = [choice for choice in choices if choice['question_id'] == question['id']]
question['choices'] = question_choices
category['questions'] = category_questions
game_detail['categories'] = game_categories
game_details.append(game_detail)

return jsonify(game_details)
except Exception as e:
return {"error": str(e)}

@bp.route('/<game_id>', methods=['GET', 'PATCH', 'DELETE'])
def get_patch_delete_game(game_id):
Expand Down

0 comments on commit bfaf87a

Please sign in to comment.