backend: add suggestions CRUD
This commit is contained in:
parent
78b04811b9
commit
21987f02cc
@ -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):
|
||||
|
||||
@ -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():
|
||||
|
||||
104
backend/src/suggestions.py
Normal file
104
backend/src/suggestions.py
Normal file
@ -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/<int:id>', 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/<int:id>', 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/<int:id>', 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
|
||||
Loading…
x
Reference in New Issue
Block a user