Move all business-logic packages from internal/ root into internal/domain/: auth, cuisine, diary, dish, home, ingredient, language, menu, product, recipe, recognition, recommendation, savedrecipe, tag, units, user Rename model.go → entity.go in packages that hold domain entities: diary, dish, home, ingredient, menu, product, recipe, savedrecipe, user Update all import paths accordingly (adapters, infra/server, cmd/server, tests). No logic changes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
52 lines
1.1 KiB
Go
52 lines
1.1 KiB
Go
package ingredient
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
// Handler handles ingredient HTTP requests.
|
|
type Handler struct {
|
|
repo *Repository
|
|
}
|
|
|
|
// NewHandler creates a new Handler.
|
|
func NewHandler(repo *Repository) *Handler {
|
|
return &Handler{repo: repo}
|
|
}
|
|
|
|
// Search handles GET /ingredients/search?q=&limit=10.
|
|
func (h *Handler) Search(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query().Get("q")
|
|
if q == "" {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_, _ = w.Write([]byte("[]"))
|
|
return
|
|
}
|
|
|
|
limit := 10
|
|
if s := r.URL.Query().Get("limit"); s != "" {
|
|
if n, err := strconv.Atoi(s); err == nil && n > 0 && n <= 50 {
|
|
limit = n
|
|
}
|
|
}
|
|
|
|
mappings, err := h.repo.Search(r.Context(), q, limit)
|
|
if err != nil {
|
|
slog.Error("search ingredients", "q", q, "err", err)
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
_, _ = w.Write([]byte(`{"error":"search failed"}`))
|
|
return
|
|
}
|
|
|
|
if mappings == nil {
|
|
mappings = []*IngredientMapping{}
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(mappings)
|
|
}
|