forked from lonng/nanoserver
/
endpoint.go
113 lines (101 loc) · 2.86 KB
/
endpoint.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
package order
import (
"github.com/go-kit/kit/endpoint"
"golang.org/x/net/context"
"github.com/lonnng/nanoserver/internal/errutil"
"github.com/lonnng/nanoserver/internal/protocol"
"github.com/lonnng/nanoserver/internal/types"
)
func makeCreateOrderEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
r, ok := request.(*protocol.CreateOrderRequest)
if !ok {
return nil, errutil.YXErrWrongType
}
// todo: check user exists
/*if !cache.Exists(r.Token) {
return nil, errutil.YXErrTokenNotFound
}*/
return s.CreateOrder(r)
}
}
func makeYXPayOrderListEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
if r, ok := request.(*protocol.PayOrderListRequest); ok {
list, total, err := s.YXPayOrderList(r)
if err != nil {
return nil, err
}
return protocol.PayOrderListResponse{
Data: list,
Total: total,
}, nil
}
return nil, errutil.YXErrWrongType
}
}
func makeOrderListEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
if r, ok := request.(*protocol.OrderListRequest); ok {
list, total, err := s.OrderList(r)
if err != nil {
return nil, err
}
return protocol.OrderListResponse{
Data: list,
Total: total,
}, nil
}
return nil, errutil.YXErrWrongType
}
}
func makeTradeListEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
if r, ok := request.(*protocol.TradeListRequest); ok {
list, total, err := s.TradeList(r)
if err != nil {
return nil, err
}
return protocol.TradeListResponse{
Data: list,
Total: total,
}, nil
}
return nil, errutil.YXErrWrongType
}
}
func makeBalanceListEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
if r, ok := request.(*protocol.BalanceListRequest); ok {
m, err := s.BalanceList(r.Uids)
if err != nil {
return nil, err
}
return protocol.BalanceListResponse{
Data: m,
}, nil
}
return nil, errutil.YXErrWrongType
}
}
func makeWechatOrderCallbackEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
r, ok := request.(*protocol.WechatOrderCallbackRequest)
if !ok {
return nil, errutil.YXErrWrongType
}
resp, err := s.Notify(types.YX, &protocol.UnifyOrderCallbackRequest{
PayPlatform: types.Wechat,
RawRequest: r,
})
if err != nil {
return protocol.ErrorResponse{Error: err.Error()}, nil
}
return resp, nil
}
}
func makeGetOptionsEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
return protocol.StringResponse{Data: s.GetOptions()}, nil
}
}