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,
|
||||
|
||||
Reference in New Issue
Block a user