lots of stuff, don't truly remember
This commit is contained in:
@@ -35,8 +35,6 @@ func (h *Handler) GetProfile(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("DEBUG GetProfile: User ID=%d, Profile=%+v", user.ID, user.Profile)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(GetProfileResponse{
|
||||
User: user,
|
||||
@@ -48,13 +46,21 @@ func (h *Handler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
|
||||
|
||||
var req struct {
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Bio string `json:"bio"`
|
||||
FTP int `json:"ftp"`
|
||||
MaxHR int `json:"max_hr"`
|
||||
RestingHR int `json:"resting_hr"`
|
||||
Weight float64 `json:"weight"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
Bio string `json:"bio"`
|
||||
FTP int `json:"ftp"`
|
||||
MaxHR int `json:"max_hr"`
|
||||
RestingHR int `json:"resting_hr"`
|
||||
Weight float64 `json:"weight"`
|
||||
Height float64 `json:"height"`
|
||||
Age int `json:"age"`
|
||||
Gender string `json:"gender"`
|
||||
NutritionGoal string `json:"nutrition_goal"`
|
||||
TargetWeight float64 `json:"target_weight"`
|
||||
ActivityLevel string `json:"activity_level"`
|
||||
DietaryPref string `json:"dietary_preference"`
|
||||
Units string `json:"units"`
|
||||
}
|
||||
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
@@ -72,8 +78,6 @@ func (h *Handler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("DEBUG UpdateProfile: Before - Profile=%+v", user.Profile)
|
||||
|
||||
if user.Profile != nil {
|
||||
user.Profile.FirstName = req.FirstName
|
||||
user.Profile.LastName = req.LastName
|
||||
@@ -82,11 +86,19 @@ func (h *Handler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
user.Profile.MaxHR = req.MaxHR
|
||||
user.Profile.RestingHR = req.RestingHR
|
||||
user.Profile.Weight = req.Weight
|
||||
|
||||
log.Printf("DEBUG UpdateProfile: After - Profile=%+v", user.Profile)
|
||||
user.Profile.Height = req.Height
|
||||
user.Profile.Age = req.Age
|
||||
user.Profile.Gender = req.Gender
|
||||
user.Profile.NutritionGoal = req.NutritionGoal
|
||||
user.Profile.TargetWeight = req.TargetWeight
|
||||
user.Profile.ActivityLevel = req.ActivityLevel
|
||||
user.Profile.DietaryPref = req.DietaryPref
|
||||
if req.Units == "imperial" || req.Units == "metric" {
|
||||
user.Profile.Units = req.Units
|
||||
}
|
||||
|
||||
if err := h.service.UpdateUser(user); err != nil {
|
||||
log.Printf("DEBUG UpdateProfile: Error updating - %v", err)
|
||||
log.Printf("UpdateProfile: error saving - %v", err)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "failed to update profile"})
|
||||
@@ -95,7 +107,7 @@ func (h *Handler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
user, err = h.service.GetUserByID(claims.UserID)
|
||||
if err != nil {
|
||||
log.Printf("DEBUG UpdateProfile: Error reloading - %v", err)
|
||||
log.Printf("UpdateProfile: error reloading user - %v", err)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
json.NewEncoder(w).Encode(map[string]string{"error": "failed to load profile"})
|
||||
@@ -103,8 +115,6 @@ func (h *Handler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("DEBUG UpdateProfile: Final - Profile=%+v", user.Profile)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(GetProfileResponse{
|
||||
User: user,
|
||||
|
||||
@@ -13,6 +13,7 @@ type User struct {
|
||||
Username string `gorm:"uniqueIndex;not null" json:"username"`
|
||||
Email string `gorm:"uniqueIndex;not null" json:"email"`
|
||||
Password string `gorm:"not null" json:"-"`
|
||||
Role string `gorm:"default:'athlete'" json:"role"`
|
||||
IsActive bool `gorm:"default:true" json:"is_active"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
@@ -33,6 +34,14 @@ type Profile struct {
|
||||
MaxHR int `gorm:"default:0" json:"max_hr"`
|
||||
FTP int `gorm:"default:0" json:"ftp"`
|
||||
Weight float64 `gorm:"default:0" json:"weight"`
|
||||
Height float64 `gorm:"default:0" json:"height"` // cm
|
||||
Age int `gorm:"default:0" json:"age"`
|
||||
Gender string `gorm:"default:''" json:"gender"` // male, female
|
||||
NutritionGoal string `gorm:"default:''" json:"nutrition_goal"` // weight_loss, maintenance, performance
|
||||
TargetWeight float64 `gorm:"default:0" json:"target_weight"` // kg
|
||||
ActivityLevel string `gorm:"default:''" json:"activity_level"` // sedentary, lightly_active, active, very_active
|
||||
DietaryPref string `gorm:"default:''" json:"dietary_preference"` // balanced, high_carb, high_protein, keto
|
||||
Units string `gorm:"default:'metric'" json:"units"` // metric, imperial
|
||||
TotalRides int `gorm:"default:0" json:"total_rides"`
|
||||
TotalDistance float64 `gorm:"default:0" json:"total_distance"`
|
||||
TotalTime int `gorm:"default:0" json:"total_time"`
|
||||
@@ -102,4 +111,4 @@ func (prt *PasswordReset) IsValid() bool {
|
||||
|
||||
func (s *Session) IsValid() bool {
|
||||
return time.Now().Before(s.ExpiresAt)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -61,8 +61,6 @@ func (r *Repository) GetUserByID(id uint) (*User, error) {
|
||||
user.Profile = &profile
|
||||
}
|
||||
|
||||
log.Printf("DEBUG: Loaded user %d, profile ID=%d, profile=%+v", id, profile.ID, user.Profile)
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -27,14 +27,17 @@ func NewService() *Service {
|
||||
}
|
||||
|
||||
func (s *Service) CreateUser(username, password, email, firstName, lastName string) (*User, error) {
|
||||
if username == "" || password == "" {
|
||||
return nil, errors.New("username and password are required")
|
||||
if username == "" || password == "" || email == "" {
|
||||
return nil, errors.New("username, password, and email are required")
|
||||
}
|
||||
|
||||
if email != "" {
|
||||
if !isValidEmail(email) {
|
||||
return nil, errors.New("invalid email format")
|
||||
}
|
||||
// Username: 3-30 chars, alphanumeric + underscores/hyphens, must start with a letter
|
||||
if !isValidUsername(username) {
|
||||
return nil, errors.New("username must be 3-30 characters, start with a letter, and contain only letters, numbers, underscores, or hyphens")
|
||||
}
|
||||
|
||||
if !isValidEmail(email) {
|
||||
return nil, errors.New("invalid email format")
|
||||
}
|
||||
|
||||
exists, err := s.repo.UserExists(username, email)
|
||||
@@ -158,6 +161,14 @@ func (s *Service) UpdateUser(user *User) error {
|
||||
return s.repo.UpdateUser(user)
|
||||
}
|
||||
|
||||
func isValidUsername(username string) bool {
|
||||
if len(username) < 3 || len(username) > 30 {
|
||||
return false
|
||||
}
|
||||
regex := regexp.MustCompile(`^[a-zA-Z][a-zA-Z0-9_-]{2,29}$`)
|
||||
return regex.MatchString(username)
|
||||
}
|
||||
|
||||
func isValidEmail(email string) bool {
|
||||
regex := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)
|
||||
return regex.MatchString(email)
|
||||
|
||||
Reference in New Issue
Block a user