Replaces the flat JSONB-based recipe schema with a normalized relational model:
Schema (migrations consolidated to 001_initial_schema + 002_seed_data):
- New: dishes, dish_translations, dish_tags — canonical dish catalog
- New: cuisines, tags, dish_categories with _translations tables + full seed data
- New: recipe_ingredients, recipe_steps with _translations (replaces JSONB blobs)
- New: user_saved_recipes thin bookmark (drops saved_recipes + saved_recipe_translations)
- New: product_ingredients M2M table
- recipes: now a cooking variant of a dish (dish_id FK, no title/JSONB columns)
- recipe_translations: repurposed to per-language notes only
- products: mapping_id → primary_ingredient_id
- menu_items: recipe_id FK → recipes; adds dish_id
- meal_diary: adds dish_id, recipe_id → recipes, portion_g
Backend (Go):
- New packages: internal/cuisine, internal/tag, internal/dish (registry + handler + repo)
- New GET /cuisines, GET /tags (public), GET /dishes, GET /dishes/{id}, GET /recipes/{id}
- recipe, savedrecipe, menu, diary, product, ingredient packages updated for new schema
Flutter:
- New models: Cuisine, Tag; new providers: cuisineNamesProvider, tagNamesProvider
- recipe.dart: RecipeIngredient gains unit_code + effectiveUnit getter
- saved_recipe.dart: thin model, manual fromJson, computed nutrition getter
- diary_entry.dart: adds dishId, recipeId, portionG
- recipe_detail_screen.dart: localized cuisine/tag names via providers
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
68 lines
1.6 KiB
Go
68 lines
1.6 KiB
Go
package dish
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"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}
|
|
}
|
|
|
|
// 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)
|
|
}
|