Skip to content

Commit

Permalink
fix getSessionData
Browse files Browse the repository at this point in the history
  • Loading branch information
Qaleka committed Dec 21, 2024
1 parent 49dfb3e commit 639e652
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion microservices/auth_service/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func main() {

sessionService := session.NewSessionService(redisStore)
auRepository := authRepository.NewAuthRepository(db)
auUseCase := authUseCase.NewAuthUseCase(auRepository, minioService)
auUseCase := authUseCase.NewAuthUseCase(auRepository, minioService, redisStore)
authServer := grpcAuth.NewGrpcAuthHandler(auUseCase, sessionService, jwtToken)

grpcServer := grpc.NewServer(
Expand Down
2 changes: 1 addition & 1 deletion microservices/auth_service/controller/auth_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (h *GrpcAuthHandler) PutUser(ctx context.Context, in *gen.PutUserRequest) (
Birthdate: (in.Creds.Birthdate).AsTime(),
IsHost: in.Creds.IsHost,
}
err = h.usecase.PutUser(ctx, Payload, userID, in.Avatar)
err = h.usecase.PutUser(ctx, Payload, userID, in.Avatar, in.SessionId)
if err != nil {
logger.AccessLogger.Warn("Failed to put user",
zap.String("request_id", requestID),
Expand Down
24 changes: 21 additions & 3 deletions microservices/auth_service/usecase/auth_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@ import (
"2024_2_FIGHT-CLUB/internal/service/images"
"2024_2_FIGHT-CLUB/internal/service/logger"
"2024_2_FIGHT-CLUB/internal/service/middleware"
"2024_2_FIGHT-CLUB/internal/service/session"
"2024_2_FIGHT-CLUB/internal/service/validation"
"context"
"errors"
"github.com/mailru/easyjson"
"go.uber.org/zap"
"net/http"
"regexp"
"time"
)

type AuthUseCase interface {
RegisterUser(ctx context.Context, creds *domain.User) error
LoginUser(ctx context.Context, creds *domain.User) (*domain.User, error)
PutUser(ctx context.Context, creds *domain.User, userID string, avatar []byte) error
PutUser(ctx context.Context, creds *domain.User, userID string, avatar []byte, sessionId string) error
GetAllUser(ctx context.Context) ([]domain.User, error)
GetUserById(ctx context.Context, userID string) (*domain.User, error)
UpdateUserRegions(ctx context.Context, regions domain.UpdateUserRegion, userId string) error
Expand All @@ -27,12 +29,14 @@ type AuthUseCase interface {
type authUseCase struct {
authRepository domain.AuthRepository
minioService images.MinioServiceInterface
store session.RedisInterface
}

func NewAuthUseCase(authRepository domain.AuthRepository, minioService images.MinioServiceInterface) AuthUseCase {
func NewAuthUseCase(authRepository domain.AuthRepository, minioService images.MinioServiceInterface, store session.RedisInterface) AuthUseCase {
return &authUseCase{
authRepository: authRepository,
minioService: minioService,
store: store,
}
}

Expand Down Expand Up @@ -157,7 +161,7 @@ func (uc *authUseCase) LoginUser(ctx context.Context, creds *domain.User) (*doma
return requestedUser, nil
}

func (uc *authUseCase) PutUser(ctx context.Context, creds *domain.User, userID string, avatar []byte) error {
func (uc *authUseCase) PutUser(ctx context.Context, creds *domain.User, userID string, avatar []byte, sessionId string) error {
requestID := middleware.GetRequestID(ctx)
const maxLen = 255
validCharPattern := regexp.MustCompile(`^[a-zA-Zа-яА-Я0-9@.,\s\-_]*$`)
Expand Down Expand Up @@ -225,6 +229,20 @@ func (uc *authUseCase) PutUser(ctx context.Context, creds *domain.User, userID s
}
return err
}

sessionStore := uc.store
sessionData, err := sessionStore.Get(ctx, sessionId)
if err != nil {
logger.AccessLogger.Warn("Failed to get session from Redis", zap.String("request_id", requestID), zap.Error(err))
return errors.New("failed to update Redis")
}

sessionData.Avatar = creds.Avatar
if err := sessionStore.Set(ctx, sessionId, sessionData, 24*time.Hour); err != nil {
logger.AccessLogger.Warn("Failed to update session in Redis", zap.String("request_id", requestID), zap.Error(err))
return errors.New("failed to update Redis")
}

return nil
}

Expand Down

0 comments on commit 639e652

Please sign in to comment.