permission.go 1.7 KB
package service

import (
	"github.com/linmadan/egglib-go/core/application"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/factory"
	"gitlab.fjmaimaimai.com/mmm-go-pp/partner01/pkg/application/permission/query"
)

// 权限服务
type PermissionService struct {
}

// 根据当前用户返回权限列表
func (permissionService *PermissionService) Menus(menusQuery *query.MenusQuery) (interface{}, error) {
	if err := menusQuery.ValidateQuery(); err != nil {
		return nil, application.ThrowError(application.ARG_ERROR, err.Error())
	}
	transactionContext, err := factory.CreateTransactionContext(nil)
	if err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	if err := transactionContext.StartTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	defer func() {
		transactionContext.RollbackTransaction()
	}()

	PermissionRepository, err := factory.CreatePermissionRepository(map[string]interface{}{"transactionContext": transactionContext})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	total, permissions, err := PermissionRepository.Find(map[string]interface{}{})
	if err != nil {
		return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
	}
	if err := transactionContext.CommitTransaction(); err != nil {
		return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
	}
	return map[string]interface{}{"total": total, "permissions": permissions}, nil
}

func NewPermissionService(options map[string]interface{}) *PermissionService {
	newPermissionService := &PermissionService{}
	return newPermissionService
}