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,

View File

@@ -59,6 +59,14 @@ type RecipeStep struct {
ImageURL *string `json:"image_url"`
}
// DishSearchResult is a lightweight dish returned by the search endpoint.
type DishSearchResult struct {
ID string `json:"id"`
Name string `json:"name"`
ImageURL *string `json:"image_url,omitempty"`
AvgRating float64 `json:"avg_rating"`
}
// CreateRequest is the body used to create a new dish + recipe at once.
// Used when saving a Gemini-generated recommendation.
type CreateRequest struct {

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"log/slog"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
)
@@ -18,6 +19,31 @@ func NewHandler(repo *Repository) *Handler {
return &Handler{repo: repo}
}
// Search handles GET /dishes/search?q=<query>&limit=<n>
func (h *Handler) Search(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query().Get("q")
if query == "" {
writeJSON(w, http.StatusOK, []*DishSearchResult{})
return
}
limit := 10
if limitStr := r.URL.Query().Get("limit"); limitStr != "" {
if parsed, parseError := strconv.Atoi(limitStr); parseError == nil && parsed > 0 && parsed <= 50 {
limit = parsed
}
}
results, searchError := h.repo.Search(r.Context(), query, limit)
if searchError != nil {
slog.Error("search dishes", "err", searchError)
writeError(w, http.StatusInternalServerError, "failed to search dishes")
return
}
if results == nil {
results = []*DishSearchResult{}
}
writeJSON(w, http.StatusOK, results)
}
// List handles GET /dishes — returns all dishes (no recipe variants).
func (h *Handler) List(w http.ResponseWriter, r *http.Request) {
dishes, err := h.repo.List(r.Context())

View File

@@ -98,6 +98,57 @@ func (r *Repository) List(ctx context.Context) ([]*Dish, error) {
return dishes, nil
}
// Search finds dishes matching the query string using FTS + trigram similarity.
// Text is resolved for the language in ctx (English fallback).
func (r *Repository) Search(ctx context.Context, query string, limit int) ([]*DishSearchResult, error) {
if limit <= 0 || limit > 50 {
limit = 10
}
lang := locale.FromContext(ctx)
const searchQuery = `
SELECT d.id,
COALESCE(dt.name, d.name) AS name,
d.image_url,
d.avg_rating,
GREATEST(
ts_rank(to_tsvector('english', d.name), plainto_tsquery('english', $1)),
ts_rank(to_tsvector('simple', COALESCE(dt.name, '')), plainto_tsquery('simple', $1)),
similarity(COALESCE(dt.name, d.name), $1)
) AS rank
FROM dishes d
LEFT JOIN dish_translations dt ON dt.dish_id = d.id AND dt.lang = $3
WHERE (
to_tsvector('english', d.name) @@ plainto_tsquery('english', $1)
OR to_tsvector('simple', COALESCE(dt.name, '')) @@ plainto_tsquery('simple', $1)
OR d.name ILIKE '%' || $1 || '%'
OR dt.name ILIKE '%' || $1 || '%'
OR similarity(COALESCE(dt.name, d.name), $1) > 0.3
)
ORDER BY rank DESC
LIMIT $2`
rows, queryError := r.pool.Query(ctx, searchQuery, query, limit, lang)
if queryError != nil {
return nil, fmt.Errorf("search dishes: %w", queryError)
}
defer rows.Close()
var results []*DishSearchResult
for rows.Next() {
var result DishSearchResult
var rank float64
if scanError := rows.Scan(&result.ID, &result.Name, &result.ImageURL, &result.AvgRating, &rank); scanError != nil {
return nil, fmt.Errorf("scan dish search result: %w", scanError)
}
results = append(results, &result)
}
if err := rows.Err(); err != nil {
return nil, err
}
return results, nil
}
// FindOrCreate returns the dish ID and whether it was newly created.
// Looks up by case-insensitive name match; creates a minimal dish row if not found.
func (r *Repository) FindOrCreate(ctx context.Context, name string) (id string, created bool, err error) {

View File

@@ -202,15 +202,33 @@ func (r *Repository) Search(requestContext context.Context, query string, limit
FROM product_aliases pa
WHERE pa.product_id = p.id AND pa.lang = $3
) al ON true
WHERE EXISTS (
SELECT 1 FROM product_aliases pa
WHERE pa.product_id = p.id
AND (pa.lang = $3 OR pa.lang = 'en')
AND pa.alias ILIKE '%' || $1 || '%'
WHERE (
to_tsvector('english', p.canonical_name) @@ plainto_tsquery('english', $1)
OR EXISTS (
SELECT 1 FROM product_aliases pa
WHERE pa.product_id = p.id
AND (pa.lang = $3 OR pa.lang = 'en')
AND to_tsvector('simple', pa.alias) @@ plainto_tsquery('simple', $1)
)
OR p.canonical_name ILIKE '%' || $1 || '%'
OR EXISTS (
SELECT 1 FROM product_aliases pa
WHERE pa.product_id = p.id
AND (pa.lang = $3 OR pa.lang = 'en')
AND pa.alias ILIKE '%' || $1 || '%'
)
OR similarity(p.canonical_name, $1) > 0.3
)
OR p.canonical_name ILIKE '%' || $1 || '%'
OR similarity(p.canonical_name, $1) > 0.3
ORDER BY similarity(p.canonical_name, $1) DESC
ORDER BY
GREATEST(
ts_rank(to_tsvector('english', p.canonical_name), plainto_tsquery('english', $1)),
COALESCE((
SELECT MAX(ts_rank(to_tsvector('simple', pa.alias), plainto_tsquery('simple', $1)))
FROM product_aliases pa
WHERE pa.product_id = p.id AND (pa.lang = $3 OR pa.lang = 'en')
), 0),
similarity(p.canonical_name, $1)
) DESC
LIMIT $2`
rows, queryError := r.pool.Query(requestContext, searchQuery, query, limit, lang)

View File

@@ -92,6 +92,7 @@ func NewRouter(
r.Route("/dishes", func(r chi.Router) {
r.Get("/", dishHandler.List)
r.Get("/search", dishHandler.Search)
r.Get("/{id}", dishHandler.GetByID)
})
@@ -111,6 +112,7 @@ func NewRouter(
r.Route("/diary", func(r chi.Router) {
r.Get("/", diaryHandler.GetByDate)
r.Get("/recent", diaryHandler.GetRecent)
r.Post("/", diaryHandler.Create)
r.Delete("/{id}", diaryHandler.Delete)
})