57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
from flask import Flask, jsonify, request
|
|
|
|
compiti = [
|
|
{"id": 1, "descrizione": "Compito matematica"},
|
|
{"id": 2, "descrizione": "Consegna TPSIT progetto API REST"},
|
|
{"id": 3, "descrizione": "Compito di sistemi"},
|
|
{"id": 4, "descrizione": "Compito di italiano"}
|
|
]
|
|
|
|
app = Flask(__name__)
|
|
|
|
@app.route("/", methods=["GET"])
|
|
def home():
|
|
html = "<h1>Home page API REST Compiti</h1><h2>ciao, 3cx merda</h2>"
|
|
html += "<a href=\"/doc.html\">Documentazione</a>"
|
|
return html, 200
|
|
|
|
@app.route("/<path:filename>", methods=["GET"])
|
|
def template(filename):
|
|
if not filename.endswith(".html"):
|
|
return "File non valido", 404
|
|
try:
|
|
read_file = open(f"templates/{filename}", "r")
|
|
content = read_file.read()
|
|
read_file.close()
|
|
return content, 200
|
|
except FileNotFoundError:
|
|
return "File non trovato", 404
|
|
|
|
@app.route("/compiti", methods=["GET"])
|
|
def get_compiti():
|
|
return jsonify(compiti), 200
|
|
|
|
@app.route("/compiti/<int:id>", methods=["GET"])
|
|
def get_compito_by_id(id):
|
|
for ciscomerda in compiti:
|
|
if ciscomerda["id"] == id:
|
|
return jsonify(ciscomerda), 200
|
|
return jsonify([]), 404
|
|
|
|
@app.route("/compiti", methods=["POST"])
|
|
def create_compito():
|
|
j = request.get_json()
|
|
j["id"] = 69
|
|
compiti.append(j)
|
|
return jsonify(j), 201
|
|
|
|
@app.route("/compiti/<int:id>", methods=["DELETE"])
|
|
def delete_compito(id):
|
|
for ciscomerda in compiti:
|
|
if ciscomerda["id"] == id:
|
|
compiti.remove(ciscomerda)
|
|
return jsonify(ciscomerda), 200
|
|
return jsonify([]), 404
|
|
|
|
app.run(debug=True)
|