Change search parameter name from 'query' to 'filter'
This commit is contained in:
parent
6435ac6d82
commit
8dd3d8dd8b
@ -101,7 +101,7 @@ The API will be available at http://localhost:8000.
|
|||||||
|
|
||||||
- `GET /api/v1/items` - List all items (with pagination, filtering, and search)
|
- `GET /api/v1/items` - List all items (with pagination, filtering, and search)
|
||||||
- Query Parameters:
|
- Query Parameters:
|
||||||
- `query` - Search text in name and description
|
- `filter` - Filter text to match in name and description
|
||||||
- `active` - Filter by active status (true/false)
|
- `active` - Filter by active status (true/false)
|
||||||
- `price_min` - Filter by minimum price (in cents)
|
- `price_min` - Filter by minimum price (in cents)
|
||||||
- `price_max` - Filter by maximum price (in cents)
|
- `price_max` - Filter by maximum price (in cents)
|
||||||
@ -110,7 +110,7 @@ The API will be available at http://localhost:8000.
|
|||||||
|
|
||||||
- `GET /api/v1/items/search` - Search for items by name or description
|
- `GET /api/v1/items/search` - Search for items by name or description
|
||||||
- Query Parameters:
|
- Query Parameters:
|
||||||
- `query` - Search text in name and description (required)
|
- `filter` - Filter text to match in name and description (required)
|
||||||
- `active` - Filter by active status (true/false)
|
- `active` - Filter by active status (true/false)
|
||||||
- `price_min` - Filter by minimum price (in cents)
|
- `price_min` - Filter by minimum price (in cents)
|
||||||
- `price_max` - Filter by maximum price (in cents)
|
- `price_max` - Filter by maximum price (in cents)
|
||||||
@ -133,7 +133,7 @@ The API supports searching items by:
|
|||||||
|
|
||||||
Example search request:
|
Example search request:
|
||||||
```
|
```
|
||||||
GET /api/v1/items/search?query=phone&active=true&price_min=10000&price_max=50000
|
GET /api/v1/items/search?filter=phone&active=true&price_min=10000&price_max=50000
|
||||||
```
|
```
|
||||||
|
|
||||||
This will find all active items that contain "phone" in their name or description, with a price between 100.00 and 500.00 (prices are stored in cents).
|
This will find all active items that contain "phone" in their name or description, with a price between 100.00 and 500.00 (prices are stored in cents).
|
||||||
|
@ -18,7 +18,7 @@ router = APIRouter()
|
|||||||
)
|
)
|
||||||
def read_items(
|
def read_items(
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
query: Optional[str] = Query(None, description="Search text in name and description"),
|
filter: Optional[str] = Query(None, description="Filter text to match in name and description"),
|
||||||
active: Optional[bool] = Query(None, description="Filter by active status"),
|
active: Optional[bool] = Query(None, description="Filter by active status"),
|
||||||
price_min: Optional[int] = Query(None, description="Minimum price in cents"),
|
price_min: Optional[int] = Query(None, description="Minimum price in cents"),
|
||||||
price_max: Optional[int] = Query(None, description="Maximum price in cents"),
|
price_max: Optional[int] = Query(None, description="Maximum price in cents"),
|
||||||
@ -27,16 +27,16 @@ def read_items(
|
|||||||
) -> Any:
|
) -> Any:
|
||||||
"""
|
"""
|
||||||
Retrieve items with support for:
|
Retrieve items with support for:
|
||||||
- Text search in name and description
|
- Text filtering in name and description
|
||||||
- Filtering by active status
|
- Filtering by active status
|
||||||
- Price range filtering
|
- Price range filtering
|
||||||
- Pagination with skip and limit parameters
|
- Pagination with skip and limit parameters
|
||||||
"""
|
"""
|
||||||
# If search query or price filters are provided, use search method
|
# If search filter or price filters are provided, use search method
|
||||||
if query is not None or price_min is not None or price_max is not None:
|
if filter is not None or price_min is not None or price_max is not None:
|
||||||
items = crud.item.search(
|
items = crud.item.search(
|
||||||
db,
|
db,
|
||||||
query=query or "",
|
filter_text=filter or "",
|
||||||
skip=skip,
|
skip=skip,
|
||||||
limit=limit,
|
limit=limit,
|
||||||
active=active,
|
active=active,
|
||||||
@ -57,10 +57,10 @@ def read_items(
|
|||||||
response_model=List[schemas.Item],
|
response_model=List[schemas.Item],
|
||||||
status_code=status.HTTP_200_OK,
|
status_code=status.HTTP_200_OK,
|
||||||
summary="Search items",
|
summary="Search items",
|
||||||
description="Search for items by query text in name and description with additional filters",
|
description="Search for items by filter text in name and description with additional filters",
|
||||||
)
|
)
|
||||||
def search_items(
|
def search_items(
|
||||||
query: str = Query(..., description="Search text in name and description"),
|
filter: str = Query(..., description="Filter text to match in name and description"),
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
active: Optional[bool] = Query(None, description="Filter by active status"),
|
active: Optional[bool] = Query(None, description="Filter by active status"),
|
||||||
price_min: Optional[int] = Query(None, description="Minimum price in cents"),
|
price_min: Optional[int] = Query(None, description="Minimum price in cents"),
|
||||||
@ -70,14 +70,14 @@ def search_items(
|
|||||||
) -> Any:
|
) -> Any:
|
||||||
"""
|
"""
|
||||||
Search for items with support for:
|
Search for items with support for:
|
||||||
- Text search in name and description (required)
|
- Text filtering in name and description (required)
|
||||||
- Filtering by active status
|
- Filtering by active status
|
||||||
- Price range filtering
|
- Price range filtering
|
||||||
- Pagination with skip and limit parameters
|
- Pagination with skip and limit parameters
|
||||||
"""
|
"""
|
||||||
items = crud.item.search(
|
items = crud.item.search(
|
||||||
db,
|
db,
|
||||||
query=query,
|
filter_text=filter,
|
||||||
skip=skip,
|
skip=skip,
|
||||||
limit=limit,
|
limit=limit,
|
||||||
active=active,
|
active=active,
|
||||||
|
@ -24,14 +24,14 @@ class CRUDItem(CRUDBase[Item, ItemCreate, ItemUpdate]):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def search(
|
def search(
|
||||||
self, db: Session, *, query: str, skip: int = 0, limit: int = 100, **filters: Any
|
self, db: Session, *, filter_text: str, skip: int = 0, limit: int = 100, **filters: Any
|
||||||
) -> List[Item]:
|
) -> List[Item]:
|
||||||
"""
|
"""
|
||||||
Search for items based on the provided query string and additional filters.
|
Search for items based on the provided filter text and additional filters.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
db: Database session
|
db: Database session
|
||||||
query: Search query to match against name and description
|
filter_text: Search filter text to match against name and description
|
||||||
skip: Number of records to skip
|
skip: Number of records to skip
|
||||||
limit: Maximum number of records to return
|
limit: Maximum number of records to return
|
||||||
**filters: Additional filters (e.g., active=True)
|
**filters: Additional filters (e.g., active=True)
|
||||||
@ -41,11 +41,11 @@ class CRUDItem(CRUDBase[Item, ItemCreate, ItemUpdate]):
|
|||||||
"""
|
"""
|
||||||
db_query = db.query(Item)
|
db_query = db.query(Item)
|
||||||
|
|
||||||
# Apply search query to name and description
|
# Apply search filter to name and description
|
||||||
if query:
|
if filter_text:
|
||||||
search_filter = or_(
|
search_filter = or_(
|
||||||
Item.name.ilike(f"%{query}%"),
|
Item.name.ilike(f"%{filter_text}%"),
|
||||||
Item.description.ilike(f"%{query}%"),
|
Item.description.ilike(f"%{filter_text}%"),
|
||||||
)
|
)
|
||||||
db_query = db_query.filter(search_filter)
|
db_query = db_query.filter(search_filter)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user