Files
food-ai/backend/internal/cuisine/registry.go
dbastrikin 61feb91bba feat: core schema redesign — dishes, structured recipes, cuisines, tags (iteration 7)
Replaces the flat JSONB-based recipe schema with a normalized relational model:

Schema (migrations consolidated to 001_initial_schema + 002_seed_data):
- New: dishes, dish_translations, dish_tags — canonical dish catalog
- New: cuisines, tags, dish_categories with _translations tables + full seed data
- New: recipe_ingredients, recipe_steps with _translations (replaces JSONB blobs)
- New: user_saved_recipes thin bookmark (drops saved_recipes + saved_recipe_translations)
- New: product_ingredients M2M table
- recipes: now a cooking variant of a dish (dish_id FK, no title/JSONB columns)
- recipe_translations: repurposed to per-language notes only
- products: mapping_id → primary_ingredient_id
- menu_items: recipe_id FK → recipes; adds dish_id
- meal_diary: adds dish_id, recipe_id → recipes, portion_g

Backend (Go):
- New packages: internal/cuisine, internal/tag, internal/dish (registry + handler + repo)
- New GET /cuisines, GET /tags (public), GET /dishes, GET /dishes/{id}, GET /recipes/{id}
- recipe, savedrecipe, menu, diary, product, ingredient packages updated for new schema

Flutter:
- New models: Cuisine, Tag; new providers: cuisineNamesProvider, tagNamesProvider
- recipe.dart: RecipeIngredient gains unit_code + effectiveUnit getter
- saved_recipe.dart: thin model, manual fromJson, computed nutrition getter
- diary_entry.dart: adds dishId, recipeId, portionG
- recipe_detail_screen.dart: localized cuisine/tag names via providers

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-15 18:01:24 +02:00

81 lines
1.9 KiB
Go

package cuisine
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
)
// Record is a cuisine loaded from DB with all its translations.
type Record struct {
Slug string
Name string // English canonical name
SortOrder int
// Translations maps lang code to localized name.
Translations map[string]string
}
// Records is the ordered list of cuisines, populated by LoadFromDB at startup.
var Records []Record
// LoadFromDB queries cuisines + cuisine_translations and populates Records.
func LoadFromDB(ctx context.Context, pool *pgxpool.Pool) error {
rows, err := pool.Query(ctx, `
SELECT c.slug, c.name, c.sort_order, ct.lang, ct.name
FROM cuisines c
LEFT JOIN cuisine_translations ct ON ct.cuisine_slug = c.slug
ORDER BY c.sort_order, ct.lang`)
if err != nil {
return fmt.Errorf("load cuisines from db: %w", err)
}
defer rows.Close()
bySlug := map[string]*Record{}
var order []string
for rows.Next() {
var slug, engName string
var sortOrder int
var lang, name *string
if err := rows.Scan(&slug, &engName, &sortOrder, &lang, &name); err != nil {
return err
}
if _, ok := bySlug[slug]; !ok {
bySlug[slug] = &Record{
Slug: slug,
Name: engName,
SortOrder: sortOrder,
Translations: map[string]string{},
}
order = append(order, slug)
}
if lang != nil && name != nil {
bySlug[slug].Translations[*lang] = *name
}
}
if err := rows.Err(); err != nil {
return err
}
result := make([]Record, 0, len(order))
for _, slug := range order {
result = append(result, *bySlug[slug])
}
Records = result
return nil
}
// NameFor returns the localized name for a cuisine slug.
// Falls back to the English canonical name.
func NameFor(slug, lang string) string {
for _, c := range Records {
if c.Slug == slug {
if name, ok := c.Translations[lang]; ok {
return name
}
return c.Name
}
}
return slug
}