Public Access
feat(meals): enforce three lower-calorie dinners with media
This commit is contained in:
+37
-11
@@ -10,7 +10,13 @@ from sqlalchemy import or_
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.database import get_db
|
||||
from app.models import FamilyProfile, Ingredient, NeverSuggest, Recipe
|
||||
from app.models import (
|
||||
FamilyProfile,
|
||||
Ingredient,
|
||||
NeverSuggest,
|
||||
Recipe,
|
||||
RecipeCatalogStatus,
|
||||
)
|
||||
from app.schemas.recipe import (
|
||||
RecipeCreate,
|
||||
RecipeRead,
|
||||
@@ -45,7 +51,19 @@ def _validate_ingredient_ids(db: Session, ingredient_refs) -> None:
|
||||
)
|
||||
|
||||
|
||||
def _serialize(row: Recipe) -> dict:
|
||||
def _serialize(row: Recipe, db: Session) -> dict:
|
||||
raw_ingredients = row.ingredients or []
|
||||
# Resolve ingredient names from the canonical Ingredient table
|
||||
ingredient_ids = [ref["ingredient_id"] for ref in raw_ingredients if ref.get("ingredient_id")]
|
||||
name_map = {}
|
||||
if ingredient_ids:
|
||||
name_rows = db.query(Ingredient.id, Ingredient.name).filter(Ingredient.id.in_(ingredient_ids)).all()
|
||||
name_map = {str(r[0]): r[1] for r in name_rows}
|
||||
enriched_ingredients = []
|
||||
for ref in raw_ingredients:
|
||||
enriched = dict(ref)
|
||||
enriched["name"] = name_map.get(str(ref.get("ingredient_id")), "unknown ingredient")
|
||||
enriched_ingredients.append(enriched)
|
||||
return {
|
||||
"id": row.id,
|
||||
"name": row.name,
|
||||
@@ -59,7 +77,7 @@ def _serialize(row: Recipe) -> dict:
|
||||
"protein_type": row.protein_type,
|
||||
"spice_level": row.spice_level,
|
||||
"calories_per_serving": row.calories_per_serving,
|
||||
"ingredients": row.ingredients or [],
|
||||
"ingredients": enriched_ingredients,
|
||||
"side_dishes": row.side_dishes or [],
|
||||
"instructions": list(row.instructions or []),
|
||||
"source_url": row.source_url,
|
||||
@@ -91,7 +109,9 @@ def list_recommended_recipes(
|
||||
}
|
||||
|
||||
# Start from all recipes
|
||||
query = db.query(Recipe).filter(~Recipe.id.in_(blocked)) if blocked else db.query(Recipe)
|
||||
query = db.query(Recipe).filter(Recipe.catalog_status == RecipeCatalogStatus.ACTIVE)
|
||||
if blocked:
|
||||
query = query.filter(~Recipe.id.in_(blocked))
|
||||
|
||||
# Apply positive signal filters
|
||||
pos_cuisines = {s.value for s in result.positive_signals if s.type == "prefer_cuisine"}
|
||||
@@ -119,6 +139,7 @@ def list_recommended_recipes(
|
||||
top_rated = result.top_rated_recipe_names or []
|
||||
if top_rated:
|
||||
top_rows = db.query(Recipe).filter(
|
||||
Recipe.catalog_status == RecipeCatalogStatus.ACTIVE,
|
||||
Recipe.name.in_(top_rated),
|
||||
(~Recipe.id.in_(blocked) if blocked else True),
|
||||
).limit(limit).all()
|
||||
@@ -133,7 +154,7 @@ def list_recommended_recipes(
|
||||
ids_seen.add(r.id)
|
||||
ordered.append(r)
|
||||
|
||||
return [_serialize(r) for r in ordered]
|
||||
return [_serialize(r, db) for r in ordered]
|
||||
|
||||
|
||||
@public_router.get("", response_model=List[RecipeRead])
|
||||
@@ -150,7 +171,9 @@ def list_recipes(
|
||||
limit: int = Query(default=100, le=500),
|
||||
db: Session = Depends(get_db),
|
||||
):
|
||||
query = db.query(Recipe)
|
||||
query = db.query(Recipe).filter(
|
||||
Recipe.catalog_status == RecipeCatalogStatus.ACTIVE
|
||||
)
|
||||
if q:
|
||||
like = f"%{q.lower()}%"
|
||||
query = query.filter(Recipe.name.ilike(like))
|
||||
@@ -194,15 +217,18 @@ def list_recipes(
|
||||
if blocked:
|
||||
query = query.filter(~Recipe.id.in_(blocked))
|
||||
rows = query.order_by(Recipe.name).limit(limit).all()
|
||||
return [_serialize(r) for r in rows]
|
||||
return [_serialize(r, db) for r in rows]
|
||||
|
||||
|
||||
@public_router.get("/{recipe_id}", response_model=RecipeRead)
|
||||
def get_recipe(recipe_id: UUID, db: Session = Depends(get_db)):
|
||||
row = db.query(Recipe).filter(Recipe.id == recipe_id).first()
|
||||
row = db.query(Recipe).filter(
|
||||
Recipe.id == recipe_id,
|
||||
Recipe.catalog_status == RecipeCatalogStatus.ACTIVE,
|
||||
).first()
|
||||
if row is None:
|
||||
raise HTTPException(status_code=404, detail="recipe not found")
|
||||
return _serialize(row)
|
||||
return _serialize(row, db)
|
||||
|
||||
|
||||
@admin_router.post("", response_model=RecipeRead, status_code=status.HTTP_201_CREATED)
|
||||
@@ -228,7 +254,7 @@ def create_recipe(payload: RecipeCreate, db: Session = Depends(get_db)):
|
||||
db.add(row)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return _serialize(row)
|
||||
return _serialize(row, db)
|
||||
|
||||
|
||||
@admin_router.patch("/{recipe_id}", response_model=RecipeRead)
|
||||
@@ -245,7 +271,7 @@ def update_recipe(recipe_id: UUID, payload: RecipeUpdate, db: Session = Depends(
|
||||
setattr(row, field, value)
|
||||
db.commit()
|
||||
db.refresh(row)
|
||||
return _serialize(row)
|
||||
return _serialize(row, db)
|
||||
|
||||
|
||||
@admin_router.delete("/{recipe_id}", status_code=status.HTTP_204_NO_CONTENT, response_class=Response)
|
||||
|
||||
Reference in New Issue
Block a user