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>
105 lines
2.8 KiB
Go
105 lines
2.8 KiB
Go
package diary
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// ErrNotFound is returned when a diary entry does not exist for the user.
|
|
var ErrNotFound = errors.New("diary entry not found")
|
|
|
|
// Repository handles persistence for meal diary entries.
|
|
type Repository struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
// NewRepository creates a new Repository.
|
|
func NewRepository(pool *pgxpool.Pool) *Repository {
|
|
return &Repository{pool: pool}
|
|
}
|
|
|
|
// ListByDate returns all diary entries for a user on a given date (YYYY-MM-DD).
|
|
func (r *Repository) ListByDate(ctx context.Context, userID, date string) ([]*Entry, error) {
|
|
rows, err := r.pool.Query(ctx, `
|
|
SELECT id, date::text, meal_type, name, portions,
|
|
calories, protein_g, fat_g, carbs_g,
|
|
source, dish_id, recipe_id, portion_g, created_at
|
|
FROM meal_diary
|
|
WHERE user_id = $1 AND date = $2::date
|
|
ORDER BY created_at ASC`, userID, date)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("list diary: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var result []*Entry
|
|
for rows.Next() {
|
|
e, err := scanEntry(rows)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("scan diary entry: %w", err)
|
|
}
|
|
result = append(result, e)
|
|
}
|
|
return result, rows.Err()
|
|
}
|
|
|
|
// Create inserts a new diary entry and returns the stored record.
|
|
func (r *Repository) Create(ctx context.Context, userID string, req CreateRequest) (*Entry, error) {
|
|
portions := req.Portions
|
|
if portions <= 0 {
|
|
portions = 1
|
|
}
|
|
source := req.Source
|
|
if source == "" {
|
|
source = "manual"
|
|
}
|
|
|
|
row := r.pool.QueryRow(ctx, `
|
|
INSERT INTO meal_diary (user_id, date, meal_type, name, portions,
|
|
calories, protein_g, fat_g, carbs_g, source, dish_id, recipe_id, portion_g)
|
|
VALUES ($1, $2::date, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13)
|
|
RETURNING id, date::text, meal_type, name, portions,
|
|
calories, protein_g, fat_g, carbs_g, source, dish_id, recipe_id, portion_g, created_at`,
|
|
userID, req.Date, req.MealType, req.Name, portions,
|
|
req.Calories, req.ProteinG, req.FatG, req.CarbsG,
|
|
source, req.DishID, req.RecipeID, req.PortionG,
|
|
)
|
|
return scanEntry(row)
|
|
}
|
|
|
|
// Delete removes a diary entry for the given user.
|
|
func (r *Repository) Delete(ctx context.Context, id, userID string) error {
|
|
tag, err := r.pool.Exec(ctx,
|
|
`DELETE FROM meal_diary WHERE id = $1 AND user_id = $2`, id, userID)
|
|
if err != nil {
|
|
return fmt.Errorf("delete diary entry: %w", err)
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// --- helpers ---
|
|
|
|
type scannable interface {
|
|
Scan(dest ...any) error
|
|
}
|
|
|
|
func scanEntry(s scannable) (*Entry, error) {
|
|
var e Entry
|
|
err := s.Scan(
|
|
&e.ID, &e.Date, &e.MealType, &e.Name, &e.Portions,
|
|
&e.Calories, &e.ProteinG, &e.FatG, &e.CarbsG,
|
|
&e.Source, &e.DishID, &e.RecipeID, &e.PortionG, &e.CreatedAt,
|
|
)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, ErrNotFound
|
|
}
|
|
return &e, err
|
|
}
|