feat: food search sheet with FTS+trgm, dish/recent endpoints, multilingual aliases

Backend:
- GET /dishes/search — hybrid FTS (english + simple) + trgm + ILIKE search
- GET /diary/recent — recently used dishes and products for the current user
- product search upgraded: FTS on canonical_name and product_aliases, ranked by GREATEST(ts_rank, similarity)
- importoff: collect product_name_ru/de/fr/... as product_aliases for multilingual search (e.g. "сникерс" → "Snickers")
- migrations: FTS + trgm indexes merged into 001_initial_schema.sql (002 removed)

Flutter:
- FoodSearchSheet: debounced search field, recently-used section, product/dish results, scan-photo and barcode chips
- DishPortionSheet: quick ½/1/1½/2 buttons + custom input
- + button in meal card now opens FoodSearchSheet instead of going directly to AI scan
- 7 new l10n keys across all 12 languages

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-21 15:28:29 +02:00
parent 81185bb7ff
commit 78f1c8bf76
41 changed files with 1688 additions and 28 deletions

View File

@@ -22,6 +22,18 @@ type Entry struct {
CreatedAt time.Time `json:"created_at"`
}
// RecentDiaryItem is a lightweight summary of a recently logged product or dish.
type RecentDiaryItem struct {
ItemType string `json:"item_type"` // "dish" | "product"
DishID *string `json:"dish_id,omitempty"`
ProductID *string `json:"product_id,omitempty"`
Name string `json:"name"`
ImageURL *string `json:"image_url,omitempty"`
CategoryName *string `json:"category_name,omitempty"`
CaloriesPer100g *float64 `json:"calories_per_100g,omitempty"`
CaloriesPerServing *float64 `json:"calories_per_serving,omitempty"`
}
// CreateRequest is the body for POST /diary.
type CreateRequest struct {
Date string `json:"date"`

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"log/slog"
"net/http"
"strconv"
"github.com/food-ai/backend/internal/infra/middleware"
"github.com/go-chi/chi/v5"
@@ -15,6 +16,7 @@ type DiaryRepository interface {
ListByDate(ctx context.Context, userID, date string) ([]*Entry, error)
Create(ctx context.Context, userID string, req CreateRequest) (*Entry, error)
Delete(ctx context.Context, id, userID string) error
GetRecent(ctx context.Context, userID string, limit int) ([]*RecentDiaryItem, error)
}
// DishRepository is the subset of dish.Repository used by Handler to resolve dish IDs.
@@ -119,6 +121,31 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusCreated, entry)
}
// GetRecent handles GET /diary/recent?limit=<n>
func (h *Handler) GetRecent(w http.ResponseWriter, r *http.Request) {
userID := middleware.UserIDFromCtx(r.Context())
if userID == "" {
writeError(w, http.StatusUnauthorized, "unauthorized")
return
}
limit := 10
if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
if parsed, parseError := strconv.Atoi(limitStr); parseError == nil && parsed > 0 && parsed <= 20 {
limit = parsed
}
}
items, queryError := h.repo.GetRecent(r.Context(), userID, limit)
if queryError != nil {
slog.Error("get recent diary items", "err", queryError)
writeError(w, http.StatusInternalServerError, "failed to get recent items")
return
}
if items == nil {
items = []*RecentDiaryItem{}
}
writeJSON(w, http.StatusOK, items)
}
// Delete handles DELETE /diary/{id}
func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
userID := middleware.UserIDFromCtx(r.Context())

View File

@@ -35,6 +35,7 @@ type MockDiaryRepository struct {
ListByDateFn func(ctx context.Context, userID, date string) ([]*diary.Entry, error)
CreateFn func(ctx context.Context, userID string, req diary.CreateRequest) (*diary.Entry, error)
DeleteFn func(ctx context.Context, id, userID string) error
GetRecentFn func(ctx context.Context, userID string, limit int) ([]*diary.RecentDiaryItem, error)
}
func (m *MockDiaryRepository) ListByDate(ctx context.Context, userID, date string) ([]*diary.Entry, error) {
@@ -48,3 +49,10 @@ func (m *MockDiaryRepository) Create(ctx context.Context, userID string, req dia
func (m *MockDiaryRepository) Delete(ctx context.Context, id, userID string) error {
return m.DeleteFn(ctx, id, userID)
}
func (m *MockDiaryRepository) GetRecent(ctx context.Context, userID string, limit int) ([]*diary.RecentDiaryItem, error) {
if m.GetRecentFn != nil {
return m.GetRecentFn(ctx, userID, limit)
}
return []*diary.RecentDiaryItem{}, nil
}

View File

@@ -124,6 +124,78 @@ func (r *Repository) Create(ctx context.Context, userID string, req CreateReques
return scanEntry(row)
}
// GetRecent returns the most recently logged distinct dishes and products for a user.
func (r *Repository) GetRecent(ctx context.Context, userID string, limit int) ([]*RecentDiaryItem, error) {
if limit <= 0 || limit > 20 {
limit = 10
}
lang := locale.FromContext(ctx)
const q = `
WITH recent AS (
SELECT
dish_id, product_id,
MAX(created_at) AS last_used
FROM meal_diary
WHERE user_id = $1
GROUP BY dish_id, product_id
ORDER BY last_used DESC
LIMIT $2
)
SELECT
r.dish_id::text,
r.product_id::text,
COALESCE(dt.name, d.name, p.canonical_name) AS name,
d.image_url,
COALESCE(pct.name, pc.name) AS category_name,
p.calories_per_100g,
(SELECT MIN(rec.calories_per_serving)
FROM recipes rec WHERE rec.dish_id = r.dish_id) AS calories_per_serving
FROM recent r
LEFT JOIN dishes d
ON d.id = r.dish_id
LEFT JOIN dish_translations dt
ON dt.dish_id = d.id AND dt.lang = $3
LEFT JOIN products p
ON p.id = r.product_id
LEFT JOIN product_categories pc
ON pc.slug = p.category
LEFT JOIN product_category_translations pct
ON pct.product_category_slug = p.category AND pct.lang = $3
ORDER BY r.last_used DESC`
rows, queryError := r.pool.Query(ctx, q, userID, limit, lang)
if queryError != nil {
return nil, fmt.Errorf("get recent diary items: %w", queryError)
}
defer rows.Close()
var results []*RecentDiaryItem
for rows.Next() {
var item RecentDiaryItem
var dishID, productID *string
if scanError := rows.Scan(
&dishID, &productID,
&item.Name, &item.ImageURL, &item.CategoryName,
&item.CaloriesPer100g, &item.CaloriesPerServing,
); scanError != nil {
return nil, fmt.Errorf("scan recent diary item: %w", scanError)
}
item.DishID = dishID
item.ProductID = productID
if dishID != nil {
item.ItemType = "dish"
} else {
item.ItemType = "product"
}
results = append(results, &item)
}
if err := rows.Err(); err != nil {
return nil, err
}
return results, nil
}
// Delete removes a diary entry for the given user.
func (r *Repository) Delete(ctx context.Context, id, userID string) error {
tag, deleteError := r.pool.Exec(ctx,