feat: Update endpoint loading
This commit is contained in:
parent
25ed23f2e7
commit
513cc811dc
@ -0,0 +1,57 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException
|
||||||
|
from core.database import fake_users_db
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
# Demo items database
|
||||||
|
fake_items_db = [
|
||||||
|
{"id": "1", "name": "Item 1", "description": "First item"},
|
||||||
|
{"id": "2", "name": "Item 2", "description": "Second item"},
|
||||||
|
{"id": "3", "name": "Item 3", "description": "Third item"}
|
||||||
|
]
|
||||||
|
|
||||||
|
@router.post("/items")
|
||||||
|
async def get_items(
|
||||||
|
ids: Optional[List[str]] = None,
|
||||||
|
skip: int = 0,
|
||||||
|
limit: int = 10
|
||||||
|
):
|
||||||
|
"""Get items from array with optional filtering"""
|
||||||
|
if ids:
|
||||||
|
items = [item for item in fake_items_db if item["id"] in ids]
|
||||||
|
if not items:
|
||||||
|
raise HTTPException(status_code=404, detail="No items found with provided IDs")
|
||||||
|
else:
|
||||||
|
items = fake_items_db[skip:skip + limit]
|
||||||
|
|
||||||
|
return {
|
||||||
|
"message": "Items retrieved successfully",
|
||||||
|
"data": {
|
||||||
|
"items": items,
|
||||||
|
"total": len(items)
|
||||||
|
},
|
||||||
|
"metadata": {
|
||||||
|
"skip": skip,
|
||||||
|
"limit": limit,
|
||||||
|
"filtered": bool(ids)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@router.post("/items/{item_id}")
|
||||||
|
async def get_item_by_id(
|
||||||
|
item_id: str
|
||||||
|
):
|
||||||
|
"""Get single item by ID"""
|
||||||
|
item = next((item for item in fake_items_db if item["id"] == item_id), None)
|
||||||
|
if not item:
|
||||||
|
raise HTTPException(status_code=404, detail="Item not found")
|
||||||
|
|
||||||
|
return {
|
||||||
|
"message": "Item retrieved successfully",
|
||||||
|
"data": item,
|
||||||
|
"metadata": {
|
||||||
|
"source": "demo_items_db",
|
||||||
|
"result_count": 1
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user