update auth_service, add updateProfile method into interface
change main package to api_bb
This commit is contained in:
@@ -9,10 +9,10 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"go-rest-api/internal/models"
|
||||
"go-rest-api/internal/service"
|
||||
"go-rest-api/pkg/middleware"
|
||||
"go-rest-api/pkg/utils"
|
||||
"api_bb/internal/models"
|
||||
"api_bb/internal/service"
|
||||
"api_bb/pkg/middleware"
|
||||
"api_bb/pkg/utils"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
@@ -252,3 +252,64 @@ func toUserResponse(user *models.User) UserResponse {
|
||||
UpdatedAt: user.UpdatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
type UpdateProfileRequest struct {
|
||||
FirstName string `json:"firstName"`
|
||||
LastName string `json:"lastName"`
|
||||
Phone string `json:"phone"`
|
||||
Experience string `json:"experience"`
|
||||
Goals string `json:"goals"`
|
||||
Newsletter bool `json:"newsletter"`
|
||||
}
|
||||
|
||||
func (h *AuthHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
// Устанавливаем CORS заголовки
|
||||
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
|
||||
w.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
|
||||
// Получаем пользователя из контекста
|
||||
currentUser, ok := middleware.GetUserFromContext(r.Context())
|
||||
if !ok {
|
||||
utils.RespondWithError(w, http.StatusUnauthorized, "Authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
var req UpdateProfileRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
utils.RespondWithError(w, http.StatusBadRequest, "Invalid request payload: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// Валидация обязательных полей
|
||||
if req.FirstName == "" {
|
||||
utils.RespondWithError(w, http.StatusBadRequest, "First name is required")
|
||||
return
|
||||
}
|
||||
if req.LastName == "" {
|
||||
utils.RespondWithError(w, http.StatusBadRequest, "Last name is required")
|
||||
return
|
||||
}
|
||||
|
||||
// Обновляем данные пользователя
|
||||
updatedUser := &models.User{
|
||||
ID: currentUser.ID,
|
||||
FirstName: req.FirstName,
|
||||
LastName: req.LastName,
|
||||
Phone: req.Phone,
|
||||
Experience: req.Experience,
|
||||
Goals: req.Goals,
|
||||
Newsletter: req.Newsletter,
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
// Сохраняем обновленные данные
|
||||
if err := h.authService.UpdateProfile(updatedUser); err != nil {
|
||||
utils.RespondWithError(w, http.StatusInternalServerError, "Failed to update profile: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.RespondWithJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"message": "Profile updated successfully",
|
||||
"user": toUserResponse(updatedUser),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user