/
create_user.go
71 lines (62 loc) · 1.5 KB
/
create_user.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
package user
import (
"context"
"gorm.io/gorm"
"github.com/borntodie-new/todo-list-backup/constant"
"github.com/borntodie-new/todo-list-backup/model"
"github.com/borntodie-new/todo-list-backup/utils"
)
type CreateUserFlow struct {
// global data
ctx context.Context
db *gorm.DB
// request data
Username string
Password string
Email string
Avatar string
// temporarily data
// response data
}
func CreateUser(username, password, email, avatar string, ctx context.Context, db *gorm.DB) error {
return NewCreateUserFlow(username, password, email, avatar, ctx, db).Do()
}
func NewCreateUserFlow(username, password, email, avatar string, ctx context.Context, db *gorm.DB) *CreateUserFlow {
return &CreateUserFlow{
ctx: ctx,
db: db,
Username: username,
Password: password,
Email: email,
Avatar: avatar,
}
}
func (f *CreateUserFlow) Do() error {
if err := f.checkParam(); err != nil {
return err
}
if err := f.prepareData(); err != nil {
return err
}
return nil
}
func (f *CreateUserFlow) checkParam() error {
if f.Username == "" || f.Password == "" || f.Email == "" {
return constant.ParamErr
}
if f.Avatar == "" {
f.Avatar = constant.DefaultAvatarAddress
}
// 加密密码
f.Password = utils.Default().GenPassword(f.Password)
return nil
}
func (f *CreateUserFlow) prepareData() error {
user := &model.User{
Username: f.Username,
Password: f.Password,
Email: f.Email,
Avatar: f.Avatar,
}
return model.NewUserDao(f.ctx, f.db).CreateInstance(user)
}