backend: add suggestions CRUD
This commit is contained in:
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
|
||||
Reference in New Issue
Block a user