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,27 +201,21 @@ async def request(conn, person: Person, ingredient: Optional[Ingredient] = None,
return item
async def remove_request(conn, person: Person, meal: Optional[Meal] = None, ingredient: Optional[Ingredient] = None) -> None:
failed = True
async def remove_request(conn, person: Person, meal: Optional[Meal] = None, ingredient: Optional[Ingredient] = None) -> bool:
if meal is not None:
# Ensure a record is removed
async with conn.execute('''
DELETE FROM ShoppingListItem
WHERE list_id IS NULL AND meal_id = ?
''', (meal.id,)) as cursor:
if cursor.rowcount > 0:
failed = False
return cursor.rowcount > 0
elif ingredient is not None:
# Ensure a record is removed
async with conn.execute('''
DELETE FROM ShoppingListItem
WHERE list_id IS NULL AND ingredient_id = ? AND person_id = ?
''', (ingredient.id, person.id)) as cursor:
if cursor.rowcount > 0:
failed = False
return cursor.rowcount > 0
if failed:
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]: