-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
46 lines (34 loc) · 1.44 KB
/
main.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
from dto.itemorigin import ItemOrigin
from dto.inventoryitem import InventoryItem
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from typing import Dict, List
app = FastAPI()
my_inventory_item_dict: Dict[str, InventoryItem] = {}
@app.put("/items/{serial_num}")
def create_item(item: InventoryItem, serial_num: str) -> None:
my_inventory_item_dict[serial_num] = item
print(my_inventory_item_dict)
# Get API Test - if serial number is not given then this is the "Get-All-API"
@app.get("/items/{serial_num}")
def get_item(serial_num: str) -> InventoryItem:
if serial_num is None: # Another check for if the serial number is not given
print(my_inventory_item_dict.values())
return list(my_inventory_item_dict.values())
else:
if serial_num in my_inventory_item_dict.keys():
return my_inventory_item_dict[serial_num]
else:
raise HTTPException(status_code=404, detail="Item not found:" + serial_num)
# Delete Test
@app.delete("/items/{serial_num}")
def delete_item(serial_num: str) -> Dict:
if serial_num in my_inventory_item_dict.keys():
my_inventory_item_dict.pop(serial_num)
print(my_inventory_item_dict)
return {"msg": "Successfully deleted"}
else:
raise HTTPException(status_code=404, detail="Item not found : " + serial_num)
@app.get("/items/")
def get_items() -> List[InventoryItem]:
return my_inventory_item_dict.values()