-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
89 lines (82 loc) · 2.43 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
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
package service
import (
"context"
"errors"
v1 "github.com/jackycsl/catalog/api/user/service/v1"
"github.com/jackycsl/catalog/pkg/util/helper"
"github.com/jackycsl/catalog/user-service/internal/biz"
)
func (s *UserService) CreateUser(ctx context.Context, req *v1.CreateUserReq) (*v1.CreateUserReply, error) {
rv, err := s.uc.Create(ctx, &biz.User{
Username: req.Username,
Password: req.Password,
})
if err != nil {
return nil, v1.ErrorUnknownError(err.Error())
}
return &v1.CreateUserReply{
Id: rv.Id,
Username: rv.Username,
}, nil
}
func (s *UserService) GetUser(ctx context.Context, req *v1.GetUserReq) (*v1.GetUserReply, error) {
rv, err := s.uc.Get(ctx, req.Id)
if err != nil {
switch {
case errors.Is(err, helper.ErrUserNotFound):
return nil, v1.ErrorUserNotFound("User %d not found", req.Id)
default:
return nil, v1.ErrorUnknownError(err.Error())
}
}
return &v1.GetUserReply{
Id: rv.Id,
Username: rv.Username,
}, nil
}
func (s *UserService) VerifyPassword(ctx context.Context, req *v1.VerifyPasswordReq) (*v1.VerifyPasswordReply, error) {
rv, err := s.uc.VerifyPassword(ctx, &biz.User{Username: req.Username, Password: req.Password})
if err != nil {
switch {
case errors.Is(err, helper.ErrUserNotFound):
return nil, v1.ErrorUserNotFound("User %d not found", req.Username)
default:
return nil, v1.ErrorUnknownError(err.Error())
}
}
if !rv {
return nil, v1.ErrorLoginFailed("Incorrect Password")
}
return &v1.VerifyPasswordReply{
Ok: rv,
}, nil
}
func (s *UserService) GetUserByUsername(ctx context.Context, req *v1.GetUserByUsernameReq) (*v1.GetUserByUsernameReply, error) {
rv, err := s.uc.GetUserByUsername(ctx, req.Username)
if err != nil {
switch {
case errors.Is(err, helper.ErrUserNotFound):
return nil, v1.ErrorUserNotFound("User %s not found", req.Username)
default:
return nil, v1.ErrorUnknownError(err.Error())
}
}
return &v1.GetUserByUsernameReply{
Id: rv.Id,
Username: rv.Username,
}, nil
}
func (s *UserService) Save(ctx context.Context, req *v1.SaveUserReq) (*v1.SaveUserReply, error) {
rv, err := s.uc.Save(ctx, &biz.User{Id: req.Id, Username: req.Username, Password: req.Password})
if err != nil {
switch {
case errors.Is(err, helper.ErrUserNotFound):
return nil, v1.ErrorUserNotFound("User %d not found", req.Username)
default:
return nil, v1.ErrorUnknownError(err.Error())
}
}
return &v1.SaveUserReply{
Id: rv.Id,
}, nil
}