Files
food-ai/backend/internal/domain/dish/handler.go
dbastrikin 78f1c8bf76 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>
2026-03-21 15:28:29 +02:00

94 lines
2.4 KiB
Go

package dish
import (
"encoding/json"
"log/slog"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
)
// Handler handles HTTP requests for dishes.
type Handler struct {
repo *Repository
}
// NewHandler creates a new Handler.
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())
if err != nil {
slog.Error("list dishes", "err", err)
writeError(w, http.StatusInternalServerError, "failed to list dishes")
return
}
if dishes == nil {
dishes = []*Dish{}
}
writeJSON(w, http.StatusOK, map[string]any{"dishes": dishes})
}
// GetByID handles GET /dishes/{id} — returns a dish with all recipe variants.
func (h *Handler) GetByID(w http.ResponseWriter, r *http.Request) {
id := chi.URLParam(r, "id")
dish, err := h.repo.GetByID(r.Context(), id)
if err != nil {
slog.Error("get dish", "id", id, "err", err)
writeError(w, http.StatusInternalServerError, "failed to get dish")
return
}
if dish == nil {
writeError(w, http.StatusNotFound, "dish not found")
return
}
writeJSON(w, http.StatusOK, dish)
}
// --- helpers ---
type errorResponse struct {
Error string `json:"error"`
}
func writeError(w http.ResponseWriter, status int, msg string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(errorResponse{Error: msg})
}
func writeJSON(w http.ResponseWriter, status int, v any) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(v)
}