-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2946a6f
commit 1e55e6a
Showing
14 changed files
with
81 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |