Backend fixes: - migration 003: add 'menu' value to recipe_source enum (was causing SQLSTATE 22P02) - migration 004: rename recipe_products→recipe_ingredients, product_id→ingredient_id (was causing SQLSTATE 42P01) - dish/repository.go: fix INSERT INTO tags using $1/$1 for two columns → $1/$2 (was causing SQLSTATE 42P08) - home/handler.go: replace non-existent saved_recipes table with correct joins (recipes→dishes→dish_translations, user_saved_recipes) so today's plan and recommendations load correctly - reqlog: new slog.Handler wrapper that adds request_id and stack trace to ERROR-level logs - all handlers: slog.Error→slog.ErrorContext so error logs include request context; writeError includes request_id in response body Client: - home_screen.dart: extend home screen to future dates, show planned meals as ghost entries - l10n: add new localisation keys for home screen date navigation and planned meal UI Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
62 lines
2.2 KiB
Go
62 lines
2.2 KiB
Go
package cuisine
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"github.com/food-ai/backend/internal/infra/locale"
|
|
"github.com/food-ai/backend/internal/infra/middleware"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type cuisineItem struct {
|
|
Slug string `json:"slug"`
|
|
Name string `json:"name"`
|
|
}
|
|
|
|
// NewListHandler returns an http.HandlerFunc for GET /cuisines.
|
|
// It queries the database directly, resolving translations via COALESCE.
|
|
func NewListHandler(pool *pgxpool.Pool) http.HandlerFunc {
|
|
return func(responseWriter http.ResponseWriter, request *http.Request) {
|
|
lang := locale.FromContext(request.Context())
|
|
|
|
rows, queryError := pool.Query(request.Context(), `
|
|
SELECT c.slug, COALESCE(ct.name, c.name) AS name
|
|
FROM cuisines c
|
|
LEFT JOIN cuisine_translations ct ON ct.cuisine_slug = c.slug AND ct.lang = $1
|
|
ORDER BY c.sort_order`, lang)
|
|
if queryError != nil {
|
|
slog.ErrorContext(request.Context(), "list cuisines", "err", queryError)
|
|
writeErrorJSON(responseWriter, request, http.StatusInternalServerError, "failed to load cuisines")
|
|
return
|
|
}
|
|
defer rows.Close()
|
|
|
|
items := make([]cuisineItem, 0)
|
|
for rows.Next() {
|
|
var item cuisineItem
|
|
if scanError := rows.Scan(&item.Slug, &item.Name); scanError != nil {
|
|
slog.ErrorContext(request.Context(), "scan cuisine row", "err", scanError)
|
|
writeErrorJSON(responseWriter, request, http.StatusInternalServerError, "failed to load cuisines")
|
|
return
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
if rowsError := rows.Err(); rowsError != nil {
|
|
slog.ErrorContext(request.Context(), "iterate cuisine rows", "err", rowsError)
|
|
writeErrorJSON(responseWriter, request, http.StatusInternalServerError, "failed to load cuisines")
|
|
return
|
|
}
|
|
|
|
responseWriter.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(responseWriter).Encode(map[string]any{"cuisines": items})
|
|
}
|
|
}
|
|
|
|
func writeErrorJSON(responseWriter http.ResponseWriter, request *http.Request, status int, msg string) {
|
|
responseWriter.Header().Set("Content-Type", "application/json")
|
|
responseWriter.WriteHeader(status)
|
|
_ = json.NewEncoder(responseWriter).Encode(map[string]any{"error": msg, "request_id": middleware.RequestIDFromCtx(request.Context())})
|
|
}
|