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