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

Read-Only Reporting Queries

medium—No data yet

Problem statement

Complete the implementation of ShipmentReportService so the two operations reports read without leaving anything behind on the database session, while the delay adjustment still notices its own edit and saves it.

Sample Data

The operations database is pre-seeded with five depots, two of them in north, two in south and one in west. Here is an example of a depot record:

{
  "id": 2,
  "name": "Ashgrove Depot",
  "region": "north",
  "dailyCapacity": 250
}

It also holds eight shipments. Here is an example of a shipment record:

{
  "id": 2,
  "reference": "SHP-1002",
  "depotId": 2,
  "status": "in_transit",
  "parcelCount": 4,
  "delayMinutes": 45
}

A shipment's status is one of _in_transit_, delivered or held. The recorded delays run from 0 to 240 minutes, and two shipments share a delay of 90.

Service

The database session

ShipmentReportService is handed the OperationsContext that the rest of the request shares, and it is what the application asks for whenever it needs one of these reports, so all three answers have to come out of this class.

A session can hold on to the rows it reads, and a session holding a row notices later edits to that row and writes them on the next save. The adjustment below needs that. The two reports must not do it.

ShipmentReportService

Implement the three methods of ShipmentReportService:

  • GetDelayedShipments:
    • Accept a minimumDelayMinutes and report every shipment whose delayMinutes is that value or higher, so a shipment sitting exactly on the value is reported and one a minute under it is not.
    • Order the report by delayMinutes from the highest down, and order two shipments on the same delay by reference from A to Z.
    • Report an empty list when nothing is delayed that long, rather than an error.
  • GetDepotsInRegion:
    • Accept a region and report every depot in it, ordered by name from A to Z.
    • Report an empty list for a region holding no depots.
  • RecordDelay:
    • Accept a shipmentId and an extraMinutes, and report nothing when no shipment carries that id, leaving the session holding nothing.
    • Otherwise add extraMinutes to that shipment's delayMinutes, save so a later reader sees the new values, and report the shipment.
    • Set its status to held once the new total reaches 60 or more, and leave the status as it was when the new total stays under 60.
    • Leave the session holding exactly the one shipment it read, with nothing still pending on it once the call returns.
    • Let the session work out what changed, so the write carries delayMinutes, carries status only on a call that actually changed the status, and never carries reference, parcelCount or depotId.

What a report leaves behind

Neither report leaves the session holding the rows it reported, which has five consequences:

  • After a report the session is holding exactly what it was holding before the call, no more and no less.
  • A row the session was already holding is still there afterwards, even when the report reports that same row, and what the report hands back for that row is a separate object rather than the one the session is holding.
  • An edit made before the report to a row the session was holding is still pending after it and is written by the next save.
  • Two runs of the same report hand back separate objects, so the row for a given shipment in the second run is never the same object as the row for it in the first.
  • A reported row is not attached to the session, so changing one and saving the session writes nothing.
Sample Requests & Responses

GET /api/reports/delayed-shipments?minimumDelayMinutes=46

Response code: 200

[
  { "id": 8, "reference": "SHP-1008", "depotId": 3, "status": "delivered", "parcelCount": 5, "delayMinutes": 240 },
  { "id": 3, "reference": "SHP-1003", "depotId": 1, "status": "delivered", "parcelCount": 30, "delayMinutes": 120 },
  { "id": 4, "reference": "SHP-1004", "depotId": 3, "status": "in_transit", "parcelCount": 7, "delayMinutes": 90 },
  { "id": 6, "reference": "SHP-1006", "depotId": 2, "status": "held", "parcelCount": 9, "delayMinutes": 90 }
]

GET /api/reports/depots?region=south answers 200 with Calder Depot then Marlow Depot.

POST /api/shipments/2/delay

Request body:

{ "extraMinutes": 15 }

Response code: 200

{ "id": 2, "reference": "SHP-1002", "depotId": 2, "status": "held", "parcelCount": 4, "delayMinutes": 60 }

POST /api/shipments/99/delay answers 404.

Response code: 404

{ "message": "Shipment 99 was not found." }

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