57 lines
1.5 KiB
Python
57 lines
1.5 KiB
Python
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
|
|
}
|
|
} |