/
api.go
83 lines (73 loc) · 2.07 KB
/
api.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
package service
import (
"context"
"google.golang.org/protobuf/types/known/emptypb"
v1 "github.com/ZQCard/kbk-authorization/api/authorization/v1"
"github.com/ZQCard/kbk-authorization/internal/domain"
)
func (s *AuthorizationService) GetApiListAll(ctx context.Context, req *emptypb.Empty) (*v1.GetApiListAllRes, error) {
list, err := s.apiUsecase.ListApiAll(ctx)
if err != nil {
return nil, err
}
res := &v1.GetApiListAllRes{}
for _, v := range list {
res.List = append(res.List, toPbApi(v))
}
return res, nil
}
func (s *AuthorizationService) GetApiList(ctx context.Context, req *v1.GetApiListReq) (*v1.GetApiListPageRes, error) {
params := make(map[string]interface{})
params["name"] = req.Name
params["method"] = req.Method
params["path"] = req.Path
params["group"] = req.Group
list, count, err := s.apiUsecase.ListApi(ctx, req.Page, req.PageSize, params)
if err != nil {
return nil, err
}
res := &v1.GetApiListPageRes{}
res.Total = int64(count)
for _, v := range list {
res.List = append(res.List, toPbApi(v))
}
return res, nil
}
func (s *AuthorizationService) CreateApi(ctx context.Context, req *v1.CreateApiReq) (*v1.Api, error) {
res, err := s.apiUsecase.CreateApi(ctx, &domain.Api{
Name: req.Name,
Group: req.Group,
Method: req.Method,
Path: req.Path,
})
if err != nil {
return nil, err
}
return toPbApi(res), nil
}
func (s *AuthorizationService) UpdateApi(ctx context.Context, req *v1.UpdateApiReq) (*emptypb.Empty, error) {
return &emptypb.Empty{}, s.apiUsecase.UpdateApi(ctx, &domain.Api{
Id: req.Id,
Name: req.Name,
Group: req.Group,
Method: req.Method,
Path: req.Path,
})
}
func (s *AuthorizationService) DeleteApi(ctx context.Context, req *v1.DeleteApiReq) (*emptypb.Empty, error) {
return &emptypb.Empty{}, s.apiUsecase.DeleteApi(ctx, &domain.Api{
Id: req.Id,
})
}
func toPbApi(api *domain.Api) *v1.Api {
return &v1.Api{
Id: api.Id,
Domain: api.Domain,
Name: api.Name,
Group: api.Group,
Method: api.Method,
Path: api.Path,
CreatedAt: api.CreatedAt,
UpdatedAt: api.UpdatedAt,
}
}