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>
This commit is contained in:
28
backend/internal/tag/handler.go
Normal file
28
backend/internal/tag/handler.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package tag
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"github.com/food-ai/backend/internal/locale"
|
||||
)
|
||||
|
||||
type tagItem struct {
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// List handles GET /tags — returns tags with names in the requested language.
|
||||
func List(w http.ResponseWriter, r *http.Request) {
|
||||
lang := locale.FromContext(r.Context())
|
||||
items := make([]tagItem, 0, len(Records))
|
||||
for _, t := range Records {
|
||||
name, ok := t.Translations[lang]
|
||||
if !ok {
|
||||
name = t.Name
|
||||
}
|
||||
items = append(items, tagItem{Slug: t.Slug, Name: name})
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"tags": items})
|
||||
}
|
||||
79
backend/internal/tag/registry.go
Normal file
79
backend/internal/tag/registry.go
Normal file
@@ -0,0 +1,79 @@
|
||||
package tag
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// Record is a tag loaded from DB with all its translations.
|
||||
type Record struct {
|
||||
Slug string
|
||||
Name string // English canonical name
|
||||
SortOrder int
|
||||
Translations map[string]string // lang → localized name
|
||||
}
|
||||
|
||||
// Records is the ordered list of tags, populated by LoadFromDB at startup.
|
||||
var Records []Record
|
||||
|
||||
// LoadFromDB queries tags + tag_translations and populates Records.
|
||||
func LoadFromDB(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
rows, err := pool.Query(ctx, `
|
||||
SELECT t.slug, t.name, t.sort_order, tt.lang, tt.name
|
||||
FROM tags t
|
||||
LEFT JOIN tag_translations tt ON tt.tag_slug = t.slug
|
||||
ORDER BY t.sort_order, tt.lang`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("load tags 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 tag slug.
|
||||
// Falls back to the English canonical name.
|
||||
func NameFor(slug, lang string) string {
|
||||
for _, t := range Records {
|
||||
if t.Slug == slug {
|
||||
if name, ok := t.Translations[lang]; ok {
|
||||
return name
|
||||
}
|
||||
return t.Name
|
||||
}
|
||||
}
|
||||
return slug
|
||||
}
|
||||
Reference in New Issue
Block a user