-
Notifications
You must be signed in to change notification settings - Fork 2
/
loginlogic.go
60 lines (49 loc) · 1.52 KB
/
loginlogic.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
package logic
import (
"context"
"github.com/k8scommerce/k8scommerce/services/rpc/customer/internal/svc"
"github.com/k8scommerce/k8scommerce/services/rpc/customer/pb/customer"
"github.com/k8scommerce/k8scommerce/internal/models"
"github.com/k8scommerce/k8scommerce/internal/utils"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/mr"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type LoginLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
return &LoginLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *LoginLogic) Login(in *customer.LoginRequest) (*customer.LoginResponse, error) {
found, err := l.svcCtx.Repo.Customer().Login(in.StoreId, in.Email, in.Password)
if err != nil {
return &customer.LoginResponse{}, nil
}
out := &customer.Customer{}
utils.TransformObj(found, &out)
// fetch the addresses in parallel
err = mr.Finish(func() error {
addresses := getAddressesByKind(l.svcCtx.Repo, out.Id, models.AddressKindBilling)
out.BillingAddresses = addresses
return nil
}, func() error {
addresses := getAddressesByKind(l.svcCtx.Repo, out.Id, models.AddressKindShipping)
out.ShippingAddresses = addresses
return nil
})
if err != nil {
logx.Error(status.Errorf(codes.Internal, "could not fetch addresses in parallel: %s", err.Error()))
}
res := &customer.LoginResponse{
Customer: out,
}
return res, nil
}