forked from lonng/nanoserver
/
endpoint.go
76 lines (64 loc) · 1.89 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
package history
import (
"github.com/go-kit/kit/endpoint"
"golang.org/x/net/context"
"github.com/lonnng/nanoserver/internal/errutil"
"github.com/lonnng/nanoserver/internal/protocol"
)
func makeHistoryLiteListEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
r, ok := request.(*protocol.HistoryLiteListRequest)
if !ok {
return nil, errutil.YXErrWrongType
}
list, t, err := s.HistoryLiteList(r)
if err != nil {
return nil, err
}
return protocol.HistoryLiteListResponse{Data: list, Total: t}, nil
}
}
func makeHistoryListEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
r, ok := request.(*protocol.HistoryListRequest)
if !ok {
return nil, errutil.YXErrWrongType
}
list, t, err := s.HistoryList(r)
if err != nil {
return nil, err
}
return protocol.HistoryListResponse{Data: list, Total: t}, nil
}
}
func makeHistoryByIDEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
r, ok := request.(*protocol.HistoryByIDRequest)
if !ok {
return nil, errutil.YXErrWrongType
}
h, err := s.HistoryByID(r.ID)
if err != nil {
return nil, err
}
return protocol.HistoryByIDResponse{
Data: h,
}, nil
}
}
func makeDeleteHistoryEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
if r, ok := request.(*protocol.DeleteHistoryRequest); ok {
if err := s.DeleteHistory(r.ID); err != nil {
return nil, err
}
return protocol.SuccessResponse, nil
}
return nil, errutil.YXErrWrongType
}
}
func makeGetOptionsEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
return protocol.StringResponse{Data: s.GetOptions()}, nil
}
}