StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 11 · EF Core Fundamentals

Paged Product Listing

medium—No data yet

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
}

More in EF Core Fundamentals

See the module
Library Loans QueryThis problem covers reading a list of loans together with the borrower and the book each one points at, and building a short display list that asks the database only for the fields the page prints.—No data yetmediumRead-Only Reporting QueriesThis problem covers reading report rows out of a database without the session holding on to them, while the edit path still notices a change and saves it.—No data yetmediumSearchable Order HistoryThis problem covers answering a support desk's order history with one page of matching orders at a time, narrowed by a search term and a status, sorted the way the caller asked, and counted over everything that matched.—No data yetmedium

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.