StaffCoder
.NET
.NET.NET 10
12 modules · 48 challengesSign up
Module 6 · LINQ and Collections

Catalog Filter And Projection

easy—No data yet

Problem statement

A product listing endpoint returns full entities including internal fields, unfiltered and unsorted. Complete ProductSummary and CatalogQueryService with LINQ, so that the listing keeps only the catalog entries that are in stock and priced inside the requested range, hands them back ordered by price from the cheapest to the most expensive, and carries only the public fields of each entry.

Sample Data

The catalog is pre-seeded with eleven entries, two of which are out of stock: 2, a Monitor Stand priced 33.40, and 10, a Noise Cancelling Headset priced 88.25. No two entries share a price, and the store hands the entries back in an order that is neither the order of the identifiers nor the order of the prices. Here is an example of a catalog entry:

{
  "id": 4,
  "name": "Webcam",
  "category": "peripherals",
  "price": 12.00,
  "inStock": true,
  "supplierCost": 5.05,
  "warehouseCode": "WH-A08",
  "internalNote": "firmware 2.1"
}

supplierCost, warehouseCode, internalNote and inStock are internal. They are what the store keeps for the warehouse team, and none of them belongs in an answer sent to a caller.

Service

ProductSummary

ProductSummary is the shape the listing answers with. Give it the public fields of a catalog entry, and only those four:

  • Id: the entry's Id, an int.
  • Name: the entry's Name, a string.
  • Price: the entry's Price, a decimal.
  • Category: the entry's Category, a string.

Serialized, an item therefore reads id, name, price and category, and nothing else. The four internal fields are not carried as null or as a zero, they are not declared on the type at all, so they never reach the JSON: the only fields ProductSummary declares are those four.

CatalogQueryService

Implement GetSummaries in CatalogQueryService, which is handed the ICatalogStore that holds the catalog and is the only source of catalog entries.

  • GetSummaries:
    • Accept the two ends of a price range, and read the catalog entries from ICatalogStore.GetAll every time it is called, never from a copy taken earlier, so an entry the store received since the last call is listed by the next one.
    • Keep an entry only when its InStock is true. An entry that is out of stock is left out whatever it costs.
    • Keep an entry only when its Price is greater than or equal to minPrice and less than or equal to maxPrice. Both ends are inclusive, so an entry priced exactly at minPrice and an entry priced exactly at maxPrice are both kept, while one cent below minPrice or one cent above maxPrice is left out.
    • Order what is left by Price, from the lowest price to the highest.
    • Turn each remaining entry into a ProductSummary carrying that entry's Id, Name, Price and Category.
    • Return an empty list when no entry matches.
Sample Requests & Responses

GET /api/catalog/products?minPrice=12.00&maxPrice=60.00

The five entries that are in stock and priced from 12.00 to 60.00, cheapest first. 5 is left out because 11.99 is below the range, 6 and 11 because 60.01 and 129.99 are above it, 9 because 4.50 is below it, and 2 because it is out of stock even though 33.40 sits inside the range.

Response code: 200

[
  { "id": 4, "name": "Webcam", "price": 12.00, "category": "peripherals" },
  { "id": 8, "name": "Laptop Sleeve", "price": 18.75, "category": "accessories" },
  { "id": 3, "name": "Wireless Mouse", "price": 25.00, "category": "peripherals" },
  { "id": 7, "name": "Desk Lamp", "price": 47.90, "category": "lighting" },
  { "id": 1, "name": "Mechanical Keyboard", "price": 60.00, "category": "peripherals" }
]

Every listing answers 200 with that same shape, ordered by price. The entries in stock, cheapest first, are 9 Cable Clip Pack at 4.50, 5 USB Cable at 11.99, 4 Webcam at 12.00, 8 Laptop Sleeve at 18.75, 3 Wireless Mouse at 25.00, 7 Desk Lamp at 47.90, 1 Mechanical Keyboard at 60.00, 6 Docking Station at 60.01 and 11 Server Rack at 129.99. So:

  • ?minPrice=10.00&maxPrice=50.00 lists 5, 4, 8, 3 and 7.
  • ?minPrice=20.00&maxPrice=100.00 lists 3, 7, 1 and 6.
  • No range at all falls back to 0 and no upper limit, listing all nine, only 2 and 10 missing because both are out of stock.
  • ?minPrice=20.00 alone has no upper limit, so it lists 3, 7, 1, 6 and 11.
  • ?maxPrice=12.00 alone starts the range at 0, so it lists 9, 5 and 4.
  • ?minPrice=200.00&maxPrice=300.00 matches nothing, so the listing is empty and the status is still a success.

The store is read while the request is being answered, so a Portable Drive priced 30.00 and in stock, added while the application is running, is listed in its price position: the first range above then reads 4, 8, 3, the new 12 at 30.00, 7 and 1. Had it been added out of stock the listing would read exactly as it did before, and had it been added at 75.00 it would be missing from that range too, showing up only once maxPrice reached it.

More in LINQ and Collections

See the module
Sales Grouping ReportThis problem covers gathering recorded sales into one line per category, reporting how many each holds, what they add up to and what they average, and listing the richest category first with ties settled by name.—No data yeteasyDeferred Execution BugThis problem covers taking a handover report and settling it then and there, so the copy kept says what the log said at that moment however the log changes afterwards, while a fresh run always reports the log as it stands.—No data yeteasyTop Customers AnalyticsThis problem covers ranking customers by what they have spent, reporting how many orders each placed and what they average, and pinning a ranking so the pinned board keeps saying what it said when it was taken.100%1 of 1 solvedeasy

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.