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

Searchable Order History

medium—No data yet

Problem statement

Complete the implementation of OrderHistoryService so the support desk can search the order history, narrow it to one status, sort it either way and read it one page at a time, alongside the number of orders the search matched.

Sample Data

The support database is pre-seeded with six customers and twenty four orders. Here is one customer record:

{
  "id": 1,
  "fullName": "Nadia Ramsay",
  "supportTier": "priority",
  "contactEmail": "nadia.ramsay@example.com"
}

and one order record:

{
  "id": 17,
  "reference": "ORD-3105",
  "customerId": 1,
  "status": "delivered",
  "placedOn": "2026-03-02",
  "itemCount": 6,
  "total": 148.72,
  "internalNote": "receipt emailed twice"
}

Every order names the customer that placed it, and every reference is unique. A status is one of placed, shipped, delivered or cancelled. An order's id says nothing about where it sits in a page.

Service

OrderHistoryService

OrderHistoryService is handed an OrderHistoryContext, whose Orders set is the whole order table and whose Customers set is the whole customer directory. Implement Search so that it answers with an OrderHistoryPage, whose constructor takes the page's summaries and a total.

Which orders match

  • Match an order when the search term appears somewhere in its reference or somewhere in its customer's fullName. Match every order when search is missing, empty, or nothing but spaces.
  • Match an order when its status is exactly the status asked for. Match every order when status is missing, empty, or nothing but spaces.
  • When both are asked for, match only the orders that meet both.
  • Answer 200 with an empty Items and a TotalCount of 0, rather than an error, when nothing matches.

What order they come back in

  • Read sort as placedOn or as itemCount. Any other value, and no value at all, sorts on placedOn.
  • Read direction as desc to sort from the highest value down. Any other value, and no value at all, sorts from the lowest value up.
  • Orders sharing the value being sorted on come back in reference order from A to Z, whichever way the sort runs.

That second key is what holds a page still. Three orders share every placing day and several share an itemCount, so sorting on the first key alone leaves their order to the database, and two calls for the same page can then answer with different orders while consecutive pages repeat one order and skip another.

Which slice comes back

  • 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 matches at positions (page - 1) * size + 1 through page * size, in that order, counting with the page and size as they were just read rather than as they arrived.
  • Answer 200 with an empty Items rather than an error when page sits past the last match.
  • Report the number of orders the search matched as TotalCount. It reads the same on every page, including a page that carries nothing, and it counts the matches rather than the whole table or the page.

What a summary carries

Each OrderSummary carries the order's reference, its customer's fullName as customerName, and the order's status, placedOn, itemCount and total. Nothing else about an order or a customer belongs on a support page, so nothing else is asked for: building a page asks the database for those six values and never for internalNote, supportTier or contactEmail.

What the database does

  • Leave the matching, the sorting, the slicing and the counting to the database. One call runs exactly two database commands: one that counts the matches, and one that returns that page's own rows and no others, already narrowed, already sorted on both keys, and already cut to the page.
  • Reading the table and then filtering, sorting, slicing or counting the result in memory answers with the same orders, and is what this method must not do.
  • A filter that was not asked for adds no clause. A call with neither search nor status sends two commands that narrow nothing, rather than a clause that matches everything.

What the session is left holding

  • The search leaves nothing of its own on the session.
  • It leaves what the session was already holding exactly as it was: a row read before the search is still held after it, and a change made to that row before the search is still written by the next save.

Do all of this inside Search. A caller holding nothing but the service asks it for a page and gets one.

Sample Requests & Responses

GET /orders?page=1&size=3

Response code: 200

{
  "items": [
    { "reference": "ORD-3105", "customerName": "Nadia Ramsay", "status": "delivered", "placedOn": "2026-03-02", "itemCount": 6, "total": 148.72 },
    { "reference": "ORD-3142", "customerName": "Anika Sorensen", "status": "shipped", "placedOn": "2026-03-02", "itemCount": 2, "total": 62.31 },
    { "reference": "ORD-3178", "customerName": "Lena Okafor", "status": "placed", "placedOn": "2026-03-02", "itemCount": 9, "total": 219.45 }
  ],
  "totalCount": 24
}

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 yetmediumPaged Product ListingThis problem covers handing back one page of a product listing at a time, in an order that does not shift between requests, together with the number of products the whole listing holds.—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.