feat: slim meal_diary — derive name and nutrition from dish/recipe
Remove denormalized columns (name, calories, protein_g, fat_g, carbs_g) from meal_diary. Name is now resolved via JOIN with dishes/dish_translations; macros are computed as recipe.*_per_serving * portions at query time. - Add dish.Repository.FindOrCreateRecipe: finds or creates a minimal recipe stub seeded with AI-estimated macros - recognition/handler: resolve recipe_id synchronously per candidate; simplify enrichDishInBackground to translations-only - diary/handler: accept dish_id OR name; always resolve recipe_id via FindOrCreateRecipe before INSERT - diary/entity: DishID is now non-nullable string; CreateRequest drops macros - diary/repository: ListByDate and Create use JOIN to return computed macros - ai/types: add RecipeID field to DishCandidate - Update tests and wire_gen accordingly Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/food-ai/backend/internal/infra/locale"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
@@ -23,14 +24,24 @@ func NewRepository(pool *pgxpool.Pool) *Repository {
|
||||
}
|
||||
|
||||
// ListByDate returns all diary entries for a user on a given date (YYYY-MM-DD).
|
||||
// Dish name and macros are computed via JOIN with dishes and recipes.
|
||||
func (r *Repository) ListByDate(ctx context.Context, userID, date string) ([]*Entry, error) {
|
||||
lang := locale.FromContext(ctx)
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, date::text, meal_type, name, portions,
|
||||
calories, protein_g, fat_g, carbs_g,
|
||||
source, dish_id, recipe_id, portion_g, created_at
|
||||
FROM meal_diary
|
||||
WHERE user_id = $1 AND date = $2::date
|
||||
ORDER BY created_at ASC`, userID, date)
|
||||
SELECT
|
||||
md.id, md.date::text, md.meal_type, md.portions,
|
||||
md.source, md.dish_id::text, md.recipe_id::text, md.portion_g, md.created_at,
|
||||
COALESCE(dt.name, d.name) AS dish_name,
|
||||
r.calories_per_serving * md.portions,
|
||||
r.protein_per_serving * md.portions,
|
||||
r.fat_per_serving * md.portions,
|
||||
r.carbs_per_serving * md.portions
|
||||
FROM meal_diary md
|
||||
JOIN dishes d ON d.id = md.dish_id
|
||||
LEFT JOIN dish_translations dt ON dt.dish_id = d.id AND dt.lang = $3
|
||||
LEFT JOIN recipes r ON r.id = md.recipe_id
|
||||
WHERE md.user_id = $1 AND md.date = $2::date
|
||||
ORDER BY md.created_at ASC`, userID, date, lang)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list diary: %w", err)
|
||||
}
|
||||
@@ -38,17 +49,18 @@ func (r *Repository) ListByDate(ctx context.Context, userID, date string) ([]*En
|
||||
|
||||
var result []*Entry
|
||||
for rows.Next() {
|
||||
e, err := scanEntry(rows)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan diary entry: %w", err)
|
||||
entry, scanError := scanEntry(rows)
|
||||
if scanError != nil {
|
||||
return nil, fmt.Errorf("scan diary entry: %w", scanError)
|
||||
}
|
||||
result = append(result, e)
|
||||
result = append(result, entry)
|
||||
}
|
||||
return result, rows.Err()
|
||||
}
|
||||
|
||||
// Create inserts a new diary entry and returns the stored record.
|
||||
// Create inserts a new diary entry and returns the stored record (with computed macros).
|
||||
func (r *Repository) Create(ctx context.Context, userID string, req CreateRequest) (*Entry, error) {
|
||||
lang := locale.FromContext(ctx)
|
||||
portions := req.Portions
|
||||
if portions <= 0 {
|
||||
portions = 1
|
||||
@@ -58,25 +70,40 @@ func (r *Repository) Create(ctx context.Context, userID string, req CreateReques
|
||||
source = "manual"
|
||||
}
|
||||
|
||||
var entryID string
|
||||
insertError := r.pool.QueryRow(ctx, `
|
||||
INSERT INTO meal_diary (user_id, date, meal_type, portions, source, dish_id, recipe_id, portion_g)
|
||||
VALUES ($1, $2::date, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING id`,
|
||||
userID, req.Date, req.MealType, portions, source, req.DishID, req.RecipeID, req.PortionG,
|
||||
).Scan(&entryID)
|
||||
if insertError != nil {
|
||||
return nil, fmt.Errorf("insert diary entry: %w", insertError)
|
||||
}
|
||||
|
||||
row := r.pool.QueryRow(ctx, `
|
||||
INSERT INTO meal_diary (user_id, date, meal_type, name, portions,
|
||||
calories, protein_g, fat_g, carbs_g, source, dish_id, recipe_id, portion_g)
|
||||
VALUES ($1, $2::date, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
|
||||
RETURNING id, date::text, meal_type, name, portions,
|
||||
calories, protein_g, fat_g, carbs_g, source, dish_id, recipe_id, portion_g, created_at`,
|
||||
userID, req.Date, req.MealType, req.Name, portions,
|
||||
req.Calories, req.ProteinG, req.FatG, req.CarbsG,
|
||||
source, req.DishID, req.RecipeID, req.PortionG,
|
||||
)
|
||||
SELECT
|
||||
md.id, md.date::text, md.meal_type, md.portions,
|
||||
md.source, md.dish_id::text, md.recipe_id::text, md.portion_g, md.created_at,
|
||||
COALESCE(dt.name, d.name) AS dish_name,
|
||||
r.calories_per_serving * md.portions,
|
||||
r.protein_per_serving * md.portions,
|
||||
r.fat_per_serving * md.portions,
|
||||
r.carbs_per_serving * md.portions
|
||||
FROM meal_diary md
|
||||
JOIN dishes d ON d.id = md.dish_id
|
||||
LEFT JOIN dish_translations dt ON dt.dish_id = d.id AND dt.lang = $2
|
||||
LEFT JOIN recipes r ON r.id = md.recipe_id
|
||||
WHERE md.id = $1`, entryID, lang)
|
||||
return scanEntry(row)
|
||||
}
|
||||
|
||||
// Delete removes a diary entry for the given user.
|
||||
func (r *Repository) Delete(ctx context.Context, id, userID string) error {
|
||||
tag, err := r.pool.Exec(ctx,
|
||||
tag, deleteError := r.pool.Exec(ctx,
|
||||
`DELETE FROM meal_diary WHERE id = $1 AND user_id = $2`, id, userID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete diary entry: %w", err)
|
||||
if deleteError != nil {
|
||||
return fmt.Errorf("delete diary entry: %w", deleteError)
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return ErrNotFound
|
||||
@@ -91,14 +118,15 @@ type scannable interface {
|
||||
}
|
||||
|
||||
func scanEntry(s scannable) (*Entry, error) {
|
||||
var e Entry
|
||||
err := s.Scan(
|
||||
&e.ID, &e.Date, &e.MealType, &e.Name, &e.Portions,
|
||||
&e.Calories, &e.ProteinG, &e.FatG, &e.CarbsG,
|
||||
&e.Source, &e.DishID, &e.RecipeID, &e.PortionG, &e.CreatedAt,
|
||||
var entry Entry
|
||||
scanError := s.Scan(
|
||||
&entry.ID, &entry.Date, &entry.MealType, &entry.Portions,
|
||||
&entry.Source, &entry.DishID, &entry.RecipeID, &entry.PortionG, &entry.CreatedAt,
|
||||
&entry.Name,
|
||||
&entry.Calories, &entry.ProteinG, &entry.FatG, &entry.CarbsG,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
if errors.Is(scanError, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
return &e, err
|
||||
return &entry, scanError
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user