-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
78 lines (54 loc) · 1.65 KB
/
Copy pathapp.py
File metadata and controls
78 lines (54 loc) · 1.65 KB
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
72
73
74
75
76
77
78
from flask import Flask, jsonify, request
from datetime import datetime, timezone
app = Flask(__name__)
VERSION = "0.1.0"
@app.route("/health")
def health():
return jsonify(
status="ok",
timestamp=datetime.now(timezone.utc).isoformat(),
version=VERSION,
)
@app.route("/items", methods=["GET"])
def list_items():
return jsonify(items=_get_items())
@app.route("/items", methods=["POST"])
def create_item():
data = request.get_json()
if not data or "name" not in data:
return jsonify(error="name is required"), 400
item = {
"id": len(_get_items()) + 1,
"name": data["name"],
"done": False,
}
_get_items().append(item)
return jsonify(item), 201
@app.route("/items/<int:item_id>", methods=["PATCH"])
def update_item(item_id):
items = _get_items()
item = next((i for i in items if i["id"] == item_id), None)
if item is None:
return jsonify(error="item not found"), 404
data = request.get_json()
if "done" in data:
item["done"] = data["done"]
if "name" in data:
item["name"] = data["name"]
return jsonify(item)
@app.route("/items/<int:item_id>", methods=["DELETE"])
def delete_item(item_id):
items = _get_items()
item = next((i for i in items if i["id"] == item_id), None)
if item is None:
return jsonify(error="item not found"), 404
items.remove(item)
return "", 204
_items_store = []
def _get_items():
return _items_store
def reset_items():
"""Reset the in-memory store (used by tests)."""
_items_store.clear()
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)