Skip to content

Commit d75dd72

Browse files
Copilotks6088ts
andcommitted
Refactor items router: remove path prefixes and move config to app.py
Co-authored-by: ks6088ts <[email protected]>
1 parent d9b4990 commit d75dd72

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

template_fastapi/app.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,18 @@ def server_request_hook(span: Span, scope: dict):
3636
FastAPIInstrumentor.instrument_app(app)
3737

3838
# Include routers
39-
app.include_router(items.router)
39+
app.include_router(
40+
router=items.router,
41+
prefix="/items",
42+
tags=[
43+
"items",
44+
],
45+
responses={
46+
404: {
47+
"description": "Not found",
48+
},
49+
},
50+
)
4051
app.include_router(demos.router)
4152
app.include_router(games.router)
4253
app.include_router(foodies.router)

template_fastapi/routers/items.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
router = APIRouter()
77

88

9-
@router.get("/items/", response_model=list[Item], tags=["items"], operation_id="list_items")
9+
@router.get("/", response_model=list[Item], operation_id="list_items")
1010
async def list_items(skip: int = 0, limit: int = 10):
1111
"""
1212
List all items in the database.
@@ -16,7 +16,7 @@ async def list_items(skip: int = 0, limit: int = 10):
1616
return list(items_db.values())[skip : skip + limit]
1717

1818

19-
@router.get("/items/{item_id}", response_model=Item, tags=["items"], operation_id="get_item")
19+
@router.get("/{item_id}", response_model=Item, operation_id="get_item")
2020
async def read_item(item_id: int):
2121
"""
2222
Get a specific item by its ID.
@@ -28,7 +28,7 @@ async def read_item(item_id: int):
2828
return items_db[item_id]
2929

3030

31-
@router.post("/items/", response_model=Item, tags=["items"], operation_id="create_item")
31+
@router.post("/", response_model=Item, operation_id="create_item")
3232
async def create_item(item: Item):
3333
"""
3434
Create a new item in the database.
@@ -39,7 +39,7 @@ async def create_item(item: Item):
3939
return item
4040

4141

42-
@router.put("/items/{item_id}", response_model=Item, tags=["items"], operation_id="update_item")
42+
@router.put("/{item_id}", response_model=Item, operation_id="update_item")
4343
async def update_item(item_id: int, item: Item):
4444
"""
4545
Update an existing item.
@@ -54,7 +54,7 @@ async def update_item(item_id: int, item: Item):
5454
return item
5555

5656

57-
@router.delete("/items/{item_id}", tags=["items"], operation_id="delete_item")
57+
@router.delete("/{item_id}", operation_id="delete_item")
5858
async def delete_item(item_id: int):
5959
"""
6060
Delete an item from the database.
@@ -68,7 +68,7 @@ async def delete_item(item_id: int):
6868
return {"message": "Item deleted successfully"}
6969

7070

71-
@router.get("/items/search/", response_model=list[Item], tags=["search"], operation_id="search_items")
71+
@router.get("/search/", response_model=list[Item], tags=["search"], operation_id="search_items")
7272
async def search_items(
7373
q: str | None = Query(None, description="Search query string"),
7474
min_price: float | None = Query(None, description="Minimum price"),

0 commit comments

Comments
 (0)