Skip to content

Commit

Permalink
backend push
Browse files Browse the repository at this point in the history
  • Loading branch information
demuthsa committed May 23, 2024
1 parent dda7e5d commit 1534436
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
28 changes: 27 additions & 1 deletion trivia-forge/backend/endpoints/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dotenv import dotenv_values



bp = Blueprint('game', __name__, url_prefix='/games')

config = dotenv_values("./.env")
Expand All @@ -21,7 +22,11 @@ def post_get_games():
except Exception as e:
return {"error": e.details}
elif request.method == 'GET':
query = supabase.table("Games").select("*").execute()
user_id = request.args.get('user_id')
if user_id:
query = supabase.table("Games").select("*").eq("user_id", user_id).execute()
else:
query = supabase.table("Games").select("*").execute()
games = query.data
if not games:
return {"error": "No games found"}
Expand All @@ -44,3 +49,24 @@ def get_patch_delete_game(game_id):
elif request.method == 'DELETE':
supabase.table("Games").delete().eq("id", game_id).execute()
return {}

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

for game in games:
categories_query = supabase.table("Categories").select("*").eq("game_id", game['id']).execute()
categories = categories_query.data
for category in categories:
questions_query = supabase.table("Questions").select("*").eq("category_id", category['id']).execute()
questions = questions_query.data
category['questions'] = questions
game['categories'] = categories

return jsonify(games)
except Exception as e:
print(f"Error fetching games with details: {str(e)}")
return jsonify({"error": str(e)})
21 changes: 21 additions & 0 deletions trivia-forge/backend/endpoints/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from dotenv import dotenv_values



bp = Blueprint('user', __name__, url_prefix='/users')

config = dotenv_values("./.env")
Expand All @@ -19,6 +20,26 @@ def serialize_user(user):
"email": user.get("email")
}

@bp.route('/login', methods=['POST'])
def login():
"""Handle user login by verifying email and password"""
# get data from the request
data = request.get_json()
email = data.get('email')
password = data.get('password')

# query users table for email
query = supabase.table("Users").select("*").eq("email", email).execute()
user = query.data

# check if user exists and password matches
if not user or user[0]['password'] != password:
return jsonify({"error": "Invalid email or password"})

user = user[0]
return jsonify(serialize_user(user))



@bp.route('', methods=['POST', 'GET'])
def post_get_users():
Expand Down
1 change: 1 addition & 0 deletions trivia-forge/backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

app = Flask(__name__)
CORS(app)

app.register_blueprint(home.bp)
app.register_blueprint(user.bp)
app.register_blueprint(game.bp)
Expand Down
6 changes: 6 additions & 0 deletions trivia-forge/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1534436

Please sign in to comment.