feat: Rezepte beim Zuordnen anlegen und offene Zutaten listen
Lebensmittel oder Rezept wählbar; gemappte Zutaten werden übernommen, offene erscheinen in der Liste. Dazu Fettgehalt-Suche, EPA-Stoffe, Rezept-CRUD und Wechsel bestehender Zuordnungen. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
132a364a3a
commit
a7e5511039
26 files changed
+1234
-147
No files matched your search
@@ -108,12 +108,12 @@ def list_recipes(cur, profile_id: str) -> list[dict[str, Any]]:
|
||||
recipes = [dict(r) for r in cur.fetchall()]
|
||||
if not recipes:
|
||||
return []
|
||||
ids = [r["id"] for r in recipes]
|
||||
ids = [str(r["id"]) for r in recipes]
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT recipe_id, source_name_raw, source_name_normalized, quantity_raw, quantity_g, sort_order
|
||||
FROM food_recipe_ingredients
|
||||
WHERE recipe_id = ANY(%s)
|
||||
WHERE recipe_id = ANY(%s::uuid[])
|
||||
ORDER BY sort_order, source_name_raw
|
||||
""",
|
||||
(ids,),
|
||||
@@ -122,10 +122,109 @@ def list_recipes(cur, profile_id: str) -> list[dict[str, Any]]:
|
||||
for row in cur.fetchall():
|
||||
by_r.setdefault(str(row["recipe_id"]), []).append(dict(row))
|
||||
for rec in recipes:
|
||||
rec["ingredients"] = by_r.get(str(rec["id"]), [])
|
||||
rec["id"] = str(rec["id"])
|
||||
rec["ingredients"] = by_r.get(rec["id"], [])
|
||||
return recipes
|
||||
|
||||
|
||||
def get_recipe(cur, profile_id: str, recipe_id: str) -> dict[str, Any] | None:
|
||||
for rec in list_recipes(cur, profile_id):
|
||||
if rec["id"] == str(recipe_id):
|
||||
return rec
|
||||
return None
|
||||
|
||||
|
||||
def save_recipe(cur, profile_id: str, rec: dict[str, Any], recipe_id: str | None = None) -> dict[str, Any]:
|
||||
name_raw = (rec.get("name_raw") or "").strip()
|
||||
norm = rec.get("name_normalized") or normalize_food_name(name_raw)
|
||||
if not name_raw or not norm:
|
||||
raise ValueError("Rezeptname fehlt")
|
||||
try:
|
||||
portions = float(rec.get("portions") or 1)
|
||||
except (TypeError, ValueError):
|
||||
portions = 1.0
|
||||
if portions <= 0:
|
||||
portions = 1.0
|
||||
description = rec.get("description")
|
||||
source = (rec.get("source") or "manual").strip() or "manual"
|
||||
if recipe_id:
|
||||
cur.execute(
|
||||
"SELECT id FROM food_recipes WHERE id = %s AND profile_id = %s",
|
||||
(recipe_id, profile_id),
|
||||
)
|
||||
if not cur.fetchone():
|
||||
raise KeyError("Rezept nicht gefunden")
|
||||
cur.execute(
|
||||
"""
|
||||
SELECT id FROM food_recipes
|
||||
WHERE profile_id = %s AND name_normalized = %s AND id <> %s
|
||||
""",
|
||||
(profile_id, norm, recipe_id),
|
||||
)
|
||||
if cur.fetchone():
|
||||
raise ValueError("Ein Rezept mit diesem Namen existiert bereits")
|
||||
cur.execute(
|
||||
"""
|
||||
UPDATE food_recipes
|
||||
SET name_raw=%s, name_normalized=%s, portions=%s, description=%s, updated_at=NOW()
|
||||
WHERE id=%s AND profile_id=%s
|
||||
""",
|
||||
(name_raw, norm, portions, description, recipe_id, profile_id),
|
||||
)
|
||||
cur.execute("DELETE FROM food_recipe_ingredients WHERE recipe_id = %s", (recipe_id,))
|
||||
rid = recipe_id
|
||||
else:
|
||||
cur.execute(
|
||||
"SELECT id FROM food_recipes WHERE profile_id = %s AND name_normalized = %s",
|
||||
(profile_id, norm),
|
||||
)
|
||||
existing = cur.fetchone()
|
||||
if existing:
|
||||
return save_recipe(cur, profile_id, rec, str(existing["id"]))
|
||||
rid = str(uuid.uuid4())
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO food_recipes
|
||||
(id, profile_id, name_raw, name_normalized, portions, description, source)
|
||||
VALUES (%s,%s,%s,%s,%s,%s,%s)
|
||||
""",
|
||||
(rid, profile_id, name_raw, norm, portions, description, source),
|
||||
)
|
||||
for i, ing in enumerate(rec.get("ingredients") or []):
|
||||
raw = (ing.get("source_name_raw") or "").strip()
|
||||
inorm = ing.get("source_name_normalized") or normalize_food_name(raw)
|
||||
if not inorm:
|
||||
continue
|
||||
qty = ing.get("quantity_g")
|
||||
try:
|
||||
qty = float(qty) if qty not in (None, "") else None
|
||||
except (TypeError, ValueError):
|
||||
qty = None
|
||||
cur.execute(
|
||||
"""
|
||||
INSERT INTO food_recipe_ingredients
|
||||
(id, recipe_id, source_name_raw, source_name_normalized,
|
||||
quantity_raw, quantity_g, sort_order)
|
||||
VALUES (%s,%s,%s,%s,%s,%s,%s)
|
||||
""",
|
||||
(str(uuid.uuid4()), rid, raw or inorm, inorm, ing.get("quantity_raw"), qty, i),
|
||||
)
|
||||
link_recipes_to_items(cur, profile_id)
|
||||
saved = get_recipe(cur, profile_id, rid)
|
||||
if not saved:
|
||||
raise ValueError("Rezept konnte nicht gelesen werden")
|
||||
return saved
|
||||
|
||||
|
||||
def delete_recipe(cur, profile_id: str, recipe_id: str) -> None:
|
||||
cur.execute(
|
||||
"DELETE FROM food_recipes WHERE id = %s AND profile_id = %s RETURNING id",
|
||||
(recipe_id, profile_id),
|
||||
)
|
||||
if not cur.fetchone():
|
||||
raise KeyError("Rezept nicht gefunden")
|
||||
|
||||
|
||||
def apply_recipe_to_items(cur, profile_id: str, source_name_normalized: str, recipe_id: str) -> int:
|
||||
cur.execute(
|
||||
"""
|
||||
|
||||
Reference in new issue
Block a user