diff --git a/src/lib/server/shopping/repository.ts b/src/lib/server/shopping/repository.ts index 5463f6c..2a40528 100644 --- a/src/lib/server/shopping/repository.ts +++ b/src/lib/server/shopping/repository.ts @@ -107,12 +107,22 @@ export function listShoppingList( } export function toggleCheck( - _db: Database.Database, - _nameKey: string, - _unitKey: string, - _checked: boolean + db: Database.Database, + nameKey: string, + unitKey: string, + checked: boolean ): void { - throw new Error('not implemented'); + if (checked) { + db.prepare( + `INSERT INTO shopping_cart_check (name_key, unit_key) + VALUES (?, ?) + ON CONFLICT(name_key, unit_key) DO NOTHING` + ).run(nameKey, unitKey); + } else { + db.prepare( + 'DELETE FROM shopping_cart_check WHERE name_key = ? AND unit_key = ?' + ).run(nameKey, unitKey); + } } export function clearCheckedItems(_db: Database.Database): void { diff --git a/tests/integration/shopping-repository.test.ts b/tests/integration/shopping-repository.test.ts index 437b087..8453e5f 100644 --- a/tests/integration/shopping-repository.test.ts +++ b/tests/integration/shopping-repository.test.ts @@ -5,7 +5,8 @@ import { addRecipeToCart, removeRecipeFromCart, listShoppingList, - setCartServings + setCartServings, + toggleCheck } from '../../src/lib/server/shopping/repository'; import type { Recipe } from '../../src/lib/types'; @@ -185,3 +186,48 @@ describe('listShoppingList aggregation', () => { expect(rows[0].total_quantity).toBeNull(); }); }); + +describe('toggleCheck', () => { + function setupOneRowCart() { + const db = openInMemoryForTest(); + const id = insertRecipe(db, recipe({ + ingredients: [{ position: 1, quantity: 200, unit: 'g', name: 'Mehl', note: null, raw_text: '', section_heading: null }] + })); + addRecipeToCart(db, id, null); + return { db, id }; + } + + it('marks a row as checked', () => { + const { db } = setupOneRowCart(); + toggleCheck(db, 'mehl', 'g', true); + const rows = listShoppingList(db).rows; + expect(rows[0].checked).toBe(1); + }); + + it('unchecks a row when passed false', () => { + const { db } = setupOneRowCart(); + toggleCheck(db, 'mehl', 'g', true); + toggleCheck(db, 'mehl', 'g', false); + expect(listShoppingList(db).rows[0].checked).toBe(0); + }); + + it('check survives removal of one recipe when another still contributes', () => { + const db = openInMemoryForTest(); + const a = insertRecipe(db, recipe({ + title: 'A', + ingredients: [{ position: 1, quantity: 100, unit: 'g', name: 'Mehl', note: null, raw_text: '', section_heading: null }] + })); + const b = insertRecipe(db, recipe({ + title: 'B', + ingredients: [{ position: 1, quantity: 200, unit: 'g', name: 'Mehl', note: null, raw_text: '', section_heading: null }] + })); + addRecipeToCart(db, a, null); + addRecipeToCart(db, b, null); + toggleCheck(db, 'mehl', 'g', true); + // Rezept A weg, Mehl kommt noch aus B — check bleibt, mit neuer Menge + removeRecipeFromCart(db, a); + const rows = listShoppingList(db).rows; + expect(rows[0].checked).toBe(1); + expect(rows[0].total_quantity).toBe(200); + }); +});