Correct Increment Handler

This commit is contained in:
thekiwismarthome
2026-02-16 22:17:16 +13:00
parent bcfde6df9a
commit e76fad5a92
@@ -61,24 +61,25 @@ async def websocket_increment_item(
item_id = msg["item_id"] item_id = msg["item_id"]
amount = msg["amount"] amount = msg["amount"]
# Loop through all lists to find the item # First get current item
for list_id in storage.lists: for list_id, items in storage._items.items():
items = storage.get_items(list_id)
for item in items: for item in items:
if item.id == item_id: if item.id == item_id:
item.quantity += amount new_quantity = item.quantity + amount
# Prevent negative quantities if new_quantity < 1:
if item.quantity < 1: new_quantity = 1
item.quantity = 1
await storage.async_save() updated_item = await storage.update_item(
item_id,
quantity=new_quantity
)
connection.send_result(msg["id"], { if updated_item:
"item": item.to_dict() connection.send_result(msg["id"], {
}) "item": updated_item.to_dict()
return })
return
connection.send_error(msg["id"], "not_found", "Item not found") connection.send_error(msg["id"], "not_found", "Item not found")