refactor: migrate DI to Wire, replace startup registries with on-demand DB queries

- Add google/wire; generate wire_gen.go from wire.go injector
- Move all constructor wiring out of main.go into providers.go / wire.go
- Export recognition.IngredientRepository (was unexported, blocked Wire binding)
- Delete units/cuisine/tag registry.go files (global state + unused NameFor helpers)
- Replace List handlers with NewListHandler(pool) using COALESCE SQL queries
- Remove units/cuisine/tag LoadFromDB from server startup; locale.LoadFromDB kept
  (locale.Supported is used by language middleware on every request)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
dbastrikin
2026-03-15 18:45:21 +02:00
parent 61feb91bba
commit 0ce111fa08
14 changed files with 552 additions and 423 deletions

View File

@@ -2,9 +2,11 @@ package units
import (
"encoding/json"
"log/slog"
"net/http"
"github.com/food-ai/backend/internal/locale"
"github.com/jackc/pgx/v5/pgxpool"
)
type unitItem struct {
@@ -12,17 +14,47 @@ type unitItem struct {
Name string `json:"name"`
}
// List handles GET /units — returns units with names in the requested language.
func List(w http.ResponseWriter, r *http.Request) {
lang := locale.FromContext(r.Context())
items := make([]unitItem, 0, len(Records))
for _, u := range Records {
name, ok := u.Translations[lang]
if !ok {
name = u.Code // fallback to English code
// 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
}
items = append(items, unitItem{Code: u.Code, Name: name})
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})
}
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).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})
}