Problem statement
Map a search endpoint whose filters come from the query string as typed handler parameters, each with a default for when the caller leaves it out.
Requirements
The product catalog and IProductSearchService are already provided and registered. Your task has one part: map GET /search in MapSearchEndpoint in ProductSearchApi/Endpoints/SearchEndpoints.cs.
In a minimal API, a simple handler parameter that is not part of the route is filled from the query string with the same name, and converted to the parameter's type. A parameter with a default value becomes optional, and a value that cannot be converted to the type is answered with 400.
IProductSearchService (provided)
- Search(term, minPrice, maxPrice, page) returns the matching products, three per page, and echoes the four values it received
SearchEndpoints
- GET /search answers with the result of Search
- term, minPrice, maxPrice and page are parameters of the handler, typed as string, decimal, decimal and int
- When a value is left out, it takes its default: term is empty, minPrice is 0, maxPrice is 1000 and page is 1
- A value of the wrong type, such as minPrice=abc or page=two, is answered with 400
Sample Requests & Responses
GET /search
Response (200 OK), with every default:
{
"term": "",
"minPrice": 0,
"maxPrice": 1000,
"page": 1,
"pageSize": 3,
"totalMatches": 9,
"items": [
{ "id": 1, "name": "Aurora Desk Lamp", "price": 45.00 },
{ "id": 2, "name": "Basalt Coffee Table", "price": 320.00 },
{ "id": 3, "name": "Cedar Bookshelf", "price": 189.50 }
]
}
GET /search?term=desk&maxPrice=100
Response (200 OK):
{
"term": "desk",
"minPrice": 0,
"maxPrice": 100,
"page": 1,
"pageSize": 3,
"totalMatches": 3,
"items": [
{ "id": 1, "name": "Aurora Desk Lamp", "price": 45.00 },
{ "id": 7, "name": "Glacier Desk Mat", "price": 39.00 },
{ "id": 9, "name": "Ivory Desk Organizer", "price": 18.25 }
]
}
GET /search?page=two
Response (400 Bad Request).
