StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 7 · Async Fundamentals

Non-Blocking Report Endpoint

easy—No data yet

Problem statement

Complete the implementation of IReportBuilder, ReportBuilder and ReportsController so a report is assembled out of the pieces a slow source hands back, and answered without any layer of the chain making the thread that called it stand and wait.

Sample Data

The report source is pre-seeded with four reports. Each one lists its sections in a fixed order, and every section holds a line of text of its own:

  • nightly-sales lists summary, then regions, then refunds.
  • warehouse-audit lists intake, then shortfall, then signoff.
  • courier-latency lists overview and nothing else.
  • staffing-roster is a real report that lists no sections at all.

A report name is matched exactly, so nightly-sale names no report, and neither does a name like payroll-summary that was never seeded.

Service

IReportSource

The chain's one source of data, and both of its members answer through a Task that finishes later rather than on the spot:

  • ListSectionsAsync takes a report name and answers that report's section names in their listed order. It answers null when no report carries that name, and an empty list for a report that lists no sections.
  • ReadSectionAsync takes a report name and one of that report's section names, and answers that section's text. Asking it for a section the report does not list is an error.

IReportBuilder and ReportBuilder

Both ship declaring one member that hands a finished ReportResponse straight back to its caller. Change that member on the interface and on the class so it hands back a Task carrying the ReportResponse instead, and then fill it in:

  • List the sections of the requested report.
  • Answer nothing at all when the source reports no such report, and read no section in that case.
  • Read every listed section, once each, in the order the source listed them.
  • Answer a ReportResponse carrying name, the report name that was asked for; sectionCount, how many sections the report listed; and body, the section texts in that same listed order with one blank line between neighbouring sections.
  • A report that lists no sections answers a sectionCount of 0 and an empty body.

Await each source call rather than waiting on it, and begin the work on the thread that asked for it rather than handing the whole job to another one. Reading a task's Result, calling Wait, calling GetAwaiter().GetResult(), and starting the chain on another thread with Task.Run all fail, however right the answer they hand back looks. The interface keeps declaring exactly the one member, and every member you declare on it and on the class answers through a Task.

API Endpoints to Implement

ReportsController

GET /reports/{reportName}

The controller ships declaring one action that answers on the spot, and it keeps declaring exactly that one action. Change it the same way, so the action answers through a Task as well and awaits the builder instead of waiting on it.

  • 200 with the built report, when the builder answers one.
  • 404 with {"message": "Report '<reportName>' was not found."}, carrying the requested name, when the builder answers nothing.
Sample Requests & Responses

GET /reports/nightly-sales

The report lists three sections, so all three are read in their listed order and their texts are joined with a blank line between them.

Response code: 200

{
  "name": "nightly-sales",
  "sectionCount": 3,
  "body": "Orders 412, revenue 51840.00\n\nNorth 18400.00, South 21300.00, West 12140.00\n\n7 refunds totalling 1260.00"
}

The other seeded reports answer 200 the same way:

  • GET /reports/warehouse-audit answers a sectionCount of 3 and a body of 94 pallets received, 3 lines short, 12 units missing and Cleared by D. Marsh at 05:40, joined the same way.
  • GET /reports/courier-latency answers a sectionCount of 1 and a body of Average pickup delay 38 minutes, with no separator to add.
  • GET /reports/staffing-roster answers a sectionCount of 0 and an empty body.

GET /reports/payroll-summary

Response code: 404

{
  "message": "Report 'payroll-summary' was not found."
}

GET /reports/nightly-sale answers 404 the same way, with its own name in the message. GET / answers 200.

More in Async Fundamentals

See the module
Parallel Price FetchThis problem covers asking three suppliers to price the same basket at the same time instead of one after another, waiting for all of their answers together, and reporting them with the cheapest supplier named.—No data yeteasyCancelable Long ExportThis problem covers running a long export that stops as soon as the caller stops waiting for it, instead of carrying on to the end once nobody wants the answer.—No data yeteasyAggregated Dashboard LoaderThis problem covers loading the three panels of a dashboard at the same time, still answering with the panels that came back when one of them fails, and stopping every load that is still running once the caller has given up.—No data yeteasy

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.