-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_service.go
127 lines (103 loc) · 2.91 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package service
import (
"errors"
"project3/middleware"
"project3/model/entity"
"project3/model/input"
"project3/repository"
"golang.org/x/crypto/bcrypt"
)
type UserService interface {
CreateUser(userInput input.UserRegisterInput) (entity.User, error)
CreateAdmin(input input.UserRegisterInput) (entity.User, error)
LoginUser(userInput input.UserLoginInput) (string, error)
UpdateUser(id_user int, input input.UserUpdateInput) (entity.User, error)
DeleteUser(id_user int) (entity.User, error)
}
type userService struct {
userRepository repository.UserRepository
}
func NewUserService(userRepository repository.UserRepository) *userService {
return &userService{userRepository}
}
func (s *userService) CreateUser(input input.UserRegisterInput) (entity.User, error) {
passwordHash, err := bcrypt.GenerateFromPassword([]byte(input.Password), bcrypt.MinCost)
if err != nil {
return entity.User{}, err
}
user := entity.User{
FullName: input.FullName,
Email: input.Email,
Password: string(passwordHash),
Role: "member",
}
userData, err := s.userRepository.CreateUser(user)
if err != nil {
return entity.User{}, err
}
return userData, nil
}
func (s *userService) CreateAdmin(input input.UserRegisterInput) (entity.User, error) {
passwordHash, err := bcrypt.GenerateFromPassword([]byte(input.Password), bcrypt.MinCost)
if err != nil {
return entity.User{}, err
}
user := entity.User{
FullName: input.FullName,
Email: input.Email,
Password: string(passwordHash),
Role: "admin",
}
userData, err := s.userRepository.CreateUser(user)
if err != nil {
return entity.User{}, err
}
return userData, nil
}
func (s *userService) LoginUser(userInput input.UserLoginInput) (string, error) {
userData, err := s.userRepository.FindByEmail(userInput.Email)
if err != nil {
return "", err
}
if userData.ID == 0 {
return "", errors.New("user not found")
}
err = bcrypt.CompareHashAndPassword([]byte(userData.Password), []byte(userInput.Password))
if err != nil {
return "", errors.New("wrong password")
}
token, err := middleware.GenerateToken(userData.ID, userData.Role)
if err != nil {
return "", err
}
return token, nil
}
func (s *userService) UpdateUser(id_user int, input input.UserUpdateInput) (entity.User, error) {
user := entity.User{
FullName: input.FullName,
Email: input.Email,
}
_, err := s.userRepository.Update(id_user, user)
if err != nil {
return entity.User{}, err
}
userData, err := s.userRepository.FindByID(id_user)
if err != nil {
return entity.User{}, err
}
return userData, nil
}
func (s *userService) DeleteUser(id_user int) (entity.User, error) {
userData, err := s.userRepository.FindByID(id_user)
if err != nil {
return entity.User{}, err
}
if userData.ID == 0 {
return entity.User{}, errors.New("user not found")
}
userData, err = s.userRepository.Delete(id_user)
if err != nil {
return entity.User{}, err
}
return userData, nil
}