diff --git a/backend/src/db_client.py b/backend/src/db_client.py index 1b9c23c..a9850f0 100644 --- a/backend/src/db_client.py +++ b/backend/src/db_client.py @@ -22,9 +22,10 @@ STARTUP_TABLE_CREATION_QUERIES = { Car varchar(255), FreeCarSpaces tinyint(1) );""", - "food": """CREATE TABLE IF NOT EXISTS food ( - Name varchar(255), - Suggestion varchar(2048) + "suggestions": """CREATE TABLE IF NOT EXISTS suggestions ( + id INT PRIMARY KEY AUTO_INCREMENT, + Name VARCHAR(255), + Suggestion VARCHAR(2048) );""", "passengers": """CREATE TABLE IF NOT EXISTS passengers ( Name varchar(255), @@ -80,7 +81,7 @@ class DBClient: self.switch_database(self.database) self.query(STARTUP_TABLE_CREATION_QUERIES['users']) self.query(STARTUP_TABLE_CREATION_QUERIES['car']) - self.query(STARTUP_TABLE_CREATION_QUERIES['food']) + self.query(STARTUP_TABLE_CREATION_QUERIES['suggestions']) self.query(STARTUP_TABLE_CREATION_QUERIES['passengers']) def query(self, query_str, quiet=False, params=None): diff --git a/backend/src/server.py b/backend/src/server.py index e920d45..91735a7 100644 --- a/backend/src/server.py +++ b/backend/src/server.py @@ -10,12 +10,14 @@ from dotenv import load_dotenv from db_client import DBClient from car import registerCarEndpoints from user import registerUserEndpoints +from suggestions import registerSuggestionsEndpoints load_dotenv() app = Flask(__name__) database = DBClient() registerCarEndpoints(app=app, database=database) registerUserEndpoints(app=app, database=database) +registerSuggestionsEndpoints(app=app, database=database) @app.route('/login', methods=['POST']) def login(): diff --git a/backend/src/suggestions.py b/backend/src/suggestions.py new file mode 100644 index 0000000..48b94ca --- /dev/null +++ b/backend/src/suggestions.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python +# encoding: utf-8 + +""" +suggestions.py is a source for all suggestions endpoints. +""" + +from flask import request, jsonify + + +def registerSuggestionsEndpoints(app, database): + @app.route("/suggestions", methods=["GET"]) + def get_suggestions(): + if not request.is_json: + return jsonify({"error": "Request must contain JSON data"}), 400 + + data = request.get_json() + + if not data.get("name"): + return jsonify({"error": "Request must contain name field"}), 400 + + query = "SELECT * FROM suggestions WHERE name = %s" + output = database.query(query_str=query, params=(data["name"],)) + if not output: + return jsonify({"message": "No suggestions by that name exist"}), 404 + + response = {"name": output[0][1], "suggestions": []} + + for suggestion in output: + suggestion_data = { + "id": suggestion[0], + "suggestion": suggestion[2], + } + response["suggestions"].append(suggestion_data) + + return jsonify(response), 200 + + @app.route("/suggestions", methods=["POST"]) + def add_suggestions(): + if not request.is_json: + return jsonify({"error": "Request must contain JSON data"}), 400 + + data = request.get_json() + if not data.get("name") or not data.get("suggestion"): + return jsonify({"error": "JSON must contain name and suggestion fields"}), 400 + + query = "INSERT INTO suggestions (Name, Suggestion) VALUES (%s, %s)" + + try: + # Execute the query with the provided parameters + database.query(query_str=query, params=(data['name'], data['suggestion'])) + database.commit() + return jsonify({"message": "Suggestion added successfully"}), 201 + except Exception as e: + # Handle database errors + return jsonify({"error": str(e)}), 500 + + + @app.route('/suggestions/', methods=['GET']) + def get_suggestion(id): + query = 'SELECT * FROM suggestions WHERE id = %s' + output = database.query(query, params=(id,)) + + if not output: + return jsonify({"error": "No suggestion found with that ID"}), 404 + + response = { + "id": output[0][0], + "name": output[0][1], + "suggestion": output[0][2] + } + return jsonify(response), 200 + + @app.route('/suggestions/', methods=['PUT']) + def update_suggestion(id): + if not request.is_json: + return jsonify({"error": "Request must contain JSON data"}), 400 + + data = request.get_json() + if not data.get("name") or not data.get("suggestion"): + return ( + jsonify({"error": "JSON must contain name and suggestion fields"}), + 400, + ) + + query = 'UPDATE suggestions SET Suggestion = %s WHERE id = %s' + result = database.query(query, params=(data['suggestion'], id)) + database.commit() + + if result == 0: + return jsonify({"error": "No suggestion found with that ID"}), 404 + + return jsonify({"message": "Suggestion updated successfully"}), 200 + + @app.route('/suggestions/', methods=['DELETE']) + def delete_suggestion(id): + query = 'DELETE FROM suggestions WHERE id = %s' + result = database.query(query, params=(id,)) + database.commit() + + if result == 0: + return jsonify({"error": "No suggestion found with that ID"}), 404 + + return jsonify({"message": "Suggestion deleted successfully"}), 204 \ No newline at end of file