feat: implement Phase 2 - Equipment Management and Training Zones
This commit is contained in:
@@ -2,6 +2,7 @@ package user
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"rideaware/internal/config"
|
||||
@@ -26,7 +27,7 @@ type GetProfileResponse struct {
|
||||
func (h *Handler) GetProfile(w http.ResponseWriter, r *http.Request) {
|
||||
claims := r.Context().Value(middleware.UserContextKey).(*config.CustomClaims)
|
||||
|
||||
user, err := h.service.repo.GetUserByID(claims.UserID)
|
||||
user, err := h.service.GetUserByID(claims.UserID)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
@@ -34,6 +35,8 @@ 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,
|
||||
@@ -50,6 +53,7 @@ func (h *Handler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
Bio string `json:"bio"`
|
||||
FTP int `json:"ftp"`
|
||||
MaxHR int `json:"max_hr"`
|
||||
RestingHR int `json:"resting_hr"`
|
||||
Weight float64 `json:"weight"`
|
||||
}
|
||||
|
||||
@@ -60,7 +64,7 @@ func (h *Handler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.service.repo.GetUserByID(claims.UserID)
|
||||
user, err := h.service.GetUserByID(claims.UserID)
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
@@ -68,23 +72,39 @@ func (h *Handler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// Update profile
|
||||
log.Printf("DEBUG UpdateProfile: Before - Profile=%+v", user.Profile)
|
||||
|
||||
if user.Profile != nil {
|
||||
user.Profile.FirstName = req.FirstName
|
||||
user.Profile.LastName = req.LastName
|
||||
user.Profile.Bio = req.Bio
|
||||
user.Profile.FTP = req.FTP
|
||||
user.Profile.MaxHR = req.MaxHR
|
||||
user.Profile.RestingHR = req.RestingHR
|
||||
user.Profile.Weight = req.Weight
|
||||
|
||||
if err := h.service.repo.UpdateUser(user); err != nil {
|
||||
log.Printf("DEBUG UpdateProfile: After - Profile=%+v", user.Profile)
|
||||
|
||||
if err := h.service.UpdateUser(user); err != nil {
|
||||
log.Printf("DEBUG UpdateProfile: Error updating - %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"})
|
||||
return
|
||||
}
|
||||
|
||||
user, err = h.service.GetUserByID(claims.UserID)
|
||||
if err != nil {
|
||||
log.Printf("DEBUG UpdateProfile: Error reloading - %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"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("DEBUG UpdateProfile: Final - Profile=%+v", user.Profile)
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(GetProfileResponse{
|
||||
User: user,
|
||||
|
||||
@@ -17,14 +17,14 @@ type User struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
Profile *Profile `gorm:"foreignKey:UserID;constraint:OnDelete:Cascade" json:"profile,omitempty"`
|
||||
PasswordResets []PasswordReset `gorm:"foreignKey:UserID;constraint:OnDelete:Cascade" json:"password_resets,omitempty"`
|
||||
Sessions []Session `gorm:"foreignKey:UserID;constraint:OnDelete:Cascade" json:"sessions,omitempty"`
|
||||
Profile *Profile `gorm:"foreignKey:UserID;references:ID" json:"profile,omitempty"`
|
||||
PasswordResets []PasswordReset `gorm:"foreignKey:UserID;references:ID" json:"password_resets,omitempty"`
|
||||
Sessions []Session `gorm:"foreignKey:UserID;references:ID" json:"sessions,omitempty"`
|
||||
}
|
||||
|
||||
type Profile struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
UserID uint `gorm:"not null;uniqueIndex" json:"user_id"`
|
||||
UserID uint `gorm:"not null;uniqueIndex;index" json:"user_id"`
|
||||
FirstName string `gorm:"default:''" json:"first_name"`
|
||||
LastName string `gorm:"default:''" json:"last_name"`
|
||||
Bio string `gorm:"default:''" json:"bio"`
|
||||
@@ -40,9 +40,14 @@ type Profile struct {
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
// Specify the table name
|
||||
func (Profile) TableName() string {
|
||||
return "user_profiles"
|
||||
}
|
||||
|
||||
type PasswordReset struct {
|
||||
ID uint `gorm:"primaryKey" json:"id"`
|
||||
UserID uint `gorm:"not null" json:"user_id"`
|
||||
UserID uint `gorm:"not null;index" json:"user_id"`
|
||||
Token string `gorm:"uniqueIndex;not null" json:"token"`
|
||||
ExpiresAt time.Time `gorm:"not null" json:"expires_at"`
|
||||
UsedAt *time.Time `json:"used_at"`
|
||||
@@ -62,7 +67,6 @@ type Session struct {
|
||||
|
||||
// ===== Methods =====
|
||||
|
||||
// SetPassword hashes and sets the password
|
||||
func (u *User) SetPassword(rawPassword string) error {
|
||||
if len(rawPassword) < 8 {
|
||||
return errors.New("password must be at least 8 characters long")
|
||||
@@ -78,7 +82,6 @@ func (u *User) SetPassword(rawPassword string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// CheckPassword verifies the password
|
||||
func (u *User) CheckPassword(password string) bool {
|
||||
return bcrypt.CompareHashAndPassword(
|
||||
[]byte(u.Password),
|
||||
@@ -86,7 +89,6 @@ func (u *User) CheckPassword(password string) bool {
|
||||
) == nil
|
||||
}
|
||||
|
||||
// AfterCreate hook: automatically create profile after user insert
|
||||
func (u *User) AfterCreate(tx *gorm.DB) error {
|
||||
profile := &Profile{
|
||||
UserID: u.ID,
|
||||
@@ -94,12 +96,10 @@ func (u *User) AfterCreate(tx *gorm.DB) error {
|
||||
return tx.Create(profile).Error
|
||||
}
|
||||
|
||||
// IsPasswordResetTokenValid checks if token exists and is not expired
|
||||
func (prt *PasswordReset) IsValid() bool {
|
||||
return prt.UsedAt == nil && time.Now().Before(prt.ExpiresAt)
|
||||
}
|
||||
|
||||
// IsSessionValid checks if session is not expired
|
||||
func (s *Session) IsValid() bool {
|
||||
return time.Now().Before(s.ExpiresAt)
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package user
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"rideaware/pkg/database"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -40,17 +41,45 @@ func (r *Repository) GetUserByEmail(email string) (*User, error) {
|
||||
|
||||
func (r *Repository) GetUserByID(id uint) (*User, error) {
|
||||
var user User
|
||||
if err := database.DB.Preload("Profile").Where("id = ?", id).First(&user).Error; err != nil {
|
||||
|
||||
// Get the user
|
||||
if err := database.DB.Where("id = ?", id).First(&user).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, errors.New("user not found")
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Manually load the profile
|
||||
var profile Profile
|
||||
if err := database.DB.Where("user_id = ?", id).First(&profile).Error; err != nil {
|
||||
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
log.Printf("Error loading profile: %v", err)
|
||||
}
|
||||
// Profile might not exist, that's okay
|
||||
} else {
|
||||
user.Profile = &profile
|
||||
}
|
||||
|
||||
log.Printf("DEBUG: Loaded user %d, profile ID=%d, profile=%+v", id, profile.ID, user.Profile)
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (r *Repository) UpdateUser(user *User) error {
|
||||
return database.DB.Save(user).Error
|
||||
// Update the user
|
||||
if err := database.DB.Model(user).Updates(user).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Update the profile if it exists
|
||||
if user.Profile != nil {
|
||||
if err := database.DB.Model(user.Profile).Updates(user.Profile).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Repository) UserExists(username, email string) (bool, error) {
|
||||
|
||||
@@ -144,6 +144,16 @@ func (s *Service) ResetPassword(token, newPassword string) error {
|
||||
return tx.Commit().Error
|
||||
}
|
||||
|
||||
// GetUserByID retrieves a user with their profile
|
||||
func (s *Service) GetUserByID(userID uint) (*User, error) {
|
||||
return s.repo.GetUserByID(userID)
|
||||
}
|
||||
|
||||
// UpdateUser saves user changes
|
||||
func (s *Service) UpdateUser(user *User) error {
|
||||
return s.repo.UpdateUser(user)
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
func isValidEmail(email string) bool {
|
||||
regex := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)
|
||||
|
||||
Reference in New Issue
Block a user