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:
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// Repository handles persistence for ingredient_mappings and their translations.
|
||||
// Repository handles persistence for ingredients and their translations.
|
||||
type Repository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
@@ -21,11 +21,11 @@ func NewRepository(pool *pgxpool.Pool) *Repository {
|
||||
return &Repository{pool: pool}
|
||||
}
|
||||
|
||||
// Upsert inserts or updates an ingredient mapping (English canonical content).
|
||||
// Upsert inserts or updates an ingredient (English canonical content).
|
||||
// Conflict is resolved on canonical_name.
|
||||
func (r *Repository) Upsert(ctx context.Context, m *IngredientMapping) (*IngredientMapping, error) {
|
||||
query := `
|
||||
INSERT INTO ingredient_mappings (
|
||||
INSERT INTO ingredients (
|
||||
canonical_name,
|
||||
category, default_unit,
|
||||
calories_per_100g, protein_per_100g, fat_per_100g, carbs_per_100g, fiber_per_100g,
|
||||
@@ -54,7 +54,7 @@ func (r *Repository) Upsert(ctx context.Context, m *IngredientMapping) (*Ingredi
|
||||
return scanMappingWrite(row)
|
||||
}
|
||||
|
||||
// GetByID returns an ingredient mapping by UUID.
|
||||
// GetByID returns an ingredient by UUID.
|
||||
// CanonicalName and aliases are resolved for the language stored in ctx.
|
||||
// Returns nil, nil if not found.
|
||||
func (r *Repository) GetByID(ctx context.Context, id string) (*IngredientMapping, error) {
|
||||
@@ -68,7 +68,7 @@ func (r *Repository) GetByID(ctx context.Context, id string) (*IngredientMapping
|
||||
im.calories_per_100g, im.protein_per_100g, im.fat_per_100g, im.carbs_per_100g, im.fiber_per_100g,
|
||||
im.storage_days, im.created_at, im.updated_at,
|
||||
COALESCE(al.aliases, '[]'::json) AS aliases
|
||||
FROM ingredient_mappings im
|
||||
FROM ingredients im
|
||||
LEFT JOIN ingredient_translations it
|
||||
ON it.ingredient_id = im.id AND it.lang = $2
|
||||
LEFT JOIN ingredient_category_translations ict
|
||||
@@ -88,7 +88,7 @@ func (r *Repository) GetByID(ctx context.Context, id string) (*IngredientMapping
|
||||
return m, err
|
||||
}
|
||||
|
||||
// FuzzyMatch finds the single best matching ingredient mapping for a given name.
|
||||
// FuzzyMatch finds the single best matching ingredient for a given name.
|
||||
// Searches both English and translated names for the language in ctx.
|
||||
// Returns nil, nil when no match is found.
|
||||
func (r *Repository) FuzzyMatch(ctx context.Context, name string) (*IngredientMapping, error) {
|
||||
@@ -102,7 +102,7 @@ func (r *Repository) FuzzyMatch(ctx context.Context, name string) (*IngredientMa
|
||||
return results[0], nil
|
||||
}
|
||||
|
||||
// Search finds ingredient mappings matching the query string.
|
||||
// Search finds ingredients matching the query string.
|
||||
// Searches aliases table and translated names for the language in ctx.
|
||||
func (r *Repository) Search(ctx context.Context, query string, limit int) ([]*IngredientMapping, error) {
|
||||
if limit <= 0 {
|
||||
@@ -118,7 +118,7 @@ func (r *Repository) Search(ctx context.Context, query string, limit int) ([]*In
|
||||
im.calories_per_100g, im.protein_per_100g, im.fat_per_100g, im.carbs_per_100g, im.fiber_per_100g,
|
||||
im.storage_days, im.created_at, im.updated_at,
|
||||
COALESCE(al.aliases, '[]'::json) AS aliases
|
||||
FROM ingredient_mappings im
|
||||
FROM ingredients im
|
||||
LEFT JOIN ingredient_translations it
|
||||
ON it.ingredient_id = im.id AND it.lang = $3
|
||||
LEFT JOIN ingredient_category_translations ict
|
||||
@@ -142,17 +142,17 @@ func (r *Repository) Search(ctx context.Context, query string, limit int) ([]*In
|
||||
|
||||
rows, err := r.pool.Query(ctx, q, query, limit, lang)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("search ingredient_mappings: %w", err)
|
||||
return nil, fmt.Errorf("search ingredients: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
return collectMappingsRead(rows)
|
||||
}
|
||||
|
||||
// Count returns the total number of ingredient mappings.
|
||||
// Count returns the total number of ingredients.
|
||||
func (r *Repository) Count(ctx context.Context) (int, error) {
|
||||
var n int
|
||||
if err := r.pool.QueryRow(ctx, `SELECT count(*) FROM ingredient_mappings`).Scan(&n); err != nil {
|
||||
return 0, fmt.Errorf("count ingredient_mappings: %w", err)
|
||||
if err := r.pool.QueryRow(ctx, `SELECT count(*) FROM ingredients`).Scan(&n); err != nil {
|
||||
return 0, fmt.Errorf("count ingredients: %w", err)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
@@ -165,7 +165,7 @@ func (r *Repository) ListMissingTranslation(ctx context.Context, lang string, li
|
||||
im.category, im.default_unit,
|
||||
im.calories_per_100g, im.protein_per_100g, im.fat_per_100g, im.carbs_per_100g, im.fiber_per_100g,
|
||||
im.storage_days, im.created_at, im.updated_at
|
||||
FROM ingredient_mappings im
|
||||
FROM ingredients im
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM ingredient_translations it
|
||||
WHERE it.ingredient_id = im.id AND it.lang = $3
|
||||
|
||||
Reference in New Issue
Block a user