feat: rename ingredients→products, products→user_products; add barcode/OFF import
- Rename catalog: ingredient/* → product/* (canonical_name, barcode, nutrition per 100g)
- Rename pantry: product/* → userproduct/* (user-owned items with expiry)
- Squash migrations into single 001_initial_schema.sql (clean-db baseline)
- product_categories: add English canonical name column; fix COALESCE in queries
- Remove product_translations: product names are stored in their original language
- Add default_unit_name to product API responses via unit_translations JOIN
- Add cmd/importoff: bulk import from OpenFoodFacts JSONL dump (COPY + ON CONFLICT)
- Diary: support product_id entries alongside dish_id (CHECK num_nonnulls = 1)
- Home: getLoggedCalories joins both recipes and catalog products
- Flutter: rename models/providers/services to match backend rename
- Flutter: add barcode scan flow for diary (mobile_scanner, product_portion_sheet)
- Flutter: localise 6 new keys across 12 languages (barcode scan, portion weight)
- Routes: GET /products/search, GET /products/barcode/{barcode}, /user-products
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -7,14 +7,15 @@ type Entry struct {
|
||||
ID string `json:"id"`
|
||||
Date string `json:"date"` // YYYY-MM-DD
|
||||
MealType string `json:"meal_type"`
|
||||
Name string `json:"name"` // from dishes JOIN
|
||||
Name string `json:"name"` // from dishes or products JOIN
|
||||
Portions float64 `json:"portions"`
|
||||
Calories *float64 `json:"calories,omitempty"` // recipe.calories_per_serving * portions
|
||||
Calories *float64 `json:"calories,omitempty"` // recipe.calories_per_serving * portions, or product * portion_g
|
||||
ProteinG *float64 `json:"protein_g,omitempty"`
|
||||
FatG *float64 `json:"fat_g,omitempty"`
|
||||
CarbsG *float64 `json:"carbs_g,omitempty"`
|
||||
Source string `json:"source"`
|
||||
DishID string `json:"dish_id"`
|
||||
DishID *string `json:"dish_id,omitempty"`
|
||||
ProductID *string `json:"product_id,omitempty"`
|
||||
RecipeID *string `json:"recipe_id,omitempty"`
|
||||
PortionG *float64 `json:"portion_g,omitempty"`
|
||||
JobID *string `json:"job_id,omitempty"`
|
||||
@@ -23,13 +24,14 @@ type Entry struct {
|
||||
|
||||
// CreateRequest is the body for POST /diary.
|
||||
type CreateRequest struct {
|
||||
Date string `json:"date"`
|
||||
MealType string `json:"meal_type"`
|
||||
Name string `json:"name"` // input-only; used if DishID is nil
|
||||
Portions float64 `json:"portions"`
|
||||
Source string `json:"source"`
|
||||
DishID *string `json:"dish_id"`
|
||||
RecipeID *string `json:"recipe_id"`
|
||||
PortionG *float64 `json:"portion_g"`
|
||||
JobID *string `json:"job_id"`
|
||||
Date string `json:"date"`
|
||||
MealType string `json:"meal_type"`
|
||||
Name string `json:"name"` // input-only; used if DishID is nil and ProductID is nil
|
||||
Portions float64 `json:"portions"`
|
||||
Source string `json:"source"`
|
||||
DishID *string `json:"dish_id"`
|
||||
ProductID *string `json:"product_id"`
|
||||
RecipeID *string `json:"recipe_id"`
|
||||
PortionG *float64 `json:"portion_g"`
|
||||
JobID *string `json:"job_id"`
|
||||
}
|
||||
|
||||
@@ -82,29 +82,32 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
writeError(w, http.StatusBadRequest, "date and meal_type are required")
|
||||
return
|
||||
}
|
||||
if req.DishID == nil && req.Name == "" {
|
||||
writeError(w, http.StatusBadRequest, "dish_id or name is required")
|
||||
if req.DishID == nil && req.ProductID == nil && req.Name == "" {
|
||||
writeError(w, http.StatusBadRequest, "dish_id, product_id, or name is required")
|
||||
return
|
||||
}
|
||||
|
||||
if req.DishID == nil {
|
||||
dishID, _, resolveError := h.dishRepo.FindOrCreate(r.Context(), req.Name)
|
||||
if resolveError != nil {
|
||||
slog.Error("resolve dish for diary entry", "name", req.Name, "err", resolveError)
|
||||
writeError(w, http.StatusInternalServerError, "failed to resolve dish")
|
||||
return
|
||||
// Product-based entry: skip dish/recipe resolution entirely.
|
||||
if req.ProductID == nil {
|
||||
if req.DishID == nil {
|
||||
dishID, _, resolveError := h.dishRepo.FindOrCreate(r.Context(), req.Name)
|
||||
if resolveError != nil {
|
||||
slog.Error("resolve dish for diary entry", "name", req.Name, "err", resolveError)
|
||||
writeError(w, http.StatusInternalServerError, "failed to resolve dish")
|
||||
return
|
||||
}
|
||||
req.DishID = &dishID
|
||||
}
|
||||
req.DishID = &dishID
|
||||
}
|
||||
|
||||
if req.RecipeID == nil {
|
||||
recipeID, _, recipeError := h.recipeRepo.FindOrCreateRecipe(r.Context(), *req.DishID, 0, 0, 0, 0)
|
||||
if recipeError != nil {
|
||||
slog.Error("find or create recipe for diary entry", "dish_id", *req.DishID, "err", recipeError)
|
||||
writeError(w, http.StatusInternalServerError, "failed to resolve recipe")
|
||||
return
|
||||
if req.RecipeID == nil {
|
||||
recipeID, _, recipeError := h.recipeRepo.FindOrCreateRecipe(r.Context(), *req.DishID, 0, 0, 0, 0)
|
||||
if recipeError != nil {
|
||||
slog.Error("find or create recipe for diary entry", "dish_id", *req.DishID, "err", recipeError)
|
||||
writeError(w, http.StatusInternalServerError, "failed to resolve recipe")
|
||||
return
|
||||
}
|
||||
req.RecipeID = &recipeID
|
||||
}
|
||||
req.RecipeID = &recipeID
|
||||
}
|
||||
|
||||
entry, createError := h.repo.Create(r.Context(), userID, req)
|
||||
|
||||
@@ -24,22 +24,35 @@ func NewRepository(pool *pgxpool.Pool) *Repository {
|
||||
}
|
||||
|
||||
// ListByDate returns all diary entries for a user on a given date (YYYY-MM-DD).
|
||||
// Dish name and macros are computed via JOIN with dishes and recipes.
|
||||
// Supports both dish-based and catalog product-based entries.
|
||||
func (r *Repository) ListByDate(ctx context.Context, userID, date string) ([]*Entry, error) {
|
||||
lang := locale.FromContext(ctx)
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT
|
||||
md.id, md.date::text, md.meal_type, md.portions,
|
||||
md.source, md.dish_id::text, md.recipe_id::text, md.portion_g, md.job_id::text, md.created_at,
|
||||
COALESCE(dt.name, d.name) AS dish_name,
|
||||
r.calories_per_serving * md.portions,
|
||||
r.protein_per_serving * md.portions,
|
||||
r.fat_per_serving * md.portions,
|
||||
r.carbs_per_serving * md.portions
|
||||
md.source, md.dish_id::text, md.product_id::text, md.recipe_id::text, md.portion_g, md.job_id::text, md.created_at,
|
||||
COALESCE(dt.name, d.name, p.canonical_name) AS entry_name,
|
||||
COALESCE(
|
||||
r.calories_per_serving * md.portions,
|
||||
p.calories_per_100g * md.portion_g / 100
|
||||
),
|
||||
COALESCE(
|
||||
r.protein_per_serving * md.portions,
|
||||
p.protein_per_100g * md.portion_g / 100
|
||||
),
|
||||
COALESCE(
|
||||
r.fat_per_serving * md.portions,
|
||||
p.fat_per_100g * md.portion_g / 100
|
||||
),
|
||||
COALESCE(
|
||||
r.carbs_per_serving * md.portions,
|
||||
p.carbs_per_100g * md.portion_g / 100
|
||||
)
|
||||
FROM meal_diary md
|
||||
JOIN dishes d ON d.id = md.dish_id
|
||||
LEFT JOIN dishes d ON d.id = md.dish_id
|
||||
LEFT JOIN dish_translations dt ON dt.dish_id = d.id AND dt.lang = $3
|
||||
LEFT JOIN recipes r ON r.id = md.recipe_id
|
||||
LEFT JOIN products p ON p.id = md.product_id
|
||||
WHERE md.user_id = $1 AND md.date = $2::date
|
||||
ORDER BY md.created_at ASC`, userID, date, lang)
|
||||
if err != nil {
|
||||
@@ -72,10 +85,10 @@ func (r *Repository) Create(ctx context.Context, userID string, req CreateReques
|
||||
|
||||
var entryID string
|
||||
insertError := r.pool.QueryRow(ctx, `
|
||||
INSERT INTO meal_diary (user_id, date, meal_type, portions, source, dish_id, recipe_id, portion_g, job_id)
|
||||
VALUES ($1, $2::date, $3, $4, $5, $6, $7, $8, $9)
|
||||
INSERT INTO meal_diary (user_id, date, meal_type, portions, source, dish_id, product_id, recipe_id, portion_g, job_id)
|
||||
VALUES ($1, $2::date, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
RETURNING id`,
|
||||
userID, req.Date, req.MealType, portions, source, req.DishID, req.RecipeID, req.PortionG, req.JobID,
|
||||
userID, req.Date, req.MealType, portions, source, req.DishID, req.ProductID, req.RecipeID, req.PortionG, req.JobID,
|
||||
).Scan(&entryID)
|
||||
if insertError != nil {
|
||||
return nil, fmt.Errorf("insert diary entry: %w", insertError)
|
||||
@@ -84,16 +97,29 @@ func (r *Repository) Create(ctx context.Context, userID string, req CreateReques
|
||||
row := r.pool.QueryRow(ctx, `
|
||||
SELECT
|
||||
md.id, md.date::text, md.meal_type, md.portions,
|
||||
md.source, md.dish_id::text, md.recipe_id::text, md.portion_g, md.job_id::text, md.created_at,
|
||||
COALESCE(dt.name, d.name) AS dish_name,
|
||||
r.calories_per_serving * md.portions,
|
||||
r.protein_per_serving * md.portions,
|
||||
r.fat_per_serving * md.portions,
|
||||
r.carbs_per_serving * md.portions
|
||||
md.source, md.dish_id::text, md.product_id::text, md.recipe_id::text, md.portion_g, md.job_id::text, md.created_at,
|
||||
COALESCE(dt.name, d.name, p.canonical_name) AS entry_name,
|
||||
COALESCE(
|
||||
r.calories_per_serving * md.portions,
|
||||
p.calories_per_100g * md.portion_g / 100
|
||||
),
|
||||
COALESCE(
|
||||
r.protein_per_serving * md.portions,
|
||||
p.protein_per_100g * md.portion_g / 100
|
||||
),
|
||||
COALESCE(
|
||||
r.fat_per_serving * md.portions,
|
||||
p.fat_per_100g * md.portion_g / 100
|
||||
),
|
||||
COALESCE(
|
||||
r.carbs_per_serving * md.portions,
|
||||
p.carbs_per_100g * md.portion_g / 100
|
||||
)
|
||||
FROM meal_diary md
|
||||
JOIN dishes d ON d.id = md.dish_id
|
||||
LEFT JOIN dishes d ON d.id = md.dish_id
|
||||
LEFT JOIN dish_translations dt ON dt.dish_id = d.id AND dt.lang = $2
|
||||
LEFT JOIN recipes r ON r.id = md.recipe_id
|
||||
LEFT JOIN products p ON p.id = md.product_id
|
||||
WHERE md.id = $1`, entryID, lang)
|
||||
return scanEntry(row)
|
||||
}
|
||||
@@ -121,7 +147,7 @@ func scanEntry(s scannable) (*Entry, error) {
|
||||
var entry Entry
|
||||
scanError := s.Scan(
|
||||
&entry.ID, &entry.Date, &entry.MealType, &entry.Portions,
|
||||
&entry.Source, &entry.DishID, &entry.RecipeID, &entry.PortionG, &entry.JobID, &entry.CreatedAt,
|
||||
&entry.Source, &entry.DishID, &entry.ProductID, &entry.RecipeID, &entry.PortionG, &entry.JobID, &entry.CreatedAt,
|
||||
&entry.Name,
|
||||
&entry.Calories, &entry.ProteinG, &entry.FatG, &entry.CarbsG,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user