-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoints.go
77 lines (64 loc) · 1.84 KB
/
endpoints.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
package x_clone_post_svc
import (
"context"
model "x_clone_post_svc/model"
service "x_clone_post_svc/service"
"github.com/go-kit/kit/endpoint"
)
type Endpoints struct {
CreateEndpoint endpoint.Endpoint
GetByIDEndpoint endpoint.Endpoint
ListEndpoint endpoint.Endpoint
}
type createRequest struct {
Content string `json:"content"`
UserID string `json:"user_id"`
}
type createResponse struct {
Post model.Post `json:"post,omitempty"`
Err error `json:"err,omitempty"`
}
type getByIDRequest struct {
ID string
}
type getByIDResponse struct {
Post model.Post `json:"post,omitempty"`
Err error `json:"err,omitempty"`
}
func (r getByIDResponse) error() error { return r.Err }
type listResponse struct {
Posts []model.Post `json:"posts,omitempty"`
Err error `json:"err,omitempty"`
}
func MakeGetByIDEndpoint(s service.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(getByIDRequest)
p, e := s.GetByID(ctx, req.ID)
return getByIDResponse{Post: p, Err: e}, nil
}
}
func MakeListEndpoint(s service.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
p, e := s.List(ctx)
return listResponse{Posts: p, Err: e}, nil
}
}
func MakeCreateEndpoint(s service.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(createRequest)
p, e := s.Create(ctx, model.Post{
Content: req.Content,
User: model.User{
ID: req.UserID,
},
})
return createResponse{Post: p, Err: e}, nil
}
}
func MakeServerEndpoints(s service.Service) Endpoints {
return Endpoints{
CreateEndpoint: MakeCreateEndpoint(s),
GetByIDEndpoint: MakeGetByIDEndpoint(s),
ListEndpoint: MakeListEndpoint(s),
}
}