diff --git a/frontend/js/db.js b/frontend/js/db.js index 21b3f70..c0b52f8 100644 --- a/frontend/js/db.js +++ b/frontend/js/db.js @@ -162,6 +162,13 @@ async function addProduct(p) { return rows[0]?.id; } +async function deleteProduct(id) { + // Reassign food_log and recipe_ingredients to avoid orphaned references + await execute("UPDATE food_log SET product_id = NULL WHERE product_id = ?1", [id]); + await execute("UPDATE recipe_ingredients SET product_id = 0 WHERE product_id = ?1", [id]); + await execute("DELETE FROM products WHERE id = ?1", [id]); +} + // --------------------------------------------------------------------------- // Cooking methods & Recipes // --------------------------------------------------------------------------- @@ -898,6 +905,7 @@ window.DB.saveSleep = saveSleep; window.DB.searchProducts = searchProducts; window.DB.getProduct = getProduct; window.DB.addProduct = addProduct; +window.DB.deleteProduct = deleteProduct; window.DB.getFoodForDate = getFoodForDate; window.DB.addFoodManual = addFoodManual; window.DB.addFoodEntry = addFoodEntry; diff --git a/frontend/js/main.js b/frontend/js/main.js index cbda855..96c8743 100644 --- a/frontend/js/main.js +++ b/frontend/js/main.js @@ -1295,6 +1295,7 @@ async function renderProducts(container) {