Public Access
fix: enrich ingredient names in meal detail API
Recipe JSONB stores ingredient_id but not name. GET /api/meals/items/{id}
now queries the Ingredient table and injects names into the response so
the frontend displays "3 cups onion" instead of just "3 cups".
This commit is contained in:
@@ -6,7 +6,7 @@ from sqlalchemy import text
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
from app.database import get_db
|
||||
from app.models import (
|
||||
MealPlan, MealPlanItem, MealPlanVote, Recipe,
|
||||
MealPlan, MealPlanItem, MealPlanVote, Recipe, Ingredient,
|
||||
FamilyProfile, FamilyMember, ApprovalToken,
|
||||
MealPlanStatus, MealPlanItemStatus, MealType, ApprovalTokenStatus
|
||||
)
|
||||
@@ -304,6 +304,18 @@ def get_meal_item(item_id: UUID, db: Session = Depends(get_db)):
|
||||
).first()
|
||||
if not item:
|
||||
raise HTTPException(status_code=404, detail="Meal plan item not found")
|
||||
|
||||
# Enrich ingredient names from the Ingredient table
|
||||
if item.recipe and item.recipe.ingredients:
|
||||
ing_ids = [ing.get("ingredient_id") for ing in item.recipe.ingredients if ing.get("ingredient_id")]
|
||||
if ing_ids:
|
||||
ingredients = db.query(Ingredient).filter(Ingredient.id.in_(ing_ids)).all()
|
||||
name_map = {str(i.id): i.name for i in ingredients}
|
||||
for ing in item.recipe.ingredients:
|
||||
ing_id = str(ing.get("ingredient_id", ""))
|
||||
if ing_id in name_map and not ing.get("name"):
|
||||
ing["name"] = name_map[ing_id]
|
||||
|
||||
return item
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user