Skip to content

Commit 60f5164

Browse files
Merge pull request #3 from dimitris-thomopoulos:feature/create-item-endpoint
Add POST endpoint to create items
2 parents 264fa72 + 71e4e54 commit 60f5164

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

app/main.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,19 @@ async def delete_item(item_id: int):
8787
return {"deleted": True, "id": item_id}
8888

8989

90+
@app.post("/api/items")
91+
async def create_item(name: str, description: str):
92+
"""Create a new item."""
93+
new_id = max(item["id"] for item in items) + 1
94+
new_item = {
95+
"id": new_id,
96+
"name": name,
97+
"description": description,
98+
"created": True,
99+
}
100+
items.append(new_item)
101+
return new_item
102+
103+
90104
if __name__ == "__main__":
91-
uvicorn.run(app, host="0.0.0.0", port=8000)
105+
uvicorn.run(app, host="0.0.0.0", port=8000) # nosec B104

tests/test_main.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,21 @@ def test_search_items_no_results():
126126
response = client.get("/api/items/search?name=xyz_not_existing")
127127
assert response.status_code == 200
128128
data = response.json()
129-
assert data["items"] == []
129+
assert data["items"] == []
130+
131+
132+
def test_create_item():
133+
"""Test the create item endpoint."""
134+
response = client.post(
135+
"/api/items?name=Learn+guitar&description=Practice+30+minutes+daily"
136+
)
137+
assert response.status_code == 200
138+
data = response.json()
139+
assert data["name"] == "Learn guitar"
140+
assert data["description"] == "Practice 30 minutes daily"
141+
assert data["created"] is True
142+
assert data["id"] == 6 # Should be next after the 5 existing items
143+
144+
# Verify it appears in the list
145+
response = client.get("/api/items")
146+
assert len(response.json()["items"]) == 6

0 commit comments

Comments
 (0)