forked from wpcodevo/nextjs-flask-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
71 lines (53 loc) · 1.71 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from flask import Flask, request
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
todos = []
todo_id_counter = 1
@app.route("/api/todos", methods=["GET"])
def get_all_todo_items():
return todos
@app.route("/api/todos/<int:todo_id>", methods=["GET"])
def get_todo_item(todo_id):
todo = next((todo for todo in todos if todo["id"] == todo_id), None)
if todo:
return todo
return {"error": "Todo item not found"}, 404
@app.route("/api/todos", methods=["POST"])
def create_todo_item():
data = request.get_json()
title = data.get("title")
if not title:
return {"error": "Title is required"}, 400
global todo_id_counter
todo = {
"id": todo_id_counter,
"title": title,
"completed": False
}
todos.append(todo)
todo_id_counter += 1
return todo, 201
@app.route("/api/todos/<int:todo_id>", methods=["PATCH"])
def update_todo_item(todo_id):
data = request.get_json()
title = data.get("title")
completed = data.get("completed")
todo = next((todo for todo in todos if todo["id"] == todo_id), None)
if todo:
if title is not None:
todo["title"] = title
if completed is not None:
todo["completed"] = completed
return todo
return {"error": "Todo item not found"}, 404
@app.route("/api/todos/<int:todo_id>", methods=["DELETE"])
def delete_todo_item(todo_id):
global todos
todos = [todo for todo in todos if todo["id"] != todo_id]
return {"message": "Todo item deleted"}
@app.route("/api/healthchecker", methods=["GET"])
def healthchecker():
return {"status": "success", "message": "Integrate Flask Framework with Next.js"}
if __name__ == "__main__":
app.run()