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

Library Loans Query

medium—No data yet

Problem statement

Complete the implementation of LoanQueryService so the loans page can list each loan next to the member who took it and the book that was taken, and can build the short display list the page prints.

Sample Data

The library database is pre-seeded with four tables.

Five members, one per row of the member directory:

{
  "id": 1,
  "fullName": "Ada Whitfield",
  "membershipTier": "gold",
  "staffNote": "prefers email reminders"
}

Five books, one per row of the catalogue:

{
  "id": 2,
  "title": "Tidal Mechanics",
  "author": "Owen Barrow",
  "shelfCode": "SCI-208",
  "acquisitionStamp": "PO-1874 / 2019-11-27"
}

Two service desks, 1 Main Counter and 2 Reference Room.

Nine loans, ids 1 to 9. A loan's returnedOn is empty while the book is still out, and its desk is empty for the three loans taken from the self-service kiosk. Member 5 holds no loans.

Service

LoanQueryService

LoanQueryService is handed the library database. Both of its methods take a memberId: when it carries a number only that member's loans are wanted, and when it is empty every loan is wanted. Both return their rows ordered by loan id, lowest first.

Each method settles its answer in a single database command, however many loans come back and however many members, books and desks those loans point at. A memberId that no member holds, and a member holding no loans, both give an empty list rather than an error, and both still cost that one command.

  • GetLoanDetails: return the loans themselves. Every loan that comes back carries the member that took it and the book that was taken, both filled in, and each matching the loan's own memberId and bookId. A loan collected at a desk carries that desk; a loan taken from the kiosk carries nothing in its place. Whole rows are wanted here, so a member's membershipTier and staffNote, a book's author, shelfCode and acquisitionStamp, and a desk's deskName all come back alongside the loan, and the one command that answers the call is the command that reads them.
  • GetLoanSummaries: return one LoanSummary per loan, carrying the loan's id as loanId, the member's name as memberName, the book's title as bookTitle, the desk's name as deskName or nothing for a kiosk loan, the loan's dueOn, and returned, which is true once the book has come back and false while it is still out. Nothing else about a member or a book belongs on that page, so nothing else about them is asked for: building a summary list asks the database for a member's fullName, a book's title and a desk's deskName, and never for membershipTier, staffNote, author, shelfCode or acquisitionStamp.

The application answers both of its loan requests through LoanQueryService, so the work belongs in that class rather than anywhere else it could be made to happen.

Sample Requests & Responses

GET /loans

All nine loans come back, in id order. Here is the first of them in full:

Response code: 200

{
  "id": 1,
  "memberId": 1,
  "bookId": 1,
  "deskId": 1,
  "borrowedOn": "2026-01-05",
  "dueOn": "2026-01-26",
  "returnedOn": "2026-01-20",
  "member": {
    "id": 1,
    "fullName": "Ada Whitfield",
    "membershipTier": "gold",
    "staffNote": "prefers email reminders"
  },
  "book": {
    "id": 1,
    "title": "The Salt Road",
    "author": "Marta Rennick",
    "shelfCode": "FIC-114",
    "acquisitionStamp": "PO-2210 / 2021-03-04"
  },
  "desk": {
    "id": 1,
    "deskName": "Main Counter"
  }
}

The other eight follow the same shape.

GET /loans/summaries

One summary comes back per loan, in id order.

Response code: 200

[
  { "loanId": 1, "memberName": "Ada Whitfield", "bookTitle": "The Salt Road", "deskName": "Main Counter", "dueOn": "2026-01-26", "returned": true },
  { "loanId": 2, "memberName": "Ada Whitfield", "bookTitle": "Kitchen Latin", "deskName": null, "dueOn": "2026-02-23", "returned": false }
]

More in EF Core Fundamentals

See the module
Read-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 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.