Changed remove_request to not throw when item isn't requested

This commit is contained in:
jableader 2025-07-29 09:13:40 +10:00
parent e06ad7611b
commit dca63bd51a

View file

@ -201,28 +201,22 @@ async def request(conn, person: Person, ingredient: Optional[Ingredient] = None,
return item return item
async def remove_request(conn, person: Person, meal: Optional[Meal] = None, ingredient: Optional[Ingredient] = None) -> None: async def remove_request(conn, person: Person, meal: Optional[Meal] = None, ingredient: Optional[Ingredient] = None) -> bool:
failed = True
if meal is not None: if meal is not None:
# Ensure a record is removed
async with conn.execute(''' async with conn.execute('''
DELETE FROM ShoppingListItem DELETE FROM ShoppingListItem
WHERE list_id IS NULL AND meal_id = ? WHERE list_id IS NULL AND meal_id = ?
''', (meal.id,)) as cursor: ''', (meal.id,)) as cursor:
if cursor.rowcount > 0: return cursor.rowcount > 0
failed = False
elif ingredient is not None: elif ingredient is not None:
# Ensure a record is removed
async with conn.execute(''' async with conn.execute('''
DELETE FROM ShoppingListItem DELETE FROM ShoppingListItem
WHERE list_id IS NULL AND ingredient_id = ? AND person_id = ? WHERE list_id IS NULL AND ingredient_id = ? AND person_id = ?
''', (ingredient.id, person.id)) as cursor: ''', (ingredient.id, person.id)) as cursor:
if cursor.rowcount > 0: return cursor.rowcount > 0
failed = False
if failed: raise ValueError('Must specify either a meal or an ingredient to remove')
raise ValueError('Must specify either a meal or an ingredient to remove')
async def find_items_by_list_id(conn, list_id: Optional[int]) -> AsyncIterator[ShoppingListItem]: async def find_items_by_list_id(conn, list_id: Optional[int]) -> AsyncIterator[ShoppingListItem]:
# Join Ingredient and Product to also load ingredient and product # Join Ingredient and Product to also load ingredient and product