优化结构
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"strings"
|
||||
|
||||
"kra/app/system/internal/biz"
|
||||
"kra/app/system/internal/dto"
|
||||
)
|
||||
|
||||
type APIService struct {
|
||||
@@ -24,23 +23,23 @@ func (s *APIService) NormalizeRoutePath(path string) string {
|
||||
return NormalizeRoutePath(path, s.settings.RouterPrefix())
|
||||
}
|
||||
|
||||
func (s *APIService) apiDomain(value *dto.APIRequest) *biz.API {
|
||||
func (s *APIService) apiDomain(value *APIRequest) *biz.API {
|
||||
return &biz.API{ID: value.ID, Path: s.NormalizeRoutePath(value.Path), Description: value.Description, APIGroup: value.APIGroup, Method: value.Method}
|
||||
}
|
||||
|
||||
func apiResponse(value *biz.API) *dto.APIResponse {
|
||||
return &dto.APIResponse{ID: value.ID, CreatedAt: value.CreatedAt, UpdatedAt: value.UpdatedAt, DeletedAt: nil, Path: value.Path, Description: value.Description, APIGroup: value.APIGroup, Method: value.Method}
|
||||
func apiResponse(value *biz.API) *APIResponse {
|
||||
return &APIResponse{ID: value.ID, CreatedAt: value.CreatedAt, UpdatedAt: value.UpdatedAt, DeletedAt: nil, Path: value.Path, Description: value.Description, APIGroup: value.APIGroup, Method: value.Method}
|
||||
}
|
||||
|
||||
func apiResponses(items []*biz.API) []*dto.APIResponse {
|
||||
out := make([]*dto.APIResponse, 0, len(items))
|
||||
func apiResponses(items []*biz.API) []*APIResponse {
|
||||
out := make([]*APIResponse, 0, len(items))
|
||||
for _, item := range items {
|
||||
out = append(out, apiResponse(item))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (s *APIService) ListAPI(ctx context.Context, req *dto.APIListRequest) ([]*dto.APIResponse, int64, error) {
|
||||
func (s *APIService) ListAPI(ctx context.Context, req *APIListRequest) ([]*APIResponse, int64, error) {
|
||||
items, total, err := s.uc.ListAPIs(ctx, req.Page, req.PageSize, &biz.API{Path: req.Path, Description: req.Description, APIGroup: req.APIGroup, Method: req.Method, OrderKey: req.OrderKey, Desc: req.Desc})
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
@@ -48,7 +47,7 @@ func (s *APIService) ListAPI(ctx context.Context, req *dto.APIListRequest) ([]*d
|
||||
return apiResponses(items), total, nil
|
||||
}
|
||||
|
||||
func (s *APIService) AllAPI(ctx context.Context, strict bool) ([]*dto.APIResponse, error) {
|
||||
func (s *APIService) AllAPI(ctx context.Context, strict bool) ([]*APIResponse, error) {
|
||||
items, _, err := s.uc.ListAPIs(ctx, 0, 0, &biz.API{StrictAll: strict})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -80,24 +79,24 @@ func (s *APIService) Groups(ctx context.Context) ([]string, map[string]string, e
|
||||
return groups, groupAPIMap, nil
|
||||
}
|
||||
|
||||
func (s *APIService) CreateAPIRequest(ctx context.Context, req *dto.APIRequest) (*dto.APIResponse, error) {
|
||||
func (s *APIService) CreateAPIRequest(ctx context.Context, req *APIRequest) (*APIResponse, error) {
|
||||
value := s.apiDomain(req)
|
||||
if err := s.uc.CreateAPI(ctx, value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return apiResponse(value), nil
|
||||
}
|
||||
func (s *APIService) UpdateAPIRequest(ctx context.Context, req *dto.APIRequest) error {
|
||||
func (s *APIService) UpdateAPIRequest(ctx context.Context, req *APIRequest) error {
|
||||
return s.uc.UpdateAPI(ctx, s.apiDomain(req))
|
||||
}
|
||||
func (s *APIService) FindAPIResponse(ctx context.Context, id uint) (*dto.APIResponse, error) {
|
||||
func (s *APIService) FindAPIResponse(ctx context.Context, id uint) (*APIResponse, error) {
|
||||
value, err := s.uc.FindAPI(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return apiResponse(value), nil
|
||||
}
|
||||
func (s *APIService) ApplyAPISyncRequest(ctx context.Context, req *dto.ApplyAPISyncRequest) error {
|
||||
func (s *APIService) ApplyAPISyncRequest(ctx context.Context, req *ApplyAPISyncRequest) error {
|
||||
added := make([]*biz.API, 0, len(req.NewAPIs))
|
||||
deleted := make([]*biz.API, 0, len(req.DeleteAPIs))
|
||||
for i := range req.NewAPIs {
|
||||
@@ -108,28 +107,28 @@ func (s *APIService) ApplyAPISyncRequest(ctx context.Context, req *dto.ApplyAPIS
|
||||
}
|
||||
return s.uc.ApplyAPISync(ctx, added, deleted)
|
||||
}
|
||||
func (s *APIService) SetPolicyPathsRequest(ctx context.Context, req *dto.SetPolicyPathsRequest) error {
|
||||
func (s *APIService) SetPolicyPathsRequest(ctx context.Context, req *SetPolicyPathsRequest) error {
|
||||
paths := make([]*biz.API, 0, len(req.Infos))
|
||||
for _, value := range req.Infos {
|
||||
paths = append(paths, &biz.API{Path: value.Path, Method: value.Method})
|
||||
}
|
||||
return s.uc.SetPolicyPaths(ctx, req.AuthorityID, paths)
|
||||
}
|
||||
func (s *APIService) PolicyPathResponses(ctx context.Context, authorityID uint) ([]dto.PolicyPathRequest, error) {
|
||||
func (s *APIService) PolicyPathResponses(ctx context.Context, authorityID uint) ([]PolicyPathRequest, error) {
|
||||
items, err := s.uc.PolicyPaths(ctx, authorityID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var out []dto.PolicyPathRequest
|
||||
var out []PolicyPathRequest
|
||||
if len(items) > 0 {
|
||||
out = make([]dto.PolicyPathRequest, 0, len(items))
|
||||
out = make([]PolicyPathRequest, 0, len(items))
|
||||
}
|
||||
for _, item := range items {
|
||||
out = append(out, dto.PolicyPathRequest{Path: item.Path, Method: item.Method})
|
||||
out = append(out, PolicyPathRequest{Path: item.Path, Method: item.Method})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
func (s *APIService) SyncAPIResponses(ctx context.Context, routes []dto.APIRequest) (*dto.APISyncResponse, error) {
|
||||
func (s *APIService) SyncAPIResponses(ctx context.Context, routes []APIRequest) (*APISyncResponse, error) {
|
||||
items := make([]*biz.API, 0, len(routes))
|
||||
for i := range routes {
|
||||
routes[i].Path = s.NormalizeRoutePath(routes[i].Path)
|
||||
@@ -156,12 +155,12 @@ func (s *APIService) SetAPIRoles(ctx context.Context, path, method string, ids [
|
||||
func (s *APIService) FreshCasbin(ctx context.Context) error {
|
||||
return s.uc.FreshCasbin(ctx)
|
||||
}
|
||||
func (s *APIService) SyncAPIs(ctx context.Context, routes []*biz.API) (*dto.APISyncResponse, error) {
|
||||
func (s *APIService) SyncAPIs(ctx context.Context, routes []*biz.API) (*APISyncResponse, error) {
|
||||
diff, err := s.uc.SyncAPIs(ctx, routes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.APISyncResponse{NewAPIs: apiResponses(diff.Added), DeleteAPIs: apiResponses(diff.Deleted), IgnoreAPIs: apiResponses(diff.Ignored)}, nil
|
||||
return &APISyncResponse{NewAPIs: apiResponses(diff.Added), DeleteAPIs: apiResponses(diff.Deleted), IgnoreAPIs: apiResponses(diff.Ignored)}, nil
|
||||
}
|
||||
func (s *APIService) SetAPIIgnored(ctx context.Context, path, method string, ignored bool) error {
|
||||
return s.uc.SetAPIIgnored(ctx, s.NormalizeRoutePath(path), method, ignored)
|
||||
|
||||
Reference in New Issue
Block a user