- Add units + unit_translations tables with FK constraints on products and ingredient_mappings - Normalize products.unit from Russian strings (г, кг) to English codes (g, kg) - Load units at startup (in-memory registry) and serve via GET /units (language-aware) - Replace hardcoded _units lists and _mapUnit() functions in Flutter with unitsProvider FutureProvider - Re-fetches automatically when language changes Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
29 lines
707 B
Go
29 lines
707 B
Go
package units
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/food-ai/backend/internal/locale"
|
|
)
|
|
|
|
type unitItem struct {
|
|
Code string `json:"code"`
|
|
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
|
|
}
|
|
items = append(items, unitItem{Code: u.Code, Name: name})
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
_ = json.NewEncoder(w).Encode(map[string]any{"units": items})
|
|
}
|