Skip to content

Commit fce4a07

Browse files
committed
[users] rename get method
1 parent 76982d0 commit fce4a07

File tree

4 files changed

+9
-13
lines changed

4 files changed

+9
-13
lines changed

internal/sms-gateway/handlers/middlewares/jwtauth/jwtauth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewJWT(jwtSvc jwt.Service, usersSvc *users.Service) fiber.Handler {
2626
return fiber.ErrUnauthorized
2727
}
2828

29-
user, err := usersSvc.GetByUsername(claims.UserID)
29+
user, err := usersSvc.GetByID(claims.UserID)
3030
if err != nil {
3131
if !errors.Is(err, users.ErrNotFound) {
3232
return fiber.ErrInternalServerError

internal/sms-gateway/modules/auth/service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (s *Service) AuthorizeUserByCode(code string) (*users.User, error) {
136136
return nil, fmt.Errorf("failed to get user by code: %w", err)
137137
}
138138

139-
user, err := s.usersSvc.GetByUsername(userID)
139+
user, err := s.usersSvc.GetByID(userID)
140140
if err != nil {
141141
return nil, fmt.Errorf("failed to get user: %w", err)
142142
}

internal/sms-gateway/users/repository.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"fmt"
66

7+
"github.com/android-sms-gateway/server/pkg/mysql"
78
"gorm.io/gorm"
89
)
910

@@ -45,6 +46,10 @@ func (r *repository) GetByID(id string) (*userModel, error) {
4546

4647
func (r *repository) Insert(user *userModel) error {
4748
if err := r.db.Create(user).Error; err != nil {
49+
if mysql.IsDuplicateKeyViolation(err) {
50+
return fmt.Errorf("%w: %w", ErrExists, err)
51+
}
52+
4853
return fmt.Errorf("can't create user: %w", err)
4954
}
5055

internal/sms-gateway/users/service.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,6 @@ func NewService(
3333
}
3434

3535
func (s *Service) Create(username, password string) (*User, error) {
36-
exists, err := s.users.Exists(username)
37-
if err != nil {
38-
return nil, err
39-
}
40-
41-
if exists {
42-
return nil, fmt.Errorf("%w: %s", ErrExists, username)
43-
}
44-
4536
passwordHash, err := crypto.MakeBCryptHash(password)
4637
if err != nil {
4738
return nil, fmt.Errorf("failed to hash password: %w", err)
@@ -56,8 +47,8 @@ func (s *Service) Create(username, password string) (*User, error) {
5647
return newUser(user), nil
5748
}
5849

59-
func (s *Service) GetByUsername(username string) (*User, error) {
60-
user, err := s.users.GetByID(username)
50+
func (s *Service) GetByID(id string) (*User, error) {
51+
user, err := s.users.GetByID(id)
6152
if err != nil {
6253
return nil, err
6354
}

0 commit comments

Comments
 (0)