Prices change: the Notebook costs 9 now, not 7. To change a product, a client sends a PUT to the product's own path, like /products/2, with its new name, price and category in the JSON body. Your route turns it into an UPDATE statement.
Here is an UPDATE on another table, a list of newsletter subscribers:
var cmd = db.CreateCommand();
cmd.CommandText = "UPDATE subscribers SET email = $email WHERE id = $id";
cmd.Parameters.AddWithValue("$email", "sam@example.com");
cmd.Parameters.AddWithValue("$id", 3);
Console.WriteLine(cmd.ExecuteNonQuery()); // 1 if subscriber 3 exists, 0 if not
The WHERE limits the change to one row: without it, every row of the table changes. ExecuteNonQuery() runs the statement, saves it at once, and gives back how many rows it changed. An UPDATE that matches no row is not an error, it just changes nothing, so that count is 0: that's when you answer 404. Columns the UPDATE doesn't name, like stock, keep their values.
Starting from the shop's 8 products, your code must answer:
PUT /products/2 {"name": "Notebook", "price": 9, "category": "office"} → 200 {"id": 2, "name": "Notebook", "price": 9, "category": "office", "stock": 0}
PUT /products/8 {"name": "Gift card", "price": 40, "category": "gifts"} → 200 {"id": 8, "name": "Gift card", "price": 40, "category": "gifts", "stock": 999}
PUT /products/99 {"name": "Cup", "price": 5, "category": "kitchen"} → 404 {"error": "Product not found"}
GET /products?category=gifts → 200 [{"id": 8, "name": "Gift card", "price": 40, "category": "gifts", "stock": 999}]
Your task: add the route PUT /products/{id} with app.MapPut. Update that product's name, price and category with $ parameters. Answer 404 if no row changed, and otherwise the product as it is stored now.
Assignment
Follow the Coding Tutorial to add the MapPut route, which changes a product in the database.
Hint
Each $ parameter gets its value from an AddWithValue line: $name, $price and $category from data, $id from the path. After the UPDATE, read the row again with the query from the /products/{id} GET route.
Free plan: 30 tutor messages. PRO is unlimited.
