From 8dd3d8dd8b8e9acb37b480c3e5ebf5554f839c0d Mon Sep 17 00:00:00 2001 From: Automated Action Date: Sat, 17 May 2025 16:36:36 +0000 Subject: [PATCH] Change search parameter name from 'query' to 'filter' --- README.md | 6 +++--- app/api/endpoints/items.py | 18 +++++++++--------- app/crud/crud_item.py | 14 +++++++------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 835142d..037be49 100644 --- a/README.md +++ b/README.md @@ -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) - 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) - `price_min` - Filter by minimum 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 - 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) - `price_min` - Filter by minimum price (in cents) - `price_max` - Filter by maximum price (in cents) @@ -133,7 +133,7 @@ The API supports searching items by: 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). diff --git a/app/api/endpoints/items.py b/app/api/endpoints/items.py index 053d230..fa1c109 100644 --- a/app/api/endpoints/items.py +++ b/app/api/endpoints/items.py @@ -18,7 +18,7 @@ router = APIRouter() ) def read_items( 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"), price_min: Optional[int] = Query(None, description="Minimum price in cents"), price_max: Optional[int] = Query(None, description="Maximum price in cents"), @@ -27,16 +27,16 @@ def read_items( ) -> Any: """ Retrieve items with support for: - - Text search in name and description + - Text filtering in name and description - Filtering by active status - Price range filtering - Pagination with skip and limit parameters """ - # If search query 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 search filter or price filters are provided, use search method + if filter is not None or price_min is not None or price_max is not None: items = crud.item.search( db, - query=query or "", + filter_text=filter or "", skip=skip, limit=limit, active=active, @@ -57,10 +57,10 @@ def read_items( response_model=List[schemas.Item], status_code=status.HTTP_200_OK, 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( - 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), active: Optional[bool] = Query(None, description="Filter by active status"), price_min: Optional[int] = Query(None, description="Minimum price in cents"), @@ -70,14 +70,14 @@ def search_items( ) -> Any: """ Search for items with support for: - - Text search in name and description (required) + - Text filtering in name and description (required) - Filtering by active status - Price range filtering - Pagination with skip and limit parameters """ items = crud.item.search( db, - query=query, + filter_text=filter, skip=skip, limit=limit, active=active, diff --git a/app/crud/crud_item.py b/app/crud/crud_item.py index 5d8de07..b99bc1b 100644 --- a/app/crud/crud_item.py +++ b/app/crud/crud_item.py @@ -24,14 +24,14 @@ class CRUDItem(CRUDBase[Item, ItemCreate, ItemUpdate]): ) 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]: """ - 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: 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 limit: Maximum number of records to return **filters: Additional filters (e.g., active=True) @@ -41,11 +41,11 @@ class CRUDItem(CRUDBase[Item, ItemCreate, ItemUpdate]): """ db_query = db.query(Item) - # Apply search query to name and description - if query: + # Apply search filter to name and description + if filter_text: search_filter = or_( - Item.name.ilike(f"%{query}%"), - Item.description.ilike(f"%{query}%"), + Item.name.ilike(f"%{filter_text}%"), + Item.description.ilike(f"%{filter_text}%"), ) db_query = db_query.filter(search_filter)