diff --git a/trivia-forge/frontend/.gitignore b/trivia-forge/.gitignore similarity index 93% rename from trivia-forge/frontend/.gitignore rename to trivia-forge/.gitignore index 8307a1e1..7059399d 100644 --- a/trivia-forge/frontend/.gitignore +++ b/trivia-forge/.gitignore @@ -15,6 +15,9 @@ dist-ssr *.local openai-env +__pycache__ +.venv + # Editor directories and files .vscode/* !.vscode/extensions.json diff --git a/trivia-forge/backend/__pycache__/config.cpython-310.pyc b/trivia-forge/backend/__pycache__/config.cpython-310.pyc deleted file mode 100644 index 922ee1cb..00000000 Binary files a/trivia-forge/backend/__pycache__/config.cpython-310.pyc and /dev/null differ diff --git a/trivia-forge/backend/__pycache__/model.cpython-310.pyc b/trivia-forge/backend/__pycache__/model.cpython-310.pyc deleted file mode 100644 index 472fe1b6..00000000 Binary files a/trivia-forge/backend/__pycache__/model.cpython-310.pyc and /dev/null differ diff --git a/trivia-forge/backend/app.py b/trivia-forge/backend/app.py deleted file mode 100644 index 7e0f82cb..00000000 --- a/trivia-forge/backend/app.py +++ /dev/null @@ -1,11 +0,0 @@ -import os -from flask import Flask, render_template, json, redirect - -app = Flask(__name__) - -SUPABASE_URL = os.getenv('SUPABASE_URL') -SUPABASE_ANON_KEY = os.getenv('SUPABASE_ANON_KEY') - -@app.route('/') -def home(): - return 'Hello, World!' diff --git a/trivia-forge/backend/config.py b/trivia-forge/backend/config.py deleted file mode 100644 index de099d19..00000000 --- a/trivia-forge/backend/config.py +++ /dev/null @@ -1,22 +0,0 @@ -#from flask_sqlalchemy import SQLAlchemy -from flask import Flask -#from flask_cors import CORS -from supabase import create_client, Client -#from sqlalchemy.orm import sessionmaker -from dotenv import dotenv_values - -def create_app(): - - app = Flask(__name__) - config = dotenv_values("./.env") - url: str = config.get('SUPABASE_URL') - #print("url:",url) - key: str = config.get('SUPABASE_KEY') - #print("key:",key) - supabase: Client = create_client(url, key) - app.config['Client'] = supabase - return app - - -if __name__ == '__main__': - create_app() diff --git a/trivia-forge/backend/endpoints/category.py b/trivia-forge/backend/endpoints/category.py new file mode 100644 index 00000000..e69de29b diff --git a/trivia-forge/backend/endpoints/choice.py b/trivia-forge/backend/endpoints/choice.py new file mode 100644 index 00000000..e69de29b diff --git a/trivia-forge/backend/endpoints/game.py b/trivia-forge/backend/endpoints/game.py new file mode 100644 index 00000000..e69de29b diff --git a/trivia-forge/backend/endpoints/home.py b/trivia-forge/backend/endpoints/home.py new file mode 100644 index 00000000..af82a78e --- /dev/null +++ b/trivia-forge/backend/endpoints/home.py @@ -0,0 +1,9 @@ +from flask import Blueprint + + +bp = Blueprint('home', __name__, url_prefix='/') + + +@bp.get('') +def get_home(): + return 'Welcome to Trivia Forge!' diff --git a/trivia-forge/backend/endpoints/question.py b/trivia-forge/backend/endpoints/question.py new file mode 100644 index 00000000..e69de29b diff --git a/trivia-forge/backend/endpoints/user.py b/trivia-forge/backend/endpoints/user.py new file mode 100644 index 00000000..bc74e29d --- /dev/null +++ b/trivia-forge/backend/endpoints/user.py @@ -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('/', methods=['GET', 'PATCH', 'DELETE']) +def get_patch_delete(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 {} diff --git a/trivia-forge/backend/main.py b/trivia-forge/backend/main.py index 48e02371..cc04be27 100644 --- a/trivia-forge/backend/main.py +++ b/trivia-forge/backend/main.py @@ -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 - } - -@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) \ No newline at end of file + app.run(debug=True) diff --git a/trivia-forge/backend/model.py b/trivia-forge/backend/model.py deleted file mode 100644 index 8b137891..00000000 --- a/trivia-forge/backend/model.py +++ /dev/null @@ -1 +0,0 @@ - diff --git a/trivia-forge/backend/requirements.txt b/trivia-forge/backend/requirements.txt new file mode 100644 index 00000000..fce8188e --- /dev/null +++ b/trivia-forge/backend/requirements.txt @@ -0,0 +1,5 @@ +# use command to install the following + +flask +supabase +python-dotenv \ No newline at end of file