feat: implement backend localization infrastructure
- Add internal/locale package: Parse(Accept-Language), FromContext/WithLang helpers, 12 supported languages - Add Language middleware that reads Accept-Language header and stores lang in context - Register Language middleware globally in server router (after CORS) Database migrations: - 009: create recipe_translations, saved_recipe_translations, ingredient_translations tables; migrate existing _ru data - 010: drop legacy _ru columns (title_ru, description_ru, canonical_name_ru); update FTS index Models: remove all _ru fields (TitleRu, DescriptionRu, NameRu, UnitRu, CanonicalNameRu) Repositories: - recipe: Upsert drops _ru params; GetByID does LEFT JOIN COALESCE on recipe_translations; ListMissingTranslation(lang); UpsertTranslation - ingredient: same pattern with ingredient_translations; Search now queries translated names/aliases - savedrecipe: List/GetByID LEFT JOIN COALESCE on saved_recipe_translations; UpsertTranslation Gemini: - RecipeRequest/MenuRequest gain Lang field - buildRecipePrompt rewritten in English with target-language content instruction; image_query always in English - GenerateMenu propagates Lang to GenerateRecipes Handlers: - recommendation/menu: pass locale.FromContext(ctx) as Lang - recognition: saveClassification stores Russian translation via UpsertTranslation instead of _ru column Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/food-ai/backend/internal/locale"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
@@ -13,7 +14,7 @@ import (
|
||||
// ErrNotFound is returned when a saved recipe does not exist for the given user.
|
||||
var ErrNotFound = errors.New("saved recipe not found")
|
||||
|
||||
// Repository handles persistence for saved recipes.
|
||||
// Repository handles persistence for saved recipes and their translations.
|
||||
type Repository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
@@ -24,6 +25,7 @@ func NewRepository(pool *pgxpool.Pool) *Repository {
|
||||
}
|
||||
|
||||
// Save persists a recipe for userID and returns the stored record.
|
||||
// The canonical content (any language) is stored directly in saved_recipes.
|
||||
func (r *Repository) Save(ctx context.Context, userID string, req SaveRequest) (*SavedRecipe, error) {
|
||||
const query = `
|
||||
INSERT INTO saved_recipes (
|
||||
@@ -61,16 +63,27 @@ func (r *Repository) Save(ctx context.Context, userID string, req SaveRequest) (
|
||||
}
|
||||
|
||||
// List returns all saved recipes for userID ordered by saved_at DESC.
|
||||
// Text content (title, description, ingredients, steps) is resolved for the
|
||||
// language stored in ctx, falling back to the canonical content when no
|
||||
// translation exists.
|
||||
func (r *Repository) List(ctx context.Context, userID string) ([]*SavedRecipe, error) {
|
||||
lang := locale.FromContext(ctx)
|
||||
const query = `
|
||||
SELECT id, user_id, title, description, cuisine, difficulty,
|
||||
prep_time_min, cook_time_min, servings, image_url,
|
||||
ingredients, steps, tags, nutrition, source, saved_at
|
||||
FROM saved_recipes
|
||||
WHERE user_id = $1
|
||||
ORDER BY saved_at DESC`
|
||||
SELECT sr.id, sr.user_id,
|
||||
COALESCE(srt.title, sr.title) AS title,
|
||||
COALESCE(srt.description, sr.description) AS description,
|
||||
sr.cuisine, sr.difficulty,
|
||||
sr.prep_time_min, sr.cook_time_min, sr.servings, sr.image_url,
|
||||
COALESCE(srt.ingredients, sr.ingredients) AS ingredients,
|
||||
COALESCE(srt.steps, sr.steps) AS steps,
|
||||
sr.tags, sr.nutrition, sr.source, sr.saved_at
|
||||
FROM saved_recipes sr
|
||||
LEFT JOIN saved_recipe_translations srt
|
||||
ON srt.saved_recipe_id = sr.id AND srt.lang = $2
|
||||
WHERE sr.user_id = $1
|
||||
ORDER BY sr.saved_at DESC`
|
||||
|
||||
rows, err := r.pool.Query(ctx, query, userID)
|
||||
rows, err := r.pool.Query(ctx, query, userID, lang)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list saved recipes: %w", err)
|
||||
}
|
||||
@@ -78,7 +91,7 @@ func (r *Repository) List(ctx context.Context, userID string) ([]*SavedRecipe, e
|
||||
|
||||
var result []*SavedRecipe
|
||||
for rows.Next() {
|
||||
rec, err := scanRows(rows)
|
||||
rec, err := scanRow(rows)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan saved recipe: %w", err)
|
||||
}
|
||||
@@ -88,15 +101,24 @@ func (r *Repository) List(ctx context.Context, userID string) ([]*SavedRecipe, e
|
||||
}
|
||||
|
||||
// GetByID returns the saved recipe with id for userID, or nil if not found.
|
||||
// Text content is resolved for the language stored in ctx.
|
||||
func (r *Repository) GetByID(ctx context.Context, userID, id string) (*SavedRecipe, error) {
|
||||
lang := locale.FromContext(ctx)
|
||||
const query = `
|
||||
SELECT id, user_id, title, description, cuisine, difficulty,
|
||||
prep_time_min, cook_time_min, servings, image_url,
|
||||
ingredients, steps, tags, nutrition, source, saved_at
|
||||
FROM saved_recipes
|
||||
WHERE id = $1 AND user_id = $2`
|
||||
SELECT sr.id, sr.user_id,
|
||||
COALESCE(srt.title, sr.title) AS title,
|
||||
COALESCE(srt.description, sr.description) AS description,
|
||||
sr.cuisine, sr.difficulty,
|
||||
sr.prep_time_min, sr.cook_time_min, sr.servings, sr.image_url,
|
||||
COALESCE(srt.ingredients, sr.ingredients) AS ingredients,
|
||||
COALESCE(srt.steps, sr.steps) AS steps,
|
||||
sr.tags, sr.nutrition, sr.source, sr.saved_at
|
||||
FROM saved_recipes sr
|
||||
LEFT JOIN saved_recipe_translations srt
|
||||
ON srt.saved_recipe_id = sr.id AND srt.lang = $3
|
||||
WHERE sr.id = $1 AND sr.user_id = $2`
|
||||
|
||||
rec, err := scanRow(r.pool.QueryRow(ctx, query, id, userID))
|
||||
rec, err := scanRow(r.pool.QueryRow(ctx, query, id, userID, lang))
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -119,6 +141,29 @@ func (r *Repository) Delete(ctx context.Context, userID, id string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpsertTranslation inserts or replaces a translation for a saved recipe.
|
||||
func (r *Repository) UpsertTranslation(
|
||||
ctx context.Context,
|
||||
id, lang string,
|
||||
title, description *string,
|
||||
ingredients, steps json.RawMessage,
|
||||
) error {
|
||||
const query = `
|
||||
INSERT INTO saved_recipe_translations (saved_recipe_id, lang, title, description, ingredients, steps)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT (saved_recipe_id, lang) DO UPDATE SET
|
||||
title = EXCLUDED.title,
|
||||
description = EXCLUDED.description,
|
||||
ingredients = EXCLUDED.ingredients,
|
||||
steps = EXCLUDED.steps,
|
||||
generated_at = now()`
|
||||
|
||||
if _, err := r.pool.Exec(ctx, query, id, lang, title, description, ingredients, steps); err != nil {
|
||||
return fmt.Errorf("upsert saved recipe translation %s/%s: %w", id, lang, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --- helpers ---
|
||||
|
||||
type scannable interface {
|
||||
@@ -146,11 +191,6 @@ func scanRow(s scannable) (*SavedRecipe, error) {
|
||||
return &rec, nil
|
||||
}
|
||||
|
||||
// scanRows wraps pgx.Rows to satisfy the scannable interface.
|
||||
func scanRows(rows pgx.Rows) (*SavedRecipe, error) {
|
||||
return scanRow(rows)
|
||||
}
|
||||
|
||||
func nullableStr(s string) *string {
|
||||
if s == "" {
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user