From a5ca98b549e25599787436860ed2ed2ea283981a Mon Sep 17 00:00:00 2001 From: valitovgaziz Date: Sat, 11 Oct 2025 05:41:15 +0500 Subject: [PATCH] update auth_service, add updateProfile method into interface change main package to api_bb --- serv_nginx/api_bb/cmd/main.go | 6 +- serv_nginx/api_bb/go.mod | 23 +++---- serv_nginx/api_bb/go.sum | 24 +++---- serv_nginx/api_bb/go_bbb.mod | 26 +++++++ serv_nginx/api_bb/internal/handlers/auth.go | 69 +++++++++++++++++-- .../api_bb/internal/handlers/handlers.go | 6 +- serv_nginx/api_bb/internal/handlers/health.go | 2 +- .../internal/repository/user_repository.go | 2 +- serv_nginx/api_bb/internal/routes/routes.go | 10 +-- .../api_bb/internal/service/auth_service.go | 10 +-- serv_nginx/api_bb/pkg/database/database.go | 2 +- serv_nginx/api_bb/pkg/middleware/auth.go | 6 +- 12 files changed, 136 insertions(+), 50 deletions(-) create mode 100644 serv_nginx/api_bb/go_bbb.mod diff --git a/serv_nginx/api_bb/cmd/main.go b/serv_nginx/api_bb/cmd/main.go index e8ac957..95312d6 100644 --- a/serv_nginx/api_bb/cmd/main.go +++ b/serv_nginx/api_bb/cmd/main.go @@ -8,9 +8,9 @@ import ( "gorm.io/driver/postgres" "gorm.io/gorm" - "go-rest-api/internal/config" - "go-rest-api/internal/models" - "go-rest-api/internal/routes" + "api_bb/internal/config" + "api_bb/internal/models" + "api_bb/internal/routes" ) func main() { diff --git a/serv_nginx/api_bb/go.mod b/serv_nginx/api_bb/go.mod index 6efed7f..636c5a7 100644 --- a/serv_nginx/api_bb/go.mod +++ b/serv_nginx/api_bb/go.mod @@ -1,12 +1,14 @@ -module go-rest-api +module api_bb -go 1.21 +go 1.25.1 require ( - github.com/go-chi/chi/v5 v5.0.10 - github.com/go-chi/cors v1.2.1 - golang.org/x/crypto v0.31.0 - gorm.io/gorm v1.25.10 + github.com/go-chi/chi/v5 v5.2.3 + github.com/go-chi/cors v1.2.2 + github.com/golang-jwt/jwt/v5 v5.3.0 + golang.org/x/crypto v0.43.0 + gorm.io/driver/postgres v1.6.0 + gorm.io/gorm v1.31.0 ) require ( @@ -14,13 +16,8 @@ require ( github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgx/v5 v5.6.0 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect - golang.org/x/sync v0.10.0 // indirect - golang.org/x/text v0.21.0 // indirect -) - -require ( - github.com/golang-jwt/jwt/v5 v5.3.0 github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect - gorm.io/driver/postgres v1.6.0 + golang.org/x/sync v0.17.0 // indirect + golang.org/x/text v0.30.0 // indirect ) diff --git a/serv_nginx/api_bb/go.sum b/serv_nginx/api_bb/go.sum index 6729228..d94e5fa 100644 --- a/serv_nginx/api_bb/go.sum +++ b/serv_nginx/api_bb/go.sum @@ -1,10 +1,10 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/go-chi/chi/v5 v5.0.10 h1:rLz5avzKpjqxrYwXNfmjkrYYXOyLJd37pz53UFHC6vk= -github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= -github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= -github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= +github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE= +github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops= +github.com/go-chi/cors v1.2.2 h1:Jmey33TE+b+rB7fT8MUy1u0I4L+NARQlK6LhzKPSyQE= +github.com/go-chi/cors v1.2.2/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo= github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= @@ -26,17 +26,17 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= -golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= -golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= -golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= -golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= +golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04= +golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0= +golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug= +golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= +golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k= +golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/postgres v1.6.0 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4= gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo= -gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s= -gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.31.0 h1:0VlycGreVhK7RF/Bwt51Fk8v0xLiiiFdbGDPIZQ7mJY= +gorm.io/gorm v1.31.0/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs= diff --git a/serv_nginx/api_bb/go_bbb.mod b/serv_nginx/api_bb/go_bbb.mod new file mode 100644 index 0000000..6efed7f --- /dev/null +++ b/serv_nginx/api_bb/go_bbb.mod @@ -0,0 +1,26 @@ +module go-rest-api + +go 1.21 + +require ( + github.com/go-chi/chi/v5 v5.0.10 + github.com/go-chi/cors v1.2.1 + golang.org/x/crypto v0.31.0 + gorm.io/gorm v1.25.10 +) + +require ( + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/pgx/v5 v5.6.0 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + golang.org/x/sync v0.10.0 // indirect + golang.org/x/text v0.21.0 // indirect +) + +require ( + github.com/golang-jwt/jwt/v5 v5.3.0 + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect + gorm.io/driver/postgres v1.6.0 +) diff --git a/serv_nginx/api_bb/internal/handlers/auth.go b/serv_nginx/api_bb/internal/handlers/auth.go index aa29262..eb71c4c 100644 --- a/serv_nginx/api_bb/internal/handlers/auth.go +++ b/serv_nginx/api_bb/internal/handlers/auth.go @@ -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), + }) +} diff --git a/serv_nginx/api_bb/internal/handlers/handlers.go b/serv_nginx/api_bb/internal/handlers/handlers.go index 75055a3..492c1a6 100644 --- a/serv_nginx/api_bb/internal/handlers/handlers.go +++ b/serv_nginx/api_bb/internal/handlers/handlers.go @@ -2,9 +2,9 @@ package handlers import ( - "go-rest-api/internal/repository" - "go-rest-api/internal/service" - "go-rest-api/internal/config" + "api_bb/internal/repository" + "api_bb/internal/service" + "api_bb/internal/config" "gorm.io/gorm" ) diff --git a/serv_nginx/api_bb/internal/handlers/health.go b/serv_nginx/api_bb/internal/handlers/health.go index 11b4d9e..4879f49 100644 --- a/serv_nginx/api_bb/internal/handlers/health.go +++ b/serv_nginx/api_bb/internal/handlers/health.go @@ -3,7 +3,7 @@ package handlers import ( "net/http" - "go-rest-api/pkg/utils" + "api_bb/pkg/utils" "github.com/go-chi/chi/v5" ) diff --git a/serv_nginx/api_bb/internal/repository/user_repository.go b/serv_nginx/api_bb/internal/repository/user_repository.go index e285f72..3b4ce92 100644 --- a/serv_nginx/api_bb/internal/repository/user_repository.go +++ b/serv_nginx/api_bb/internal/repository/user_repository.go @@ -1,7 +1,7 @@ package repository import ( - "go-rest-api/internal/models" + "api_bb/internal/models" "gorm.io/gorm" ) diff --git a/serv_nginx/api_bb/internal/routes/routes.go b/serv_nginx/api_bb/internal/routes/routes.go index c6e8f7f..b429612 100644 --- a/serv_nginx/api_bb/internal/routes/routes.go +++ b/serv_nginx/api_bb/internal/routes/routes.go @@ -7,11 +7,11 @@ import ( "github.com/go-chi/chi/v5" "gorm.io/gorm" - "go-rest-api/internal/config" - "go-rest-api/internal/handlers" - "go-rest-api/internal/repository" - "go-rest-api/internal/service" - "go-rest-api/pkg/middleware" + "api_bb/internal/config" + "api_bb/internal/handlers" + "api_bb/internal/repository" + "api_bb/internal/service" + "api_bb/pkg/middleware" ) func SetupRouter(db *gorm.DB, config *config.Config) http.Handler { diff --git a/serv_nginx/api_bb/internal/service/auth_service.go b/serv_nginx/api_bb/internal/service/auth_service.go index 65d3d3c..60b1d52 100644 --- a/serv_nginx/api_bb/internal/service/auth_service.go +++ b/serv_nginx/api_bb/internal/service/auth_service.go @@ -3,15 +3,18 @@ package service import ( "errors" + "fmt" + "time" - "go-rest-api/internal/models" - "go-rest-api/internal/repository" + "api_bb/internal/models" + "api_bb/internal/repository" ) type AuthService interface { Register(user *models.User) error Login(email, password string) (*models.User, string, error) GetUserProfile(userID uint) (*models.User, error) + UpdateProfile(user *models.User) error } type authService struct { @@ -48,8 +51,7 @@ func (s *authService) UpdateProfile(user *models.User) error { // Сохраняем обновленные данные через репозиторий if err := s.userRepo.Update(updateData); err != nil { - fmt.Errorf("failed to update user: %v", err) - return + return err } // Копируем обновленные данные обратно в переданный объект diff --git a/serv_nginx/api_bb/pkg/database/database.go b/serv_nginx/api_bb/pkg/database/database.go index a6dfe74..77e157a 100644 --- a/serv_nginx/api_bb/pkg/database/database.go +++ b/serv_nginx/api_bb/pkg/database/database.go @@ -5,7 +5,7 @@ import ( "log" "time" - "go-rest-api/internal/models" + "api_bb/internal/models" "gorm.io/driver/postgres" "gorm.io/gorm" ) diff --git a/serv_nginx/api_bb/pkg/middleware/auth.go b/serv_nginx/api_bb/pkg/middleware/auth.go index 14af195..37f2fb3 100644 --- a/serv_nginx/api_bb/pkg/middleware/auth.go +++ b/serv_nginx/api_bb/pkg/middleware/auth.go @@ -6,9 +6,9 @@ import ( "net/http" "strings" - "go-rest-api/internal/models" - "go-rest-api/internal/repository" - "go-rest-api/internal/service" + "api_bb/internal/models" + "api_bb/internal/repository" + "api_bb/internal/service" ) type contextKey string