|
| 1 | +from enum import Enum |
| 2 | + |
| 3 | +from pydantic import BaseModel |
| 4 | + |
| 5 | +from fastapi import FastAPI, HTTPException |
| 6 | + |
| 7 | +app = FastAPI() |
| 8 | + |
| 9 | + |
| 10 | +class Category(Enum): |
| 11 | + TOOLS = "tools" |
| 12 | + CONSUMABLES = "consumables" |
| 13 | + |
| 14 | + |
| 15 | +class Item(BaseModel): |
| 16 | + name: str |
| 17 | + price: float |
| 18 | + count: int |
| 19 | + id: int |
| 20 | + category: Category |
| 21 | + |
| 22 | + |
| 23 | +items = { |
| 24 | + 0: Item(name="Hammer", price=9.99, count=20, id=0, category=Category.TOOLS), |
| 25 | + 1: Item(name="Pliers", price=5.99, count=20, id=1, category=Category.TOOLS), |
| 26 | + 2: Item(name="Nails", price=1.99, count=100, id=2, category=Category.CONSUMABLES), |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +@app.get("/") |
| 31 | +def index() -> dict[str, dict[int, Item]]: |
| 32 | + |
| 33 | + return {"items": items} |
| 34 | + |
| 35 | + |
| 36 | +@app.get("/items/{item_id}") |
| 37 | +def query_item_by_id(item_id: int) -> Item: |
| 38 | + |
| 39 | + if item_id not in items: |
| 40 | + HTTPException(status_code=404, detail=f"Item with {item_id=} does not exist.") |
| 41 | + |
| 42 | + return items[item_id] |
| 43 | + |
| 44 | + |
| 45 | +Selection = dict[ |
| 46 | + str, str | int | float | Category | None |
| 47 | +] # dictionary containing the user's query arguments |
| 48 | + |
| 49 | + |
| 50 | +@app.get("/items/") |
| 51 | +def query_item_by_parameters( |
| 52 | + name: str | None = None, |
| 53 | + price: float | None = None, |
| 54 | + count: int | None = None, |
| 55 | + category: Category | None = None, |
| 56 | +) -> dict[str, Selection | list[Item]]: |
| 57 | + def check_item(item: Item): |
| 58 | + """Check if the item matches the query arguments from the outer scope.""" |
| 59 | + return all( |
| 60 | + ( |
| 61 | + name is None or item.name == name, |
| 62 | + price is None or item.price == price, |
| 63 | + count is None or item.count != count, |
| 64 | + category is None or item.category is category, |
| 65 | + ) |
| 66 | + ) |
| 67 | + |
| 68 | + selection = [item for item in items.values() if check_item(item)] |
| 69 | + return { |
| 70 | + "query": {"name": name, "price": price, "count": count, "category": category}, |
| 71 | + "selection": selection, |
| 72 | + } |
| 73 | + |
| 74 | + |
| 75 | +@app.post("/") |
| 76 | +def add_item(item: Item) -> dict[str, Item]: |
| 77 | + |
| 78 | + if item.id in items: |
| 79 | + HTTPException(status_code=400, detail=f"Item with {item.id=} already exists.") |
| 80 | + |
| 81 | + items[item.id] = item |
| 82 | + return {"added": item} |
| 83 | + |
| 84 | + |
| 85 | +@app.put("/update/{item_id}") |
| 86 | +def update( |
| 87 | + item_id: int, |
| 88 | + name: str | None = None, |
| 89 | + price: float | None = None, |
| 90 | + count: int | None = None, |
| 91 | +) -> dict[str, Item]: |
| 92 | + |
| 93 | + if item_id not in items: |
| 94 | + HTTPException(status_code=404, detail=f"Item with {item_id=} does not exist.") |
| 95 | + if all(info is None for info in (name, price, count)): |
| 96 | + raise HTTPException( |
| 97 | + status_code=400, detail="No parameters provided for update." |
| 98 | + ) |
| 99 | + |
| 100 | + item = items[item_id] |
| 101 | + if name is not None: |
| 102 | + item.name = name |
| 103 | + if price is not None: |
| 104 | + item.price = price |
| 105 | + if count is not None: |
| 106 | + item.count = count |
| 107 | + |
| 108 | + return {"updated": item} |
| 109 | + |
| 110 | + |
| 111 | +@app.delete("/delete/{item_id}") |
| 112 | +def delete_item(item_id: int) -> dict[str, Item]: |
| 113 | + |
| 114 | + if item_id not in items: |
| 115 | + raise HTTPException( |
| 116 | + status_code=404, detail=f"Item with {item_id=} does not exist." |
| 117 | + ) |
| 118 | + |
| 119 | + item = items.pop(item_id) |
| 120 | + return {"deleted": item} |
0 commit comments