-
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.
Merge remote-tracking branch 'origin/main' into jbranch4
merging with main
- Loading branch information
Showing
13 changed files
with
264 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.
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,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 {} |
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,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 {} |
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,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 {} |
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!' |
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,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 {} |
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(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 | ||
} | ||
#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) |
This file was deleted.
Oops, something went wrong.