Compare commits
7 Commits
ce5c7d237d
...
0892f2aa49
| Author | SHA1 | Date | |
|---|---|---|---|
| 0892f2aa49 | |||
| 5f8fb41787 | |||
| e282f19de0 | |||
| 16d4194836 | |||
| d37f2af737 | |||
| 352dfebdeb | |||
| 3390984681 |
57
app.py
57
app.py
@@ -1,4 +1,59 @@
|
|||||||
from flask import Flask
|
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"}
|
||||||
|
]
|
||||||
|
id_counter = 5
|
||||||
|
|
||||||
app = Flask(__name__)
|
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():
|
||||||
|
global id_counter
|
||||||
|
j = request.get_json()
|
||||||
|
j["id"] = id_counter
|
||||||
|
id_counter += 1
|
||||||
|
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)
|
app.run(debug=True)
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
haiiiii :3
|
||||||
Reference in New Issue
Block a user