Problem statement
The product search handler declares no query parameters, so every search fails. Declare them so the optional filters fall back to their defaults, store_id is required, and every value arrives converted to its type.
Requirements
The app, the GET / home route, the product data in app/store.py, the find_products search in app/services/search.py and the body of the search_products handler are already provided. The body answers with the five values it used, then the total and results that find_products found. Your task has one part, in app/api/routes/products.py: declare the five query parameters of search_products.
A handler parameter that is not part of the path is a query parameter. Giving it a default makes it optional, leaving the default out makes it required, and its type tells FastAPI how to convert the text from the query string.
- store_id is a required integer. A request without it is rejected by FastAPI with a 422 validation error that names store_id as missing. store_id=2 arrives as the number 2, and a value that is not a whole number, such as store_id=main, is rejected with a 422 validation error
- term is optional text that defaults to the empty string. term=LaMp arrives as the text LaMp
- limit and offset are optional integers that default to 10 and 0. limit=4 arrives as the number 4, and a value that is not a whole number, such as limit=ten or offset=first, is rejected with a 422 validation error naming that parameter
- in_stock is an optional boolean that defaults to false. It reads true, 1, yes, y, t and on as true and false, 0, no, n, f and off as false, in any letter case. Any other value, such as maybe, is rejected with a 422 validation error naming in_stock
Sample Requests & Responses
GET /products/search?store_id=1&term=lamp&limit=2&offset=1
Response (200 OK):
{"store_id": 1, "term": "lamp", "limit": 2, "offset": 1, "in_stock": false, "total": 3, "results": [{"id": 2, "name": "Aurora Floor Lamp", "price": 89.0, "stock": 0, "store_id": 1}, {"id": 9, "name": "Harbor Pendant Lamp", "price": 64.0, "stock": 4, "store_id": 1}]}
GET /products/search?store_id=2&in_stock=yes
Response (200 OK):
{"store_id": 2, "term": "", "limit": 10, "offset": 0, "in_stock": true, "total": 2, "results": [{"id": 13, "name": "Lagoon Desk Lamp", "price": 51.0, "stock": 3, "store_id": 2}, {"id": 14, "name": "Meridian Sofa", "price": 540.0, "stock": 1, "store_id": 2}]}
GET /products/search?term=lamp
Response (422 Unprocessable Entity):
{"detail": [{"type": "missing", "loc": ["query", "store_id"], "msg": "Field required", "input": null}]}
GET /products/search?store_id=1&limit=ten
Response (422 Unprocessable Entity):
{"detail": [{"type": "int_parsing", "loc": ["query", "limit"], "msg": "Input should be a valid integer, unable to parse string as an integer", "input": "ten"}]}
GET /products/search?store_id=1&in_stock=maybe
Response (422 Unprocessable Entity):
{"detail": [{"type": "bool_parsing", "loc": ["query", "in_stock"], "msg": "Input should be a valid boolean, unable to interpret input", "input": "maybe"}]}
