This project puts most of what you have built so far into one new route. A product page shows the product's rating: the average of the stars in its reviews. The rating belongs to one product, so it gets a nested path under that product, just like the reviews: /products/1/rating.
An average in C#: collect the numbers in a List<int>, then call Average(), which adds them up and divides by the count. Math.Round(..., 2) keeps 2 decimal places, the way the /stats route does for the prices.
var temperatures = new List<int> { 20, 23, 22 };
var average = Math.Round(temperatures.Average(), 2);
Console.WriteLine(average); // 21.67
No numbers, no average: on an empty list, Average() throws an InvalidOperationException, and ASP.NET Core would answer 500. So check for an empty list first and answer (double?)null, which is null in JSON. A product with no reviews still exists, so it gets 200 with a null rating. A product that isn't in products gets 404, as always.
The test requests, in this order, and what your code must answer. A whole average like 2.0 is written as 2 in the JSON:
GET /products/1/rating → 200 {"product_id": 1, "rating": 4.5}
GET /products/3/rating → 200 {"product_id": 3, "rating": 2}
GET /products/2/rating → 200 {"product_id": 2, "rating": null}
GET /products/99/rating → 404 {"error": "Product not found"}
POST /products/1/reviews {"stars": 5, "text": "My favourite mug"} → 201 (the review)
GET /products/1/rating → 200 {"product_id": 1, "rating": 4.67}
POST /products/2/reviews {"stars": 3, "text": "Thin pages"} → 201 (the review)
GET /products/2/rating → 200 {"product_id": 2, "rating": 3}
DELETE /products/3 → 204 (empty)
GET /products/3/rating → 404 {"error": "Product not found"}
Your task: at the end of the file, add a MapGet route for /products/{id:int}/rating whose lambda takes int id. It answers the "product_id" and a "rating": the average of the product's stars rounded to 2 places, or null when the product has no reviews. When no product has that id, it answers 404 with "Product not found".
Assignment
Follow the Coding Tutorial to add the /rating route at the end of the file: find the product, collect the stars of its reviews, and answer their rounded average, or a null rating when there are none.
Hint
Copy the shape of the GET reviews route to find the product and to answer the 404 with ErrorResponse, and the shape of the /stats route for the list, the empty check and the rounded average. Check for an empty list before you call Average().
Free plan: 30 tutor messages. PRO is unlimited.
