feat(shopping): setCartServings mit Positiv-Validation
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m15s

This commit is contained in:
hsiegeln
2026-04-21 22:59:12 +02:00
parent 83fe95ac76
commit 85bf197084
2 changed files with 29 additions and 5 deletions

View File

@@ -55,11 +55,16 @@ export function removeRecipeFromCart(
}
export function setCartServings(
_db: Database.Database,
_recipeId: number,
_servings: number
db: Database.Database,
recipeId: number,
servings: number
): void {
throw new Error('not implemented');
if (!Number.isInteger(servings) || servings <= 0) {
throw new Error(`Invalid servings: ${servings}`);
}
db.prepare(
'UPDATE shopping_cart_recipe SET servings = ? WHERE recipe_id = ?'
).run(servings, recipeId);
}
export function listShoppingList(db: Database.Database): ShoppingListSnapshot {

View File

@@ -4,7 +4,8 @@ import { insertRecipe } from '../../src/lib/server/recipes/repository';
import {
addRecipeToCart,
removeRecipeFromCart,
listShoppingList
listShoppingList,
setCartServings
} from '../../src/lib/server/shopping/repository';
import type { Recipe } from '../../src/lib/types';
@@ -84,3 +85,21 @@ describe('removeRecipeFromCart', () => {
expect(() => removeRecipeFromCart(db, id)).not.toThrow();
});
});
describe('setCartServings', () => {
it('updates servings for a cart recipe', () => {
const db = openInMemoryForTest();
const id = insertRecipe(db, recipe());
addRecipeToCart(db, id, null, 4);
setCartServings(db, id, 8);
expect(listShoppingList(db).recipes[0].servings).toBe(8);
});
it('rejects non-positive servings', () => {
const db = openInMemoryForTest();
const id = insertRecipe(db, recipe());
addRecipeToCart(db, id, null, 4);
expect(() => setCartServings(db, id, 0)).toThrow();
expect(() => setCartServings(db, id, -3)).toThrow();
});
});