-
Notifications
You must be signed in to change notification settings - Fork 0
/
userService.go
131 lines (105 loc) · 3.36 KB
/
userService.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
128
129
130
131
package services
import (
"github.com/asaskevich/govalidator"
"github.com/krushev/go-footing/db"
"github.com/krushev/go-footing/models"
"github.com/krushev/go-footing/util"
"golang.org/x/crypto/bcrypt"
"gorm.io/gorm"
"strings"
)
type UserService interface {
FindAll() (*[]models.User, error)
FindActive() (*[]models.User, error)
Search(q string, pagination *models.Pagination) (*[]models.User, error)
GetById(id string) (*models.User, error)
GetByEmail(email string) (*models.User, error)
Create(user models.User) (*models.User, error)
EncryptPassword(password string) (string, error)
VerifyPassword(hashed, password string) error
IsEmpty() (bool, error)
}
type userService struct {
db *gorm.DB
}
func NewUserService(conn db.Connection) *userService {
return &userService{db: conn.GetDB()}
}
func (us *userService) FindAll() (*[]models.User, error) {
var users []models.User
result := us.db.Find(&users)
return &users, result.Error
}
// Search applies a filter and returns paginated array of matching results.
func (us *userService) Search(q string, pagination *models.Pagination) (*[]models.User, error) {
var users []models.User
offset := (pagination.Page - 1) * pagination.Size
queryBuilder := us.db.Limit(pagination.Size).Offset(offset).Order(pagination.Sort)
result := queryBuilder.Model(&models.User{}).
Where("lower(name) LIKE lower(?)", "%" + q + "%").
Or("lower(email) LIKE lower(?)", "%" + q + "%").Find(&users)
if result.Error != nil {
return nil, result.Error
}
//var totalRows int64 = 0
//errCount := us.db.Model(&models.User{}).Count(&totalRows).Error
//if errCount != nil {
// return nil, totalRows, errCount
//}
return &users, nil
}
func (us *userService) FindActive() (*[]models.User, error) {
var users []models.User
result := us.db.Find(&users, models.User{Active: true})
return &users, result.Error
}
func (us *userService) GetById(id string) (*models.User, error) {
var user models.User
result := us.db.First(&user, id)
return &user, result.Error
}
func (us *userService) GetByEmail(email string) (*models.User, error) {
var user models.User
result := us.db.Where("email = ?", email).First(&user)
return &user, result.Error
}
func (us *userService) IsEmpty() (bool, error) {
var user models.User
result := us.db.First(&user)
return result.RowsAffected <= 0, result.Error
}
func (us *userService) Create(user models.User) (*models.User, error) {
if strings.TrimSpace(user.Password) == "" {
return &user, util.ErrEmptyPassword
}
user.Email = util.NormalizeEmail(user.Email)
if !govalidator.IsEmail(user.Email) {
return &user, util.ErrInvalidEmail
}
if !IsValid(strings.Split(user.Roles, ",")) {
return &user, util.ErrInvalidRole
}
exists, err := us.GetByEmail(user.Email)
if err == gorm.ErrRecordNotFound {
user.Password, err = us.EncryptPassword(user.Password)
if err != nil {
return &user, err
}
result := us.db.Create(&user)
return &user, result.Error
}
if exists != nil {
return &user, util.ErrEmailAlreadyExists
}
return &user, err
}
func (us *userService) EncryptPassword(password string) (string, error) {
hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
if err != nil {
return "", err
}
return string(hashed), nil
}
func (us *userService) VerifyPassword(hashed, password string) error {
return bcrypt.CompareHashAndPassword([]byte(hashed), []byte(password))
}