-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_service.go
73 lines (61 loc) · 1.89 KB
/
user_service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package service
import (
"context"
"errors"
"fmt"
"github.com/drexedam/gravatar"
"github.com/reinanhs/golang-web-api-structure/internal/helper"
"github.com/reinanhs/golang-web-api-structure/internal/repository"
"github.com/reinanhs/golang-web-api-structure/pkg/dto"
"github.com/reinanhs/golang-web-api-structure/pkg/entity"
)
var (
errorInvalidEmail = "o e-mail já está sendo usado"
errorInvalidUsername = "o username já está sendo usado"
)
type UserService interface {
Store(dto dto.RegisterDto) (*entity.User, error)
}
type userService struct {
repository repository.UserRepository
credentialsService CommonCredentialsService
ctx context.Context
}
//NewUserService is creates a new instance of JWTService
func NewUserService(ctx context.Context) UserService {
return &userService{
repository: repository.NewUserRepository(ctx),
credentialsService: NewCommonCredentialsService(ctx),
ctx: ctx,
}
}
func (s *userService) Store(dto dto.RegisterDto) (*entity.User, error) {
if s.repository.EmailIsInUsed(dto.Email) {
return nil, errors.New(errorInvalidEmail)
}
if s.repository.UsernameIsInUsed(dto.Username) {
return nil, errors.New(errorInvalidUsername)
}
isValidPassword, err := s.credentialsService.ValidPasswordsCommonCredentials(dto.Password)
if err != nil || !isValidPassword {
return nil, err
}
generateFromPassword, err := helper.GenerateFromPassword(dto.Password, helper.GetParams())
if err != nil {
return nil, err
}
user := &entity.User{
Username: dto.Username,
Name: dto.Name,
Avatar: gravatar.New(dto.Email).
Size(200).
DefaultURL(fmt.Sprintf("https://avatars.dicebear.com/api/initials/%s.png?size=200", dto.Username)).
AvatarURL(),
Password: generateFromPassword,
Email: dto.Email,
}
if err := s.repository.Store(user); err != nil {
return nil, errors.New(err.Error())
}
return user, nil
}