/
user.go
58 lines (47 loc) · 1.41 KB
/
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
package biz
import (
"context"
"github.com/go-kratos/kratos/v2/log"
)
type User struct {
Id int64
Username string
Password string
}
type UserRepo interface {
CreateUser(ctx context.Context, u *User) (*User, error)
GetUser(ctx context.Context, id int64) (*User, error)
VerifyPassword(ctx context.Context, u *User) (bool, error)
FindByUsername(ctx context.Context, username string) (*User, error)
UpdateUser(ctx context.Context, u *User) (*User, error)
}
type UserUseCase struct {
repo UserRepo
log *log.Helper
}
func NewUserUseCase(repo UserRepo, logger log.Logger) *UserUseCase {
return &UserUseCase{repo: repo, log: log.NewHelper(log.With(logger, "module", "usecase/user"))}
}
func (uc *UserUseCase) Create(ctx context.Context, u *User) (*User, error) {
return uc.repo.CreateUser(ctx, u)
}
func (uc *UserUseCase) Get(ctx context.Context, id int64) (*User, error) {
return uc.repo.GetUser(ctx, id)
}
func (uc *UserUseCase) GetUserByUsername(ctx context.Context, username string) (*User, error) {
return uc.repo.FindByUsername(ctx, username)
}
func (uc *UserUseCase) VerifyPassword(ctx context.Context, u *User) (bool, error) {
return uc.repo.VerifyPassword(ctx, u)
}
func (uc *UserUseCase) Save(ctx context.Context, u *User) (*User, error) {
_, err := uc.repo.GetUser(ctx, u.Id)
if err != nil {
return nil, err
}
p, err := uc.repo.UpdateUser(ctx, u)
if p.Id == 0 {
return nil, err
}
return p, nil
}