forked from microsoft/python-sample-vscode-fastapi-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
84 lines (67 loc) · 2.83 KB
/
main.py
File metadata and controls
84 lines (67 loc) · 2.83 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
79
80
81
82
83
84
import os
from fastapi import FastAPI, HTTPException, Request
from models import ItemPayload
app = FastAPI()
grocery_list: dict[int, ItemPayload] = {}
@app.get("/")
def home(request: Request) -> dict[str, str]:
url: str = (
f"https://{os.getenv('CODESPACE_NAME')}-8000.{os.getenv('GITHUB_CODESPACES_PORT_FORWARDING_DOMAIN')}/"
if os.getenv("CODESPACE_NAME")
else str(request.base_url)
)
return {
"message": f"Navigate to the following URL to access the Swagger UI: {url}docs"
}
# Route to add an item
@app.post("/items/{item_name}/{quantity}")
def add_item(item_name: str, quantity: int) -> dict[str, ItemPayload]:
if quantity <= 0:
raise HTTPException(status_code=400, detail="Quantity must be greater than 0.")
# if item already exists, we'll just add the quantity.
# get all item names
items_ids: dict[str, int] = {
item.item_name: item.item_id if item.item_id is not None else 0
for item in grocery_list.values()
}
if item_name in items_ids.keys():
# get index of item_name in item_ids, which is the item_id
item_id: int = items_ids[item_name]
grocery_list[item_id].quantity += quantity
# otherwise, create a new item
else:
# generate an ID for the item based on the highest ID in the grocery_list
item_id: int = max(grocery_list.keys()) + 1 if grocery_list else 0
grocery_list[item_id] = ItemPayload(
item_id=item_id, item_name=item_name, quantity=quantity
)
return {"item": grocery_list[item_id]}
# Route to list a specific item by id
@app.get("/items/{item_id}")
def list_item(item_id: int) -> dict[str, ItemPayload]:
if item_id not in grocery_list:
raise HTTPException(status_code=404, detail="Item not found.")
return {"item": grocery_list[item_id]}
# Route to list all items
@app.get("/items")
def list_items() -> dict[str, dict[int, ItemPayload]]:
return {"items": grocery_list}
# Route to delete a specific item by id
@app.delete("/items/{item_id}")
def delete_item(item_id: int) -> dict[str, str]:
if item_id not in grocery_list:
raise HTTPException(status_code=404, detail="Item not found.")
del grocery_list[item_id]
return {"result": "Item deleted."}
# Route to remove some quantity of a specific item by id
@app.delete("/items/{item_id}/{quantity}")
def remove_quantity(item_id: int, quantity: int) -> dict[str, str]:
if item_id not in grocery_list:
raise HTTPException(status_code=404, detail="Item not found.")
# if quantity to be removed is higher or equal to item's quantity, delete the item
if grocery_list[item_id].quantity <= quantity:
del grocery_list[item_id]
return {"result": "Item deleted."}
else:
grocery_list[item_id].quantity -= quantity
return {"result": f"{quantity} items removed."}