mirror of
https://github.com/thekiwismarthome/shopping-list-manager.git
synced 2026-05-01 11:46:30 +00:00
Correct Increment Handler
This commit is contained in:
@@ -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")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user