StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 3 · Minimal APIs and Routing

Search Endpoint Query Binding

easy100%1 of 1 solved

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).

More in Minimal APIs and Routing

See the module
Book Catalog EndpointsThis problem covers mapping the list, detail and create endpoints of a resource as minimal APIs, each answering with the right status code, and pointing the caller at what was just created.100%1 of 1 solvedeasyRoute Constraints For Order LookupThis problem covers adding route constraints, so an order number only matches a whole number and a tracking code only matches a GUID, and a badly formed URL is turned away by routing before it reaches the handler.100%1 of 1 solvedeasyTask Tracker API With Route GroupsThis problem covers mapping the five endpoints of a task tracker in one route group with typed results, so listing can be filtered, creating points at what it made, and updating or deleting something that is not there says so.100%1 of 1 solvedeasy

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.