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:
const result = db.prepare("UPDATE subscribers SET email = ? WHERE id = ?").run("sam@example.com", 3);
console.log(result.changes); // 1 if subscriber 3 exists, 0 if not
The WHERE limits the change to one row: without it, every row of the table changes. An UPDATE that matches no row is not an error, it just changes nothing, so result.changes is 0: that's when you answer 404. Columns the UPDATE doesn't name, like stock, keep their values. As with an INSERT, the change is saved the moment .run finishes.
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/:productId. Update that product's name, price and category with ? placeholders. Answer 404 if no row changed, and otherwise the product as it is stored now.
Assignment
Follow the Coding Tutorial to add the PUT /products/:productId route, which changes a product in the database.
Hint
The values go in the same order as the ?s: name, price, category, then productId for the WHERE. After the UPDATE, read the row again with the query from the GET /products/:productId route.
Free plan: 30 tutor messages. PRO is unlimited.
