Skip to content

Commit

Permalink
Updated backend
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanueposu committed Apr 29, 2024
1 parent 2946a6f commit 1e55e6a
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 74 deletions.
3 changes: 3 additions & 0 deletions trivia-forge/frontend/.gitignore → 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.

Empty file.
Empty file.
Empty file.
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!'
Empty file.
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_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
}

@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.

5 changes: 5 additions & 0 deletions trivia-forge/backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# use command <pip install -r requirements.txt> to install the following

flask
supabase
python-dotenv

0 comments on commit 1e55e6a

Please sign in to comment.