feat: translate recommendations and menu dishes into user language

- Generate recipes in English (reverted prompt to English-only)
- Add TranslateRecipes to OpenAI client (translate.go) — sends compact
  JSON payload of translatable fields, merges back into original recipes
- recommendation/handler.go: translate recipes in-memory before response
  when lang != "en"; falls back to English on error
- dish/repository.go: Create() now returns (dishID, recipeID, err) so
  callers can upsert dish_translations after saving
- menu/handler.go: saveRecipes returns savedRecipeEntry slice with dishID;
  saveDishTranslations calls TranslateRecipes then UpsertTranslation for
  each dish when the request locale is not English
- savedrecipe/repository.go: updated to ignore dishID from Create()
- init.go: wire openaiClient as RecipeTranslator and dishRepository as
  DishTranslator for menu.NewHandler

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-23 17:40:19 +02:00
parent cba50489be
commit bffeb05a43
7 changed files with 278 additions and 55 deletions

View File

@@ -289,11 +289,12 @@ func (r *Repository) AddRecipe(ctx context.Context, dishID string, req CreateReq
}
// Create creates a new dish + recipe row from a CreateRequest.
// Returns the recipe ID to be used in menu_items or user_saved_recipes.
func (r *Repository) Create(ctx context.Context, req CreateRequest) (recipeID string, err error) {
// Returns the dish ID and the recipe ID; the recipe ID is used in menu_items
// or user_saved_recipes, and the dish ID is used for upsert of translations.
func (r *Repository) Create(ctx context.Context, req CreateRequest) (dishID, recipeID string, err error) {
tx, err := r.pool.BeginTx(ctx, pgx.TxOptions{})
if err != nil {
return "", fmt.Errorf("begin tx: %w", err)
return "", "", fmt.Errorf("begin tx: %w", err)
}
defer tx.Rollback(ctx) //nolint:errcheck
@@ -301,7 +302,6 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest) (recipeID st
cuisineSlug := nullableStr(req.CuisineSlug)
// Insert dish.
var dishID string
err = tx.QueryRow(ctx, `
INSERT INTO dishes (cuisine_slug, name, description, image_url)
VALUES ($1, $2, NULLIF($3,''), NULLIF($4,''))
@@ -309,7 +309,7 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest) (recipeID st
cuisineSlug, req.Name, req.Description, req.ImageURL,
).Scan(&dishID)
if err != nil {
return "", fmt.Errorf("insert dish: %w", err)
return "", "", fmt.Errorf("insert dish: %w", err)
}
// Insert tags — upsert into tags first so the FK constraint is satisfied
@@ -319,13 +319,13 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest) (recipeID st
`INSERT INTO tags (slug, name) VALUES ($1, $2) ON CONFLICT (slug) DO NOTHING`,
slug, slug,
); upsertErr != nil {
return "", fmt.Errorf("upsert tag %s: %w", slug, upsertErr)
return "", "", fmt.Errorf("upsert tag %s: %w", slug, upsertErr)
}
if _, insertErr := tx.Exec(ctx,
`INSERT INTO dish_tags (dish_id, tag_slug) VALUES ($1, $2) ON CONFLICT DO NOTHING`,
dishID, slug,
); insertErr != nil {
return "", fmt.Errorf("insert dish tag %s: %w", slug, insertErr)
return "", "", fmt.Errorf("insert dish tag %s: %w", slug, insertErr)
}
}
@@ -352,7 +352,7 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest) (recipeID st
calories, protein, fat, carbs,
).Scan(&recipeID)
if err != nil {
return "", fmt.Errorf("insert recipe: %w", err)
return "", "", fmt.Errorf("insert recipe: %w", err)
}
// Insert recipe_ingredients.
@@ -362,7 +362,7 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest) (recipeID st
VALUES ($1, $2, $3, NULLIF($4,''), $5, $6)`,
recipeID, ing.Name, ing.Amount, ing.Unit, ing.IsOptional, i,
); err != nil {
return "", fmt.Errorf("insert ingredient %d: %w", i, err)
return "", "", fmt.Errorf("insert ingredient %d: %w", i, err)
}
}
@@ -377,14 +377,14 @@ func (r *Repository) Create(ctx context.Context, req CreateRequest) (recipeID st
VALUES ($1, $2, $3, $4)`,
recipeID, num, s.TimerSeconds, s.Description,
); err != nil {
return "", fmt.Errorf("insert step %d: %w", num, err)
return "", "", fmt.Errorf("insert step %d: %w", num, err)
}
}
if err := tx.Commit(ctx); err != nil {
return "", fmt.Errorf("commit: %w", err)
return "", "", fmt.Errorf("commit: %w", err)
}
return recipeID, nil
return dishID, recipeID, nil
}
// loadTags fills d.Tags with the slugs from dish_tags.