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

Parallel Price Fetch

easy—No data yet

Problem statement

Complete the implementation of BasketPricingService to price a basket with all three suppliers at once and report which of them came back cheapest.

Sample Data

The supplier directory lists three suppliers, in this order: northwind, eastgate, summit. Each one prices a basket by its own rules.

The basket store is pre-seeded with five baskets. Here is one of them, home-office:

[
  { "sku": "DESK-01", "quantity": 1, "unitPrice": 150.00 },
  { "sku": "CHAIR-07", "quantity": 2, "unitPrice": 40.00 },
  { "sku": "LAMP-22", "quantity": 1, "unitPrice": 10.00 }
]

The others are pantry and workshop, which hold lines of their own; welcome-pack, which holds a single line priced at 0.00; and empty-cart, which holds no lines at all.

Service

BasketPricingService

BasketPricingService is handed an IBasketStore, an ISupplierDirectory and an ISupplierPriceClient. Implement QuoteBasketAsync:

1. Read the basket's lines from the store. When the store holds no basket under that identifier, report null and ask no supplier for anything. The endpoint turns that null into a 404. A basket that holds no lines is still a basket, and is priced like any other.

2. Ask every supplier the directory lists to price those lines, through ISupplierPriceClient.GetQuoteAsync. One call to QuoteBasketAsync asks each supplier exactly once. A supplier answers with its own identifier and its own total for the basket, and that total is reported as it stands.

3. Have all three of those calls in flight at the same time, rather than starting one only after the one before it has answered. The price client makes this observable: it holds on to every call until all three have reached it, and it remembers the largest number of calls it ever had in flight at once. That number has to be three.

A call that reaches the client on its own is still answered, after a pause, with the same total it would otherwise have given. Supplier calls made one at a time therefore produce a correct looking response, and only that count gives them away.

4. Wait for all three answers together, then report them as quotes, in the order the directory lists the suppliers, alongside the basketId that was asked for.

5. Report cheapestSupplierId: the supplier whose total is the lowest. When two or more suppliers come back level on that lowest total, the one whose identifier sorts first alphabetically is the cheapest.

Sample Requests & Responses

GET /baskets/home-office/quotes

Response code: 200

{
  "basketId": "home-office",
  "quotes": [
    { "supplierId": "northwind", "total": 240.00 },
    { "supplierId": "eastgate", "total": 220.80 },
    { "supplierId": "summit", "total": 235.20 }
  ],
  "cheapestSupplierId": "eastgate"
}

The other baskets answer in the same shape, always with all three suppliers in that same order:

  • /baskets/pantry/quotes comes back at 45.50, 66.86 and 52.59, so northwind is the cheapest.
  • /baskets/workshop/quotes comes back at 120.00, 135.40 and 117.60, so summit is the cheapest.
  • /baskets/welcome-pack/quotes and /baskets/empty-cart/quotes both come back at 0 from all three suppliers, so all three are level on the lowest total and eastgate, first alphabetically, is named the cheapest. It is neither the first nor the last of the three, so no position in the list decides a tie.

GET /baskets/no-such-basket/quotes

Response code: 404

{
  "message": "Basket no-such-basket was not found."
}

GET / answers 200.

More in Async Fundamentals

See the module
Non-Blocking Report EndpointThis problem covers building a report out of the pieces a slow source hands back, and returning it without ever making the calling thread sit and wait.—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.