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:
71
backend/internal/domain/user/calories.go
Normal file
71
backend/internal/domain/user/calories.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Activity level multipliers (Mifflin-St Jeor).
|
||||
var activityMultiplier = map[string]float64{
|
||||
"low": 1.375,
|
||||
"moderate": 1.55,
|
||||
"high": 1.725,
|
||||
}
|
||||
|
||||
// Goal adjustments in kcal.
|
||||
var goalAdjustment = map[string]float64{
|
||||
"lose": -500,
|
||||
"maintain": 0,
|
||||
"gain": 300,
|
||||
}
|
||||
|
||||
// AgeFromDOB computes age in years from a "YYYY-MM-DD" string. Returns nil on error.
|
||||
func AgeFromDOB(dob *string) *int {
|
||||
if dob == nil {
|
||||
return nil
|
||||
}
|
||||
t, err := time.Parse("2006-01-02", *dob)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
now := time.Now()
|
||||
years := now.Year() - t.Year()
|
||||
if now.Month() < t.Month() || (now.Month() == t.Month() && now.Day() < t.Day()) {
|
||||
years--
|
||||
}
|
||||
return &years
|
||||
}
|
||||
|
||||
// CalculateDailyCalories computes the daily calorie target using the
|
||||
// Mifflin-St Jeor equation. Returns nil if any required parameter is missing.
|
||||
func CalculateDailyCalories(heightCM *int, weightKG *float64, age *int, gender, activity, goal *string) *int {
|
||||
if heightCM == nil || weightKG == nil || age == nil || gender == nil || activity == nil || goal == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// BMR = 10 * weight(kg) + 6.25 * height(cm) - 5 * age
|
||||
bmr := 10**weightKG + 6.25*float64(*heightCM) - 5*float64(*age)
|
||||
|
||||
switch *gender {
|
||||
case "male":
|
||||
bmr += 5
|
||||
case "female":
|
||||
bmr -= 161
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
|
||||
mult, ok := activityMultiplier[*activity]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
adj, ok := goalAdjustment[*goal]
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
|
||||
tdee := bmr * mult
|
||||
result := int(math.Round(tdee + adj))
|
||||
return &result
|
||||
}
|
||||
44
backend/internal/domain/user/entity.go
Normal file
44
backend/internal/domain/user/entity.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
FirebaseUID string `json:"-"`
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
AvatarURL *string `json:"avatar_url"`
|
||||
HeightCM *int `json:"height_cm"`
|
||||
WeightKG *float64 `json:"weight_kg"`
|
||||
DateOfBirth *string `json:"date_of_birth"`
|
||||
Gender *string `json:"gender"`
|
||||
Activity *string `json:"activity"`
|
||||
Goal *string `json:"goal"`
|
||||
DailyCalories *int `json:"daily_calories"`
|
||||
Plan string `json:"plan"`
|
||||
Preferences json.RawMessage `json:"preferences"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type UpdateProfileRequest struct {
|
||||
Name *string `json:"name"`
|
||||
HeightCM *int `json:"height_cm"`
|
||||
WeightKG *float64 `json:"weight_kg"`
|
||||
DateOfBirth *string `json:"date_of_birth"`
|
||||
Gender *string `json:"gender"`
|
||||
Activity *string `json:"activity"`
|
||||
Goal *string `json:"goal"`
|
||||
Preferences *json.RawMessage `json:"preferences"`
|
||||
DailyCalories *int `json:"-"` // internal, set by service
|
||||
}
|
||||
|
||||
// HasBodyParams returns true if any body parameter is being updated
|
||||
// that would require recalculation of daily calories.
|
||||
func (r *UpdateProfileRequest) HasBodyParams() bool {
|
||||
return r.HeightCM != nil || r.WeightKG != nil || r.DateOfBirth != nil ||
|
||||
r.Gender != nil || r.Activity != nil || r.Goal != nil
|
||||
}
|
||||
78
backend/internal/domain/user/handler.go
Normal file
78
backend/internal/domain/user/handler.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/food-ai/backend/internal/infra/middleware"
|
||||
)
|
||||
|
||||
const maxRequestBodySize = 1 << 20 // 1 MB
|
||||
|
||||
type Handler struct {
|
||||
service *Service
|
||||
}
|
||||
|
||||
func NewHandler(service *Service) *Handler {
|
||||
return &Handler{service: service}
|
||||
}
|
||||
|
||||
func (h *Handler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.UserIDFromCtx(r.Context())
|
||||
if userID == "" {
|
||||
writeErrorJSON(w, http.StatusUnauthorized, "unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
u, err := h.service.GetProfile(r.Context(), userID)
|
||||
if err != nil {
|
||||
writeErrorJSON(w, http.StatusNotFound, "user not found")
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, u)
|
||||
}
|
||||
|
||||
func (h *Handler) Update(w http.ResponseWriter, r *http.Request) {
|
||||
userID := middleware.UserIDFromCtx(r.Context())
|
||||
if userID == "" {
|
||||
writeErrorJSON(w, http.StatusUnauthorized, "unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBodySize)
|
||||
var req UpdateProfileRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
writeErrorJSON(w, http.StatusBadRequest, "invalid request body")
|
||||
return
|
||||
}
|
||||
|
||||
u, err := h.service.UpdateProfile(r.Context(), userID, req)
|
||||
if err != nil {
|
||||
writeErrorJSON(w, http.StatusBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, u)
|
||||
}
|
||||
|
||||
type errorResponse struct {
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
func writeErrorJSON(w http.ResponseWriter, status int, msg string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
if err := json.NewEncoder(w).Encode(errorResponse{Error: msg}); err != nil {
|
||||
slog.Error("failed to write error response", "err", err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, v interface{}) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
if err := json.NewEncoder(w).Encode(v); err != nil {
|
||||
slog.Error("failed to write JSON response", "err", err)
|
||||
}
|
||||
}
|
||||
46
backend/internal/domain/user/mocks/repository.go
Normal file
46
backend/internal/domain/user/mocks/repository.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package mocks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/food-ai/backend/internal/domain/user"
|
||||
)
|
||||
|
||||
type MockUserRepository struct {
|
||||
UpsertByFirebaseUIDFn func(ctx context.Context, uid, email, name, avatarURL string) (*user.User, error)
|
||||
GetByIDFn func(ctx context.Context, id string) (*user.User, error)
|
||||
UpdateFn func(ctx context.Context, id string, req user.UpdateProfileRequest) (*user.User, error)
|
||||
UpdateInTxFn func(ctx context.Context, id string, profileReq user.UpdateProfileRequest, caloriesReq *user.UpdateProfileRequest) (*user.User, error)
|
||||
SetRefreshTokenFn func(ctx context.Context, id, token string, expiresAt time.Time) error
|
||||
FindByRefreshTokenFn func(ctx context.Context, token string) (*user.User, error)
|
||||
ClearRefreshTokenFn func(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
func (m *MockUserRepository) UpsertByFirebaseUID(ctx context.Context, uid, email, name, avatarURL string) (*user.User, error) {
|
||||
return m.UpsertByFirebaseUIDFn(ctx, uid, email, name, avatarURL)
|
||||
}
|
||||
|
||||
func (m *MockUserRepository) GetByID(ctx context.Context, id string) (*user.User, error) {
|
||||
return m.GetByIDFn(ctx, id)
|
||||
}
|
||||
|
||||
func (m *MockUserRepository) Update(ctx context.Context, id string, req user.UpdateProfileRequest) (*user.User, error) {
|
||||
return m.UpdateFn(ctx, id, req)
|
||||
}
|
||||
|
||||
func (m *MockUserRepository) UpdateInTx(ctx context.Context, id string, profileReq user.UpdateProfileRequest, caloriesReq *user.UpdateProfileRequest) (*user.User, error) {
|
||||
return m.UpdateInTxFn(ctx, id, profileReq, caloriesReq)
|
||||
}
|
||||
|
||||
func (m *MockUserRepository) SetRefreshToken(ctx context.Context, id, token string, expiresAt time.Time) error {
|
||||
return m.SetRefreshTokenFn(ctx, id, token, expiresAt)
|
||||
}
|
||||
|
||||
func (m *MockUserRepository) FindByRefreshToken(ctx context.Context, token string) (*user.User, error) {
|
||||
return m.FindByRefreshTokenFn(ctx, token)
|
||||
}
|
||||
|
||||
func (m *MockUserRepository) ClearRefreshToken(ctx context.Context, id string) error {
|
||||
return m.ClearRefreshTokenFn(ctx, id)
|
||||
}
|
||||
223
backend/internal/domain/user/repository.go
Normal file
223
backend/internal/domain/user/repository.go
Normal file
@@ -0,0 +1,223 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// UserRepository defines the persistence interface for user data.
|
||||
type UserRepository interface {
|
||||
UpsertByFirebaseUID(ctx context.Context, uid, email, name, avatarURL string) (*User, error)
|
||||
GetByID(ctx context.Context, id string) (*User, error)
|
||||
Update(ctx context.Context, id string, req UpdateProfileRequest) (*User, error)
|
||||
UpdateInTx(ctx context.Context, id string, profileReq UpdateProfileRequest, caloriesReq *UpdateProfileRequest) (*User, error)
|
||||
SetRefreshToken(ctx context.Context, id, token string, expiresAt time.Time) error
|
||||
FindByRefreshToken(ctx context.Context, token string) (*User, error)
|
||||
ClearRefreshToken(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
type Repository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewRepository(pool *pgxpool.Pool) *Repository {
|
||||
return &Repository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *Repository) UpsertByFirebaseUID(ctx context.Context, uid, email, name, avatarURL string) (*User, error) {
|
||||
var avatarPtr *string
|
||||
if avatarURL != "" {
|
||||
avatarPtr = &avatarURL
|
||||
}
|
||||
|
||||
query := `
|
||||
INSERT INTO users (firebase_uid, email, name, avatar_url)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (firebase_uid) DO UPDATE SET
|
||||
email = EXCLUDED.email,
|
||||
name = CASE WHEN users.name = '' THEN EXCLUDED.name ELSE users.name END,
|
||||
avatar_url = COALESCE(EXCLUDED.avatar_url, users.avatar_url),
|
||||
updated_at = now()
|
||||
RETURNING id, firebase_uid, email, name, avatar_url,
|
||||
height_cm, weight_kg, date_of_birth, gender, activity, goal, daily_calories,
|
||||
plan, preferences, created_at, updated_at`
|
||||
|
||||
return r.scanUser(r.pool.QueryRow(ctx, query, uid, email, name, avatarPtr))
|
||||
}
|
||||
|
||||
func (r *Repository) GetByID(ctx context.Context, id string) (*User, error) {
|
||||
query := `
|
||||
SELECT id, firebase_uid, email, name, avatar_url,
|
||||
height_cm, weight_kg, date_of_birth, gender, activity, goal, daily_calories,
|
||||
plan, preferences, created_at, updated_at
|
||||
FROM users WHERE id = $1`
|
||||
|
||||
return r.scanUser(r.pool.QueryRow(ctx, query, id))
|
||||
}
|
||||
|
||||
func (r *Repository) Update(ctx context.Context, id string, req UpdateProfileRequest) (*User, error) {
|
||||
query, args := buildUpdateQuery(id, req)
|
||||
if query == "" {
|
||||
return r.GetByID(ctx, id)
|
||||
}
|
||||
return r.scanUser(r.pool.QueryRow(ctx, query, args...))
|
||||
}
|
||||
|
||||
// buildUpdateQuery constructs a dynamic UPDATE query from the request fields.
|
||||
// Returns empty string if there are no fields to update.
|
||||
func buildUpdateQuery(id string, req UpdateProfileRequest) (string, []interface{}) {
|
||||
setClauses := []string{}
|
||||
args := []interface{}{}
|
||||
argIdx := 1
|
||||
|
||||
if req.Name != nil {
|
||||
setClauses = append(setClauses, fmt.Sprintf("name = $%d", argIdx))
|
||||
args = append(args, *req.Name)
|
||||
argIdx++
|
||||
}
|
||||
if req.HeightCM != nil {
|
||||
setClauses = append(setClauses, fmt.Sprintf("height_cm = $%d", argIdx))
|
||||
args = append(args, *req.HeightCM)
|
||||
argIdx++
|
||||
}
|
||||
if req.WeightKG != nil {
|
||||
setClauses = append(setClauses, fmt.Sprintf("weight_kg = $%d", argIdx))
|
||||
args = append(args, *req.WeightKG)
|
||||
argIdx++
|
||||
}
|
||||
if req.DateOfBirth != nil {
|
||||
setClauses = append(setClauses, fmt.Sprintf("date_of_birth = $%d", argIdx))
|
||||
args = append(args, *req.DateOfBirth)
|
||||
argIdx++
|
||||
}
|
||||
if req.Gender != nil {
|
||||
setClauses = append(setClauses, fmt.Sprintf("gender = $%d", argIdx))
|
||||
args = append(args, *req.Gender)
|
||||
argIdx++
|
||||
}
|
||||
if req.Activity != nil {
|
||||
setClauses = append(setClauses, fmt.Sprintf("activity = $%d", argIdx))
|
||||
args = append(args, *req.Activity)
|
||||
argIdx++
|
||||
}
|
||||
if req.Goal != nil {
|
||||
setClauses = append(setClauses, fmt.Sprintf("goal = $%d", argIdx))
|
||||
args = append(args, *req.Goal)
|
||||
argIdx++
|
||||
}
|
||||
if req.Preferences != nil {
|
||||
setClauses = append(setClauses, fmt.Sprintf("preferences = $%d", argIdx))
|
||||
args = append(args, string(*req.Preferences))
|
||||
argIdx++
|
||||
}
|
||||
if req.DailyCalories != nil {
|
||||
setClauses = append(setClauses, fmt.Sprintf("daily_calories = $%d", argIdx))
|
||||
args = append(args, *req.DailyCalories)
|
||||
argIdx++
|
||||
}
|
||||
|
||||
if len(setClauses) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
setClauses = append(setClauses, "updated_at = now()")
|
||||
|
||||
query := fmt.Sprintf(`
|
||||
UPDATE users SET %s
|
||||
WHERE id = $%d
|
||||
RETURNING id, firebase_uid, email, name, avatar_url,
|
||||
height_cm, weight_kg, date_of_birth, gender, activity, goal, daily_calories,
|
||||
plan, preferences, created_at, updated_at`,
|
||||
strings.Join(setClauses, ", "), argIdx)
|
||||
args = append(args, id)
|
||||
|
||||
return query, args
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateInTx(ctx context.Context, id string, profileReq UpdateProfileRequest, caloriesReq *UpdateProfileRequest) (*User, error) {
|
||||
tx, err := r.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("begin tx: %w", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// First update: profile fields
|
||||
query, args := buildUpdateQuery(id, profileReq)
|
||||
if query == "" {
|
||||
return nil, fmt.Errorf("no fields to update")
|
||||
}
|
||||
updated, err := r.scanUser(tx.QueryRow(ctx, query, args...))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("update profile: %w", err)
|
||||
}
|
||||
|
||||
// Second update: calories (if provided)
|
||||
if caloriesReq != nil {
|
||||
query, args = buildUpdateQuery(id, *caloriesReq)
|
||||
if query != "" {
|
||||
updated, err = r.scanUser(tx.QueryRow(ctx, query, args...))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("update calories: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return nil, fmt.Errorf("commit tx: %w", err)
|
||||
}
|
||||
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
func (r *Repository) SetRefreshToken(ctx context.Context, id, token string, expiresAt time.Time) error {
|
||||
query := `UPDATE users SET refresh_token = $2, token_expires_at = $3, updated_at = now() WHERE id = $1`
|
||||
_, err := r.pool.Exec(ctx, query, id, token, expiresAt)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *Repository) FindByRefreshToken(ctx context.Context, token string) (*User, error) {
|
||||
query := `
|
||||
SELECT id, firebase_uid, email, name, avatar_url,
|
||||
height_cm, weight_kg, date_of_birth, gender, activity, goal, daily_calories,
|
||||
plan, preferences, created_at, updated_at
|
||||
FROM users
|
||||
WHERE refresh_token = $1 AND token_expires_at > now()`
|
||||
|
||||
return r.scanUser(r.pool.QueryRow(ctx, query, token))
|
||||
}
|
||||
|
||||
func (r *Repository) ClearRefreshToken(ctx context.Context, id string) error {
|
||||
query := `UPDATE users SET refresh_token = NULL, token_expires_at = NULL, updated_at = now() WHERE id = $1`
|
||||
_, err := r.pool.Exec(ctx, query, id)
|
||||
return err
|
||||
}
|
||||
|
||||
type scannable interface {
|
||||
Scan(dest ...interface{}) error
|
||||
}
|
||||
|
||||
func (r *Repository) scanUser(row scannable) (*User, error) {
|
||||
var u User
|
||||
var prefs []byte
|
||||
var dob *time.Time
|
||||
err := row.Scan(
|
||||
&u.ID, &u.FirebaseUID, &u.Email, &u.Name, &u.AvatarURL,
|
||||
&u.HeightCM, &u.WeightKG, &dob, &u.Gender, &u.Activity, &u.Goal, &u.DailyCalories,
|
||||
&u.Plan, &prefs, &u.CreatedAt, &u.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("scan user: %w", err)
|
||||
}
|
||||
if dob != nil {
|
||||
s := dob.Format("2006-01-02")
|
||||
u.DateOfBirth = &s
|
||||
}
|
||||
u.Preferences = json.RawMessage(prefs)
|
||||
return &u, nil
|
||||
}
|
||||
127
backend/internal/domain/user/service.go
Normal file
127
backend/internal/domain/user/service.go
Normal file
@@ -0,0 +1,127 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo UserRepository
|
||||
}
|
||||
|
||||
func NewService(repo UserRepository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
func (s *Service) GetProfile(ctx context.Context, userID string) (*User, error) {
|
||||
return s.repo.GetByID(ctx, userID)
|
||||
}
|
||||
|
||||
func (s *Service) UpdateProfile(ctx context.Context, userID string, req UpdateProfileRequest) (*User, error) {
|
||||
if err := validateProfileRequest(req); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !req.HasBodyParams() {
|
||||
updated, err := s.repo.Update(ctx, userID, req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("update profile: %w", err)
|
||||
}
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
// Need to update profile + recalculate calories in a single transaction.
|
||||
// First, get current user to merge with incoming fields for calorie calculation.
|
||||
current, err := s.repo.GetByID(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get user: %w", err)
|
||||
}
|
||||
|
||||
// Merge current values with request to compute calories
|
||||
height := current.HeightCM
|
||||
if req.HeightCM != nil {
|
||||
height = req.HeightCM
|
||||
}
|
||||
weight := current.WeightKG
|
||||
if req.WeightKG != nil {
|
||||
weight = req.WeightKG
|
||||
}
|
||||
dob := current.DateOfBirth
|
||||
if req.DateOfBirth != nil {
|
||||
dob = req.DateOfBirth
|
||||
}
|
||||
age := AgeFromDOB(dob)
|
||||
gender := current.Gender
|
||||
if req.Gender != nil {
|
||||
gender = req.Gender
|
||||
}
|
||||
activity := current.Activity
|
||||
if req.Activity != nil {
|
||||
activity = req.Activity
|
||||
}
|
||||
goal := current.Goal
|
||||
if req.Goal != nil {
|
||||
goal = req.Goal
|
||||
}
|
||||
|
||||
calories := CalculateDailyCalories(height, weight, age, gender, activity, goal)
|
||||
|
||||
var calReq *UpdateProfileRequest
|
||||
if calories != nil {
|
||||
calReq = &UpdateProfileRequest{DailyCalories: calories}
|
||||
}
|
||||
|
||||
updated, err := s.repo.UpdateInTx(ctx, userID, req, calReq)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("update profile: %w", err)
|
||||
}
|
||||
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
func validateProfileRequest(req UpdateProfileRequest) error {
|
||||
if req.HeightCM != nil {
|
||||
if *req.HeightCM < 100 || *req.HeightCM > 250 {
|
||||
return fmt.Errorf("height_cm must be between 100 and 250")
|
||||
}
|
||||
}
|
||||
if req.WeightKG != nil {
|
||||
if *req.WeightKG < 30 || *req.WeightKG > 300 {
|
||||
return fmt.Errorf("weight_kg must be between 30 and 300")
|
||||
}
|
||||
}
|
||||
if req.DateOfBirth != nil {
|
||||
t, err := time.Parse("2006-01-02", *req.DateOfBirth)
|
||||
if err != nil {
|
||||
return fmt.Errorf("date_of_birth must be in YYYY-MM-DD format")
|
||||
}
|
||||
if t.After(time.Now()) {
|
||||
return fmt.Errorf("date_of_birth cannot be in the future")
|
||||
}
|
||||
age := AgeFromDOB(req.DateOfBirth)
|
||||
if *age < 10 || *age > 120 {
|
||||
return fmt.Errorf("date_of_birth must yield an age between 10 and 120")
|
||||
}
|
||||
}
|
||||
if req.Gender != nil {
|
||||
if *req.Gender != "male" && *req.Gender != "female" {
|
||||
return fmt.Errorf("gender must be 'male' or 'female'")
|
||||
}
|
||||
}
|
||||
if req.Activity != nil {
|
||||
switch *req.Activity {
|
||||
case "low", "moderate", "high":
|
||||
default:
|
||||
return fmt.Errorf("activity must be 'low', 'moderate', or 'high'")
|
||||
}
|
||||
}
|
||||
if req.Goal != nil {
|
||||
switch *req.Goal {
|
||||
case "lose", "maintain", "gain":
|
||||
default:
|
||||
return fmt.Errorf("goal must be 'lose', 'maintain', or 'gain'")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user