feat(shopping): addRecipeToCart (idempotent via ON CONFLICT)
All checks were successful
Build & Publish Docker Image / build-and-push (push) Successful in 2m20s

This commit is contained in:
hsiegeln
2026-04-21 22:50:58 +02:00
parent 7dab267033
commit 8ceb5e95d7
2 changed files with 89 additions and 7 deletions

View File

@@ -25,12 +25,20 @@ export type ShoppingListSnapshot = {
};
export function addRecipeToCart(
_db: Database.Database,
_recipeId: number,
_profileId: number | null,
_servings?: number
db: Database.Database,
recipeId: number,
profileId: number | null,
servings?: number
): void {
throw new Error('not implemented');
const row = db
.prepare('SELECT servings_default FROM recipe WHERE id = ?')
.get(recipeId) as { servings_default: number | null } | undefined;
const resolved = servings ?? row?.servings_default ?? 4;
db.prepare(
`INSERT INTO shopping_cart_recipe (recipe_id, servings, added_by_profile_id)
VALUES (?, ?, ?)
ON CONFLICT(recipe_id) DO UPDATE SET servings = excluded.servings`
).run(recipeId, resolved, profileId);
}
export function removeRecipeFromCart(_db: Database.Database, _recipeId: number): void {
@@ -45,8 +53,17 @@ export function setCartServings(
throw new Error('not implemented');
}
export function listShoppingList(_db: Database.Database): ShoppingListSnapshot {
throw new Error('not implemented');
export function listShoppingList(db: Database.Database): ShoppingListSnapshot {
const recipes = db
.prepare(
`SELECT cr.recipe_id, r.title, r.image_path, cr.servings,
COALESCE(r.servings_default, cr.servings) AS servings_default
FROM shopping_cart_recipe cr
JOIN recipe r ON r.id = cr.recipe_id
ORDER BY cr.added_at ASC`
)
.all() as ShoppingCartRecipe[];
return { recipes, rows: [], uncheckedCount: 0 };
}
export function toggleCheck(