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:
60
backend/internal/domain/units/handler.go
Normal file
60
backend/internal/domain/units/handler.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package units
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/food-ai/backend/internal/infra/locale"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type unitItem struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// NewListHandler returns an http.HandlerFunc for GET /units.
|
||||
// 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 u.code, COALESCE(ut.name, u.code) AS name
|
||||
FROM units u
|
||||
LEFT JOIN unit_translations ut ON ut.unit_code = u.code AND ut.lang = $1
|
||||
ORDER BY u.sort_order`, lang)
|
||||
if queryError != nil {
|
||||
slog.Error("list units", "err", queryError)
|
||||
writeErrorJSON(responseWriter, http.StatusInternalServerError, "failed to load units")
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
items := make([]unitItem, 0)
|
||||
for rows.Next() {
|
||||
var item unitItem
|
||||
if scanError := rows.Scan(&item.Code, &item.Name); scanError != nil {
|
||||
slog.Error("scan unit row", "err", scanError)
|
||||
writeErrorJSON(responseWriter, http.StatusInternalServerError, "failed to load units")
|
||||
return
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if rowsError := rows.Err(); rowsError != nil {
|
||||
slog.Error("iterate unit rows", "err", rowsError)
|
||||
writeErrorJSON(responseWriter, http.StatusInternalServerError, "failed to load units")
|
||||
return
|
||||
}
|
||||
|
||||
responseWriter.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(responseWriter).Encode(map[string]any{"units": items})
|
||||
}
|
||||
}
|
||||
|
||||
func writeErrorJSON(responseWriter http.ResponseWriter, status int, message string) {
|
||||
responseWriter.Header().Set("Content-Type", "application/json")
|
||||
responseWriter.WriteHeader(status)
|
||||
_ = json.NewEncoder(responseWriter).Encode(map[string]string{"error": message})
|
||||
}
|
||||
Reference in New Issue
Block a user