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:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user