refactor: introduce internal/domain/ layer, rename model.go → entity.go
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>
This commit is contained in:
67
backend/internal/domain/dish/handler.go
Normal file
67
backend/internal/domain/dish/handler.go
Normal file
@@ -0,0 +1,67 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user