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:
cursor = db.execute("UPDATE subscribers SET email = ? WHERE id = ?", ("sam@example.com", 3))
db.commit()
print(cursor.rowcount) # 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 cursor.rowcount 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/<int:product_id>. Update that product's name, price and category with ? placeholders and commit. Answer 404 if no row changed, and otherwise the product as it is stored now.
Assignment
Follow the Coding Tutorial to add the update_product route, which changes a product in the database.
Hint
The values go in the same order as the ?s: name, price, category, then product_id for the WHERE. After the commit, read the row again with the query from get_product.
Free plan: 30 tutor messages. PRO is unlimited.
