update auth_service, add updateProfile method into interface

change main package to api_bb
This commit is contained in:
2025-10-11 05:41:15 +05:00
parent 62b0d4763d
commit a5ca98b549
12 changed files with 136 additions and 50 deletions
+3 -3
View File
@@ -8,9 +8,9 @@ import (
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
"go-rest-api/internal/config" "api_bb/internal/config"
"go-rest-api/internal/models" "api_bb/internal/models"
"go-rest-api/internal/routes" "api_bb/internal/routes"
) )
func main() { func main() {
+10 -13
View File
@@ -1,12 +1,14 @@
module go-rest-api module api_bb
go 1.21 go 1.25.1
require ( require (
github.com/go-chi/chi/v5 v5.0.10 github.com/go-chi/chi/v5 v5.2.3
github.com/go-chi/cors v1.2.1 github.com/go-chi/cors v1.2.2
golang.org/x/crypto v0.31.0 github.com/golang-jwt/jwt/v5 v5.3.0
gorm.io/gorm v1.25.10 golang.org/x/crypto v0.43.0
gorm.io/driver/postgres v1.6.0
gorm.io/gorm v1.31.0
) )
require ( require (
@@ -14,13 +16,8 @@ require (
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.6.0 // indirect github.com/jackc/pgx/v5 v5.6.0 // indirect
github.com/jackc/puddle/v2 v2.2.2 // 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/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // 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
) )
+12 -12
View File
@@ -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.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
github.com/go-chi/chi/v5 v5.0.10/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
github.com/go-chi/cors v1.2.1 h1:xEC8UT3Rlp2QuWNEr4Fs/c2EAGVKBwy/1vHx3bppil4= github.com/go-chi/cors v1.2.2 h1:Jmey33TE+b+rB7fT8MUy1u0I4L+NARQlK6LhzKPSyQE=
github.com/go-chi/cors v1.2.1/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58= 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 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE= github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= 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.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= 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.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ= golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo= golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ= 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/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.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 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 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 h1:2dxzU8xJ+ivvqTRph34QX+WrRaJlmfyPqXmoGVjMBa4=
gorm.io/driver/postgres v1.6.0/go.mod h1:vUw0mrGgrTK+uPHEhAdV4sfFELrByKVGnaVRkXDhtWo= 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.31.0 h1:0VlycGreVhK7RF/Bwt51Fk8v0xLiiiFdbGDPIZQ7mJY=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= gorm.io/gorm v1.31.0/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs=
+26
View File
@@ -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
)
+65 -4
View File
@@ -9,10 +9,10 @@ import (
"net/http" "net/http"
"time" "time"
"go-rest-api/internal/models" "api_bb/internal/models"
"go-rest-api/internal/service" "api_bb/internal/service"
"go-rest-api/pkg/middleware" "api_bb/pkg/middleware"
"go-rest-api/pkg/utils" "api_bb/pkg/utils"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
@@ -252,3 +252,64 @@ func toUserResponse(user *models.User) UserResponse {
UpdatedAt: user.UpdatedAt, 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),
})
}
@@ -2,9 +2,9 @@
package handlers package handlers
import ( import (
"go-rest-api/internal/repository" "api_bb/internal/repository"
"go-rest-api/internal/service" "api_bb/internal/service"
"go-rest-api/internal/config" "api_bb/internal/config"
"gorm.io/gorm" "gorm.io/gorm"
) )
@@ -3,7 +3,7 @@ package handlers
import ( import (
"net/http" "net/http"
"go-rest-api/pkg/utils" "api_bb/pkg/utils"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
@@ -1,7 +1,7 @@
package repository package repository
import ( import (
"go-rest-api/internal/models" "api_bb/internal/models"
"gorm.io/gorm" "gorm.io/gorm"
) )
+5 -5
View File
@@ -7,11 +7,11 @@ import (
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
"gorm.io/gorm" "gorm.io/gorm"
"go-rest-api/internal/config" "api_bb/internal/config"
"go-rest-api/internal/handlers" "api_bb/internal/handlers"
"go-rest-api/internal/repository" "api_bb/internal/repository"
"go-rest-api/internal/service" "api_bb/internal/service"
"go-rest-api/pkg/middleware" "api_bb/pkg/middleware"
) )
func SetupRouter(db *gorm.DB, config *config.Config) http.Handler { func SetupRouter(db *gorm.DB, config *config.Config) http.Handler {
@@ -3,15 +3,18 @@ package service
import ( import (
"errors" "errors"
"fmt"
"time"
"go-rest-api/internal/models" "api_bb/internal/models"
"go-rest-api/internal/repository" "api_bb/internal/repository"
) )
type AuthService interface { type AuthService interface {
Register(user *models.User) error Register(user *models.User) error
Login(email, password string) (*models.User, string, error) Login(email, password string) (*models.User, string, error)
GetUserProfile(userID uint) (*models.User, error) GetUserProfile(userID uint) (*models.User, error)
UpdateProfile(user *models.User) error
} }
type authService struct { type authService struct {
@@ -48,8 +51,7 @@ func (s *authService) UpdateProfile(user *models.User) error {
// Сохраняем обновленные данные через репозиторий // Сохраняем обновленные данные через репозиторий
if err := s.userRepo.Update(updateData); err != nil { if err := s.userRepo.Update(updateData); err != nil {
fmt.Errorf("failed to update user: %v", err) return err
return
} }
// Копируем обновленные данные обратно в переданный объект // Копируем обновленные данные обратно в переданный объект
+1 -1
View File
@@ -5,7 +5,7 @@ import (
"log" "log"
"time" "time"
"go-rest-api/internal/models" "api_bb/internal/models"
"gorm.io/driver/postgres" "gorm.io/driver/postgres"
"gorm.io/gorm" "gorm.io/gorm"
) )
+3 -3
View File
@@ -6,9 +6,9 @@ import (
"net/http" "net/http"
"strings" "strings"
"go-rest-api/internal/models" "api_bb/internal/models"
"go-rest-api/internal/repository" "api_bb/internal/repository"
"go-rest-api/internal/service" "api_bb/internal/service"
) )
type contextKey string type contextKey string