refactor: split backend transport and domain modules
This commit is contained in:
+28
-216
@@ -5,6 +5,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"kra/internal/biz"
|
||||
"kra/internal/service/dto"
|
||||
)
|
||||
|
||||
type AccessService struct{ uc *biz.AccessUsecase }
|
||||
@@ -63,32 +64,32 @@ func (s *AccessService) UpdateAuthority(ctx context.Context, v *biz.Authority) e
|
||||
func (s *AccessService) DeleteAuthority(ctx context.Context, id uint) error {
|
||||
return s.uc.Repo().DeleteAuthority(ctx, id)
|
||||
}
|
||||
func (s *AccessService) SetAuthorityMenus(ctx context.Context, id uint, ids []uint) error {
|
||||
return s.uc.Repo().SetAuthorityMenus(ctx, id, ids)
|
||||
func authorityDomain(req *dto.AuthorityRequest) *biz.Authority {
|
||||
return &biz.Authority{AuthorityID: req.AuthorityID, AuthorityName: req.AuthorityName, ParentID: req.ParentID, DataScope: req.DataScope, DefaultRouter: req.DefaultRouter}
|
||||
}
|
||||
func (s *AccessService) AuthorityMenuIDs(ctx context.Context, id uint) ([]uint, error) {
|
||||
return s.uc.Repo().AuthorityMenuIDs(ctx, id)
|
||||
func authorityResponse(value *biz.Authority) *dto.AuthorityResponse {
|
||||
return &dto.AuthorityResponse{AuthorityID: value.AuthorityID, AuthorityName: value.AuthorityName, ParentID: value.ParentID, DataScope: value.DataScope, DefaultRouter: value.DefaultRouter}
|
||||
}
|
||||
func (s *AccessService) AuthorityMenus(ctx context.Context, id uint) ([]map[string]any, error) {
|
||||
ids, err := s.uc.Repo().AuthorityMenuIDs(ctx, id)
|
||||
if err != nil {
|
||||
func (s *AccessService) CreateAuthorityRequest(ctx context.Context, req *dto.AuthorityRequest) (*dto.AuthorityResponse, error) {
|
||||
value := authorityDomain(req)
|
||||
if err := s.CreateAuthority(ctx, value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
selected := make(map[uint]bool, len(ids))
|
||||
for _, value := range ids {
|
||||
selected[value] = true
|
||||
}
|
||||
menus, err := s.uc.Repo().ListMenus(ctx)
|
||||
if err != nil {
|
||||
return authorityResponse(value), nil
|
||||
}
|
||||
func (s *AccessService) CopyAuthorityRequest(ctx context.Context, req *dto.CopyAuthorityRequest) (*dto.AuthorityResponse, error) {
|
||||
value := authorityDomain(&req.Authority)
|
||||
if err := s.CopyAuthority(ctx, req.OldAuthorityID, value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]map[string]any, 0, len(ids))
|
||||
for _, menu := range menus {
|
||||
if selected[menu.ID] {
|
||||
out = append(out, map[string]any{"menuId": menu.ID, "authorityId": id, "sysBaseMenu": convertMenu(menu), "parentId": menu.ParentID})
|
||||
}
|
||||
return authorityResponse(value), nil
|
||||
}
|
||||
func (s *AccessService) UpdateAuthorityRequest(ctx context.Context, req *dto.AuthorityRequest) (*dto.AuthorityResponse, error) {
|
||||
value := authorityDomain(req)
|
||||
if err := s.UpdateAuthority(ctx, value); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
return authorityResponse(value), nil
|
||||
}
|
||||
func (s *AccessService) SetAuthorityUsers(ctx context.Context, id uint, ids []uint) error {
|
||||
return s.uc.Repo().SetAuthorityUsers(ctx, id, ids)
|
||||
@@ -106,127 +107,16 @@ func (s *AccessService) ResolveDataScope(ctx context.Context, authorityID, userI
|
||||
return s.uc.Repo().ResolveDataScope(ctx, authorityID, userID)
|
||||
}
|
||||
|
||||
func (s *AccessService) Menus(ctx context.Context) ([]map[string]any, error) {
|
||||
items, err := s.uc.Repo().ListMenus(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]map[string]any, 0, len(items))
|
||||
for _, v := range items {
|
||||
v.Buttons, err = s.uc.Repo().Buttons(ctx, v.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, convertMenu(v))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
func (s *AccessService) MenuTree(ctx context.Context) ([]map[string]any, error) {
|
||||
items, err := s.uc.Repo().ListMenus(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
byID := make(map[uint]*biz.Menu, len(items))
|
||||
for _, item := range items {
|
||||
item.Buttons, err = s.uc.Repo().Buttons(ctx, item.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
item.Children = []*biz.Menu{}
|
||||
byID[item.ID] = item
|
||||
}
|
||||
roots := make([]*biz.Menu, 0)
|
||||
for _, item := range items {
|
||||
if parent := byID[item.ParentID]; parent != nil {
|
||||
parent.Children = append(parent.Children, item)
|
||||
} else {
|
||||
roots = append(roots, item)
|
||||
}
|
||||
}
|
||||
out := make([]map[string]any, 0, len(roots))
|
||||
for _, item := range roots {
|
||||
out = append(out, convertMenu(item))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
func (s *AccessService) CreateMenu(ctx context.Context, v *biz.Menu) error {
|
||||
return s.uc.Repo().CreateMenu(ctx, v)
|
||||
}
|
||||
func (s *AccessService) UpdateMenu(ctx context.Context, v *biz.Menu) error {
|
||||
return s.uc.Repo().UpdateMenu(ctx, v)
|
||||
}
|
||||
func (s *AccessService) DeleteMenu(ctx context.Context, id uint) error {
|
||||
return s.uc.Repo().DeleteMenu(ctx, id)
|
||||
}
|
||||
func (s *AccessService) Menu(ctx context.Context, id uint) (map[string]any, error) {
|
||||
v, err := s.uc.Repo().FindMenu(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
v.Buttons, err = s.uc.Repo().Buttons(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return convertMenu(v), nil
|
||||
}
|
||||
func (s *AccessService) MenuRoleIDs(ctx context.Context, id uint) ([]uint, error) {
|
||||
return s.uc.Repo().MenuRoleIDs(ctx, id)
|
||||
}
|
||||
func (s *AccessService) SetMenuRoles(ctx context.Context, id uint, ids []uint) error {
|
||||
return s.uc.Repo().SetMenuRoles(ctx, id, ids)
|
||||
}
|
||||
func (s *AccessService) ReplaceMenuButtons(ctx context.Context, menuID uint, buttons []*biz.MenuButton) error {
|
||||
return s.uc.Repo().ReplaceMenuButtons(ctx, menuID, buttons)
|
||||
}
|
||||
|
||||
func (s *AccessService) APIs(ctx context.Context, page, size int, q *biz.API) ([]map[string]any, int64, error) {
|
||||
items, total, err := s.uc.Repo().ListAPIs(ctx, page, size, q)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out := make([]map[string]any, 0, len(items))
|
||||
for _, v := range items {
|
||||
out = append(out, apiDTO(v))
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
func (s *AccessService) CreateAPI(ctx context.Context, v *biz.API) error {
|
||||
return s.uc.Repo().CreateAPI(ctx, v)
|
||||
}
|
||||
func (s *AccessService) UpdateAPI(ctx context.Context, v *biz.API) error {
|
||||
return s.uc.Repo().UpdateAPI(ctx, v)
|
||||
}
|
||||
func (s *AccessService) DeleteAPIs(ctx context.Context, ids []uint) error {
|
||||
return s.uc.Repo().DeleteAPIs(ctx, ids)
|
||||
}
|
||||
func (s *AccessService) API(ctx context.Context, id uint) (map[string]any, error) {
|
||||
v, err := s.uc.Repo().FindAPI(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return apiDTO(v), nil
|
||||
}
|
||||
func (s *AccessService) APIRoleIDs(ctx context.Context, id uint) ([]uint, error) {
|
||||
return s.uc.Repo().APIRoleIDs(ctx, id)
|
||||
}
|
||||
func (s *AccessService) SetAPIRoles(ctx context.Context, id uint, ids []uint) error {
|
||||
return s.uc.Repo().SetAPIRoles(ctx, id, ids)
|
||||
}
|
||||
func (s *AccessService) PolicyPaths(ctx context.Context, id uint) ([]map[string]any, error) {
|
||||
items, err := s.uc.Repo().PolicyPaths(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]map[string]any, 0, len(items))
|
||||
for _, v := range items {
|
||||
out = append(out, map[string]any{"path": v.Path, "method": v.Method})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
func (s *AccessService) SetPolicyPaths(ctx context.Context, id uint, paths []*biz.API) error {
|
||||
return s.uc.Repo().SetPolicyPaths(ctx, id, paths)
|
||||
}
|
||||
func (s *AccessService) SyncAPIs(ctx context.Context, routes []*biz.API) (map[string]any, error) {
|
||||
func (s *AccessService) SyncAPIs(ctx context.Context, routes []*biz.API) (*dto.APISyncResponse, error) {
|
||||
stored, _, err := s.uc.Repo().ListAPIs(ctx, 1, 100000, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -250,31 +140,27 @@ func (s *AccessService) SyncAPIs(ctx context.Context, routes []*biz.API) (map[st
|
||||
for _, item := range stored {
|
||||
storedSet[key(item)] = item
|
||||
}
|
||||
added := make([]map[string]any, 0)
|
||||
deleted := make([]map[string]any, 0)
|
||||
ignoredDTO := make([]map[string]any, 0, len(ignored))
|
||||
added := make([]*dto.APIResponse, 0)
|
||||
deleted := make([]*dto.APIResponse, 0)
|
||||
ignoredDTO := make([]*dto.APIResponse, 0, len(ignored))
|
||||
for k, item := range routeSet {
|
||||
if storedSet[k] == nil {
|
||||
added = append(added, apiDTO(item))
|
||||
added = append(added, apiResponse(item))
|
||||
}
|
||||
}
|
||||
for k, item := range storedSet {
|
||||
if routeSet[k] == nil && !ignoreSet[k] {
|
||||
deleted = append(deleted, apiDTO(item))
|
||||
deleted = append(deleted, apiResponse(item))
|
||||
}
|
||||
}
|
||||
for _, item := range ignored {
|
||||
ignoredDTO = append(ignoredDTO, apiDTO(item))
|
||||
ignoredDTO = append(ignoredDTO, apiResponse(item))
|
||||
}
|
||||
return map[string]any{"newApis": added, "deleteApis": deleted, "ignoreApis": ignoredDTO}, nil
|
||||
return &dto.APISyncResponse{NewAPIs: added, DeleteAPIs: deleted, IgnoreAPIs: ignoredDTO}, nil
|
||||
}
|
||||
func (s *AccessService) SetAPIIgnored(ctx context.Context, path, method string, ignored bool) error {
|
||||
return s.uc.Repo().SetAPIIgnored(ctx, path, method, ignored)
|
||||
}
|
||||
func (s *AccessService) ApplyAPISync(ctx context.Context, added, deleted []*biz.API) error {
|
||||
return s.uc.Repo().ApplyAPISync(ctx, added, deleted)
|
||||
}
|
||||
|
||||
func (s *AccessService) Buttons(ctx context.Context, id uint) ([]map[string]any, error) {
|
||||
items, err := s.uc.Repo().Buttons(ctx, id)
|
||||
if err != nil {
|
||||
@@ -301,77 +187,3 @@ func (s *AccessService) SetSelectedButtons(ctx context.Context, aid, menuID uint
|
||||
func (s *AccessService) CanRemoveButton(ctx context.Context, id uint) (bool, error) {
|
||||
return s.uc.Repo().CanRemoveButton(ctx, id)
|
||||
}
|
||||
|
||||
func (s *AccessService) Departments(ctx context.Context, name string) ([]map[string]any, error) {
|
||||
items, err := s.uc.Repo().ListDepartments(ctx, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]map[string]any, 0, len(items))
|
||||
for _, v := range items {
|
||||
out = append(out, departmentDTO(v))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
func (s *AccessService) CreateDepartment(ctx context.Context, v *biz.Department) error {
|
||||
return s.uc.Repo().CreateDepartment(ctx, v)
|
||||
}
|
||||
func (s *AccessService) UpdateDepartment(ctx context.Context, v *biz.Department) error {
|
||||
return s.uc.Repo().UpdateDepartment(ctx, v)
|
||||
}
|
||||
func (s *AccessService) DeleteDepartment(ctx context.Context, id uint) error {
|
||||
return s.uc.Repo().DeleteDepartment(ctx, id)
|
||||
}
|
||||
func (s *AccessService) Department(ctx context.Context, id uint) (map[string]any, error) {
|
||||
v, err := s.uc.Repo().FindDepartment(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return departmentDTO(v), nil
|
||||
}
|
||||
func (s *AccessService) DepartmentUserIDs(ctx context.Context, id uint) ([]uint, error) {
|
||||
return s.uc.Repo().DepartmentUserIDs(ctx, id)
|
||||
}
|
||||
func (s *AccessService) SetDepartmentUsers(ctx context.Context, id uint, ids []uint) error {
|
||||
return s.uc.Repo().SetDepartmentUsers(ctx, id, ids)
|
||||
}
|
||||
func (s *AccessService) SetUserDepartments(ctx context.Context, id uint, ids []uint, primary uint) error {
|
||||
return s.uc.Repo().SetUserDepartments(ctx, id, ids, primary)
|
||||
}
|
||||
|
||||
func (s *AccessService) Positions(ctx context.Context, page, size int, q *biz.Position) ([]map[string]any, int64, error) {
|
||||
items, total, err := s.uc.Repo().ListPositions(ctx, page, size, q)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
out := make([]map[string]any, 0, len(items))
|
||||
for _, v := range items {
|
||||
out = append(out, positionDTO(v))
|
||||
}
|
||||
return out, total, nil
|
||||
}
|
||||
func (s *AccessService) CreatePosition(ctx context.Context, v *biz.Position) error {
|
||||
return s.uc.Repo().CreatePosition(ctx, v)
|
||||
}
|
||||
func (s *AccessService) UpdatePosition(ctx context.Context, v *biz.Position) error {
|
||||
return s.uc.Repo().UpdatePosition(ctx, v)
|
||||
}
|
||||
func (s *AccessService) DeletePosition(ctx context.Context, id uint) error {
|
||||
return s.uc.Repo().DeletePosition(ctx, id)
|
||||
}
|
||||
func (s *AccessService) Position(ctx context.Context, id uint) (map[string]any, error) {
|
||||
v, err := s.uc.Repo().FindPosition(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return positionDTO(v), nil
|
||||
}
|
||||
func (s *AccessService) PositionUserIDs(ctx context.Context, id uint) ([]uint, error) {
|
||||
return s.uc.Repo().PositionUserIDs(ctx, id)
|
||||
}
|
||||
func (s *AccessService) SetPositionUsers(ctx context.Context, id uint, ids []uint) error {
|
||||
return s.uc.Repo().SetPositionUsers(ctx, id, ids)
|
||||
}
|
||||
func (s *AccessService) SetUserPositions(ctx context.Context, id uint, ids []uint) error {
|
||||
return s.uc.Repo().SetUserPositions(ctx, id, ids)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user