Skip to content

Commit

Permalink
Merge pull request #17 from emmanueposu/pbranch
Browse files Browse the repository at this point in the history
Updated backend
  • Loading branch information
justinstoner2 authored May 2, 2024
2 parents 1b896ce + 4d4bb7f commit 77d14a6
Show file tree
Hide file tree
Showing 13 changed files with 264 additions and 74 deletions.
3 changes: 3 additions & 0 deletions trivia-forge/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ dist-ssr
*.local
openai-env

__pycache__
.venv

# Editor directories and files
.vscode/*
!.vscode/extensions.json
Expand Down
Binary file removed trivia-forge/backend/__pycache__/config.cpython-310.pyc
Binary file not shown.
Binary file removed trivia-forge/backend/__pycache__/model.cpython-310.pyc
Binary file not shown.
11 changes: 0 additions & 11 deletions trivia-forge/backend/app.py

This file was deleted.

22 changes: 0 additions & 22 deletions trivia-forge/backend/config.py

This file was deleted.

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/choice.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('choice', __name__, url_prefix='/choices')

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_choices():
if request.method == 'POST':
data = request.get_json()
try:
new_choice = supabase.table("Choices").insert(data).execute()
return jsonify(new_choice.data[0])
except Exception as e:
return {"error": e.details}
elif request.method == 'GET':
query = supabase.table("Choices").select("*").execute()
choices = query.data
if not choices:
return {"error": "No choices found"}
return jsonify([choice for choice in choices])


@bp.route('/<choice_id>', methods=['GET', 'PATCH', 'DELETE'])
def get_patch_delete_choice(choice_id):
query = supabase.table("Choices").select("*").eq("id", choice_id).execute()
choice = query.data
if not choice:
return {"error": "Choice not found"}
elif request.method == 'GET':
return jsonify(choice[0])
elif request.method == 'PATCH':
data = request.get_json()
updated_data = {'text': data['text']}
updated_choice = supabase.table("Choices").update(updated_data).eq("id", choice_id).execute()
return jsonify(updated_choice.data[0])
elif request.method == 'DELETE':
supabase.table("Choices").delete().eq("id", choice_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 {}
9 changes: 9 additions & 0 deletions trivia-forge/backend/endpoints/home.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from flask import Blueprint


bp = Blueprint('home', __name__, url_prefix='/')


@bp.get('')
def get_home():
return 'Welcome to Trivia Forge!'
50 changes: 50 additions & 0 deletions trivia-forge/backend/endpoints/question.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from flask import Blueprint, request, jsonify
from supabase import create_client, Client
from dotenv import dotenv_values


bp = Blueprint('question', __name__, url_prefix='/questions')

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_questions():
if request.method == 'POST':
data = request.get_json()
try:
new_question = supabase.table("Questions").insert(data).execute()
return jsonify(new_question.data[0])
except Exception as e:
return {"error": e.details}
elif request.method == 'GET':
query = supabase.table("Questions").select("*").execute()
questions = query.data
if not questions:
return {"error": "No questions found"}
return jsonify([question for question in questions])


@bp.route('/<question_id>', methods=['GET', 'PATCH', 'DELETE'])
def get_patch_delete_question(question_id):
query = supabase.table("Questions").select("*").eq("id", question_id).execute()
question = query.data
if not question:
return {"error": "Question not found"}
elif request.method == 'GET':
return jsonify(question[0])
elif request.method == 'PATCH':
data = request.get_json()
updated_data = {'problem': data['problem'],
'answer': data['answer'],
'multiple_choice': data['multiple_choice'],
'hint': data['hint'],
'category_id': data['category_id']}
updated_question = supabase.table("Questions").update(updated_data).eq("id", question_id).execute()
return jsonify(updated_question.data[0])
elif request.method == 'DELETE':
supabase.table("Questions").delete().eq("id", question_id).execute()
return {}
55 changes: 55 additions & 0 deletions trivia-forge/backend/endpoints/user.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from flask import Blueprint, request, jsonify
from supabase import create_client, Client
from dotenv import dotenv_values


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

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


def serialize_user(user):
"""Convert user dictionary to a JSON serializable format."""
return {
"id": user.get("id"),
"username": user.get("username"),
"email": user.get("email")
}


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


@bp.route('/<user_id>', methods=['GET', 'PATCH', 'DELETE'])
def get_patch_delete_user(user_id):
query = supabase.table("Users").select("*").eq("id", user_id).execute()
user = query.data
if not user:
return {"error": "User not found"}
elif request.method == 'GET':
return jsonify(serialize_user(user[0]))
elif request.method == 'PATCH':
data = request.get_json()
updated_data = {'password': data['password'], 'email': data['email']}
updated_user = supabase.table("Users").update(updated_data).eq("id", user_id).execute()
return jsonify(updated_user.data[0])
elif request.method == 'DELETE':
supabase.table("Users").delete().eq("id", user_id).execute()
return {}
49 changes: 9 additions & 40 deletions trivia-forge/backend/main.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,15 @@
from flask import Flask, request, jsonify
from config import create_app
from dotenv import load_dotenv
from endpoints import home, user, game, category, question, choice

app = create_app()

def test_supabase_connection():
try:
# Replace 'your_table' with an actual table name to test fetching data
supabase = app.config['Client'] # Ensure this is consistent across your app
data = supabase.table("Users").select("*").execute()
if data:
print("Data fetched successfully:", data.data)
return True, "Database connection was successful."

print("Failed to fetch data:", data.error_message)
return False, data.error_message
except Exception as e:
print("An error occurred during the database connection:", e)
return False, str(e)
def serialize_user(user):
"""Convert user dictionary to a JSON serializable format."""
return {
"id": user.get("id"),
"name": user.get("name"),
"email": user.get("email")
# Add more fields as needed based on your database structure
}
#random comment
@app.route("/users", methods=['GET','POST'])
def get_users():
supabase = app.config['Client']
data = supabase.table("Users").select("*").execute()
users = data.data
# Assuming you have a function to serialize data as JSON
return jsonify([serialize_user(user) for user in users])

@app.route("/test", methods=['GET'])
def test():
success, message = test_supabase_connection()
return jsonify({"success": success, "message": message})

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(question.bp)
app.register_blueprint(choice.bp)


if __name__ == '__main__':
app.run(debug=True)
app.run(debug=True)
1 change: 0 additions & 1 deletion trivia-forge/backend/model.py

This file was deleted.

0 comments on commit 77d14a6

Please sign in to comment.