feat(shopping): setCartServings mit Positiv-Validation
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m15s
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m15s
This commit is contained in:
@@ -55,11 +55,16 @@ export function removeRecipeFromCart(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function setCartServings(
|
export function setCartServings(
|
||||||
_db: Database.Database,
|
db: Database.Database,
|
||||||
_recipeId: number,
|
recipeId: number,
|
||||||
_servings: number
|
servings: number
|
||||||
): void {
|
): 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 {
|
export function listShoppingList(db: Database.Database): ShoppingListSnapshot {
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import { insertRecipe } from '../../src/lib/server/recipes/repository';
|
|||||||
import {
|
import {
|
||||||
addRecipeToCart,
|
addRecipeToCart,
|
||||||
removeRecipeFromCart,
|
removeRecipeFromCart,
|
||||||
listShoppingList
|
listShoppingList,
|
||||||
|
setCartServings
|
||||||
} from '../../src/lib/server/shopping/repository';
|
} from '../../src/lib/server/shopping/repository';
|
||||||
import type { Recipe } from '../../src/lib/types';
|
import type { Recipe } from '../../src/lib/types';
|
||||||
|
|
||||||
@@ -84,3 +85,21 @@ describe('removeRecipeFromCart', () => {
|
|||||||
expect(() => removeRecipeFromCart(db, id)).not.toThrow();
|
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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user