Skip to content

Commit

Permalink
Added endpoints for games and categories
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanueposu committed Apr 29, 2024
1 parent 1e55e6a commit 2eddf96
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 3 deletions.
46 changes: 46 additions & 0 deletions trivia-forge/backend/endpoints/category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from flask import Blueprint, request, jsonify
from supabase import create_client, Client
from dotenv import dotenv_values


bp = Blueprint('category', __name__, url_prefix='/categories')

config = dotenv_values("./.env")
url: str = config.get('SUPABASE_URL')
key: str = config.get('SUPABASE_KEY')
supabase: Client = create_client(url, key)


@bp.route('', methods=['POST', 'GET'])
def post_get_categories():
if request.method == 'POST':
data = request.get_json()
try:
new_category = supabase.table("Categories").insert(data).execute()
return jsonify(new_category.data[0])
except Exception as e:
return {"error": e.details}
elif request.method == 'GET':
query = supabase.table("Categories").select("*").execute()
categories = query.data
if not categories:
return {"error": "No categories found"}
return jsonify([category for category in categories])


@bp.route('/<category_id>', methods=['GET', 'PATCH', 'DELETE'])
def get_patch_delete_category(category_id):
query = supabase.table("Categories").select("*").eq("id", category_id).execute()
category = query.data
if not category:
return {"error": "Category not found"}
elif request.method == 'GET':
return jsonify(category[0])
elif request.method == 'PATCH':
data = request.get_json()
updated_data = {'title': data['title']}
updated_category = supabase.table("Categories").update(updated_data).eq("id", category_id).execute()
return jsonify(updated_category.data[0])
elif request.method == 'DELETE':
supabase.table("Categories").delete().eq("id", category_id).execute()
return {}
46 changes: 46 additions & 0 deletions trivia-forge/backend/endpoints/game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from flask import Blueprint, request, jsonify
from supabase import create_client, Client
from dotenv import dotenv_values


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

config = dotenv_values("./.env")
url: str = config.get('SUPABASE_URL')
key: str = config.get('SUPABASE_KEY')
supabase: Client = create_client(url, key)


@bp.route('', methods=['POST', 'GET'])
def post_get_games():
if request.method == 'POST':
data = request.get_json()
try:
new_game = supabase.table("Games").insert(data).execute()
return jsonify(new_game.data[0])
except Exception as e:
return {"error": e.details}
elif request.method == 'GET':
query = supabase.table("Games").select("*").execute()
games = query.data
if not games:
return {"error": "No games found"}
return jsonify([game for game in games])


@bp.route('/<game_id>', methods=['GET', 'PATCH', 'DELETE'])
def get_patch_delete_game(game_id):
query = supabase.table("Games").select("*").eq("id", game_id).execute()
game = query.data
if not game:
return {"error": "Game not found"}
elif request.method == 'GET':
return jsonify(game[0])
elif request.method == 'PATCH':
data = request.get_json()
updated_data = {'title': data['title']}
updated_game = supabase.table("Games").update(updated_data).eq("id", game_id).execute()
return jsonify(updated_game.data[0])
elif request.method == 'DELETE':
supabase.table("Games").delete().eq("id", game_id).execute()
return {}
2 changes: 1 addition & 1 deletion trivia-forge/backend/endpoints/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def post_get_users():


@bp.route('/<user_id>', methods=['GET', 'PATCH', 'DELETE'])
def get_patch_delete(user_id):
def get_patch_delete_user(user_id):
query = supabase.table("Users").select("*").eq("id", user_id).execute()
user = query.data
if not user:
Expand Down
4 changes: 2 additions & 2 deletions trivia-forge/backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
app = Flask(__name__)
app.register_blueprint(home.bp)
app.register_blueprint(user.bp)
# app.register_blueprint(game.bp)
# app.register_blueprint(category.bp)
app.register_blueprint(game.bp)
app.register_blueprint(category.bp)
# app.register_blueprint(question.bp)
# app.register_blueprint(choice.bp)

Expand Down

0 comments on commit 2eddf96

Please sign in to comment.