feat: slim meal_diary — derive name and nutrition from dish/recipe
Remove denormalized columns (name, calories, protein_g, fat_g, carbs_g) from meal_diary. Name is now resolved via JOIN with dishes/dish_translations; macros are computed as recipe.*_per_serving * portions at query time. - Add dish.Repository.FindOrCreateRecipe: finds or creates a minimal recipe stub seeded with AI-estimated macros - recognition/handler: resolve recipe_id synchronously per candidate; simplify enrichDishInBackground to translations-only - diary/handler: accept dish_id OR name; always resolve recipe_id via FindOrCreateRecipe before INSERT - diary/entity: DishID is now non-nullable string; CreateRequest drops macros - diary/repository: ListByDate and Create use JOIN to return computed macros - ai/types: add RecipeID field to DishCandidate - Update tests and wire_gen accordingly Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -17,14 +17,26 @@ type DiaryRepository interface {
|
||||
Delete(ctx context.Context, id, userID string) error
|
||||
}
|
||||
|
||||
// DishRepository is the subset of dish.Repository used by Handler to resolve dish IDs.
|
||||
type DishRepository interface {
|
||||
FindOrCreate(ctx context.Context, name string) (string, bool, error)
|
||||
}
|
||||
|
||||
// RecipeRepository is the subset of dish.Repository used by Handler to resolve recipe IDs.
|
||||
type RecipeRepository interface {
|
||||
FindOrCreateRecipe(ctx context.Context, dishID string, calories, proteinG, fatG, carbsG float64) (string, bool, error)
|
||||
}
|
||||
|
||||
// Handler handles diary endpoints.
|
||||
type Handler struct {
|
||||
repo DiaryRepository
|
||||
repo DiaryRepository
|
||||
dishRepo DishRepository
|
||||
recipeRepo RecipeRepository
|
||||
}
|
||||
|
||||
// NewHandler creates a new Handler.
|
||||
func NewHandler(repo DiaryRepository) *Handler {
|
||||
return &Handler{repo: repo}
|
||||
func NewHandler(repo DiaryRepository, dishRepo DishRepository, recipeRepo RecipeRepository) *Handler {
|
||||
return &Handler{repo: repo, dishRepo: dishRepo, recipeRepo: recipeRepo}
|
||||
}
|
||||
|
||||
// GetByDate handles GET /diary?date=YYYY-MM-DD
|
||||
@@ -41,9 +53,9 @@ func (h *Handler) GetByDate(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
entries, err := h.repo.ListByDate(r.Context(), userID, date)
|
||||
if err != nil {
|
||||
slog.Error("list diary by date", "err", err)
|
||||
entries, listError := h.repo.ListByDate(r.Context(), userID, date)
|
||||
if listError != nil {
|
||||
slog.Error("list diary by date", "err", listError)
|
||||
writeError(w, http.StatusInternalServerError, "failed to load diary")
|
||||
return
|
||||
}
|
||||
@@ -62,18 +74,42 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
var req CreateRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
if decodeError := json.NewDecoder(r.Body).Decode(&req); decodeError != nil {
|
||||
writeError(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
if req.Date == "" || req.Name == "" || req.MealType == "" {
|
||||
writeError(w, http.StatusBadRequest, "date, meal_type and name are required")
|
||||
if req.Date == "" || req.MealType == "" {
|
||||
writeError(w, http.StatusBadRequest, "date and meal_type are required")
|
||||
return
|
||||
}
|
||||
if req.DishID == nil && req.Name == "" {
|
||||
writeError(w, http.StatusBadRequest, "dish_id or name is required")
|
||||
return
|
||||
}
|
||||
|
||||
entry, err := h.repo.Create(r.Context(), userID, req)
|
||||
if err != nil {
|
||||
slog.Error("create diary entry", "err", err)
|
||||
if req.DishID == nil {
|
||||
dishID, _, resolveError := h.dishRepo.FindOrCreate(r.Context(), req.Name)
|
||||
if resolveError != nil {
|
||||
slog.Error("resolve dish for diary entry", "name", req.Name, "err", resolveError)
|
||||
writeError(w, http.StatusInternalServerError, "failed to resolve dish")
|
||||
return
|
||||
}
|
||||
req.DishID = &dishID
|
||||
}
|
||||
|
||||
if req.RecipeID == nil {
|
||||
recipeID, _, recipeError := h.recipeRepo.FindOrCreateRecipe(r.Context(), *req.DishID, 0, 0, 0, 0)
|
||||
if recipeError != nil {
|
||||
slog.Error("find or create recipe for diary entry", "dish_id", *req.DishID, "err", recipeError)
|
||||
writeError(w, http.StatusInternalServerError, "failed to resolve recipe")
|
||||
return
|
||||
}
|
||||
req.RecipeID = &recipeID
|
||||
}
|
||||
|
||||
entry, createError := h.repo.Create(r.Context(), userID, req)
|
||||
if createError != nil {
|
||||
slog.Error("create diary entry", "err", createError)
|
||||
writeError(w, http.StatusInternalServerError, "failed to create diary entry")
|
||||
return
|
||||
}
|
||||
@@ -89,12 +125,12 @@ func (h *Handler) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
id := chi.URLParam(r, "id")
|
||||
if err := h.repo.Delete(r.Context(), id, userID); err != nil {
|
||||
if err == ErrNotFound {
|
||||
if deleteError := h.repo.Delete(r.Context(), id, userID); deleteError != nil {
|
||||
if deleteError == ErrNotFound {
|
||||
writeError(w, http.StatusNotFound, "diary entry not found")
|
||||
return
|
||||
}
|
||||
slog.Error("delete diary entry", "err", err)
|
||||
slog.Error("delete diary entry", "err", deleteError)
|
||||
writeError(w, http.StatusInternalServerError, "failed to delete diary entry")
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user