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 method turns it into an UPDATE statement.
Here is an UPDATE on another table, a list of newsletter subscribers:
int count = db.update("UPDATE subscribers SET email = ? WHERE id = ?", "sam@example.com", 3);
System.out.println(count); // 1 if subscriber 3 exists, 0 if not
The WHERE limits the change to one row: without it, every row of the table changes. db.update answers how many rows the statement changed. An UPDATE that matches no row is not an error, it just changes nothing, so the count is 0: that's when you answer 404. Columns the UPDATE doesn't name, like stock, keep their values. JdbcTemplate saves each statement as soon as it runs, so there is no commit to write.
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 PUT /products/{productId} with the method updateProduct. 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 updateProduct method, 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. Then read the row again with the query from getProduct.
Free plan: 30 tutor messages. PRO is unlimited.
