Problem statement
Complete the implementation of ProductQueryService to answer with one page of the product listing at a time, in an order that stays put between requests, together with the number of products the whole listing holds.
Sample Data
The catalog table is pre-seeded with thirty products, twenty six of them listed and four of them not. Here is one product record:
{
"id": 4,
"sku": "SN1002",
"name": "Studio Headphones",
"category": "Audio",
"price": 129.45,
"listed": true
}
The listed products spread over five categories: Audio holds six of them, and Cables, Lighting, Storage and Wearables hold five each. Every sku is unique. SN1019, CB2022, LT3024 and ST4028 are the four products whose listed is false. A product's id says nothing about where it sits in the listing.
Service
The Listing
ProductQueryService is handed a CatalogDbContext through its constructor, and its Products set is the whole catalog table.
The listing is every product whose listed is true, ordered by category from A to Z, and then by sku from A to Z within a category. That second key is what holds the listing still: five or six products share each category, so ordering on category alone leaves their order to the database, and two calls for the same page can then answer with different products while consecutive pages repeat one product and skip another.
ProductQueryService
Implement GetPage so that it answers with a ProductPage, whose constructor takes the page's products and a total:
- Read page as a page number counted from 1, and read a page below 1 as 1.
- Read a size below 1 as 1, and a size above 20 as 20.
- Fill Items with the products the listing holds at positions (page - 1) * size + 1 through page * size, in the listing's order.
- Answer 200 with an empty Items rather than an error when page sits past the end of the listing.
- Report the number of products the whole listing holds as TotalItems. It reads the same on every page, including a page that carries nothing, and it counts no product whose listed is false.
- Leave the ordering, the slicing and the counting to the database. One call runs exactly two database commands: one that counts the listing, and one that returns that page's own rows and no others, already ordered on both keys. Reading the table and then ordering, slicing or counting the result in memory answers with the same products, and is what this method must not do.
- Do all of this inside GetPage. A caller holding nothing but the service asks it for a page and gets one.
Sample Requests & Responses
GET /products?page=1&size=5
Response code: 200
{
"items": [
{ "id": 4, "sku": "SN1002", "name": "Studio Headphones", "category": "Audio", "price": 129.45, "listed": true },
{ "id": 19, "sku": "SN1008", "name": "Desk Microphone", "category": "Audio", "price": 58.25, "listed": true },
{ "id": 14, "sku": "SN1015", "name": "Bookshelf Speaker", "category": "Audio", "price": 96.85, "listed": true },
{ "id": 24, "sku": "SN1023", "name": "Portable Amplifier", "category": "Audio", "price": 164.25, "listed": true },
{ "id": 1, "sku": "SN1031", "name": "Wireless Earbuds", "category": "Audio", "price": 79.55, "listed": true }
],
"totalItems": 26
}
