Ejemplo n.º 1
0
        public async Task <UpdateOperationOutput> Update(UpdateOperationInput input)
        {
            var operation = await _operationRepository.SingleOrDefaultAsync(p => p.Id == input.Id);

            if (operation == null)
            {
                throw new BusinessException($"不存在Id为{input.Id}的操作信息");
            }
            var permission = await _permissionRepository.SingleOrDefaultAsync(p => p.Id == operation.PermissionId);

            if (permission == null)
            {
                throw new BusinessException($"不存在Id为{operation.PermissionId}的权限信息");
            }
            var menu = await _menuRepository.SingleOrDefaultAsync(p => p.Id == operation.MenuId);

            if (menu == null)
            {
                throw new BusinessException($"不存在Id为{operation.MenuId}的菜单信息");
            }
            operation  = input.MapTo(operation);
            permission = input.MapTo(permission);

            await UnitOfWorkAsync(async (conn, trans) =>
            {
                await _permissionRepository.UpdateAsync(permission, conn, trans);
                await _operationRepository.UpdateAsync(operation, conn, trans);
                await _operationActionRepository.DeleteAsync(p => p.OperationId == permission.Id, conn, trans);
                if (input.ActionIds != null && input.ActionIds.Any())
                {
                    foreach (var actionId in input.ActionIds)
                    {
                        var action = await _actionRepository.SingleOrDefaultAsync(p => p.Id == actionId);
                        if (action == null)
                        {
                            throw new BusinessException($"系统中不存在Id为{actionId}的方法");
                        }
                        var operationAction = new OperationActionRelation()
                        {
                            ActionId = actionId, OperationId = operation.Id, ServiceId = action.ServiceId
                        };
                        await _operationActionRepository.InsertAsync(operationAction, conn, trans);
                    }
                }
            }, Connection);

            return(new UpdateOperationOutput()
            {
                Id = operation.Id, PermissionId = operation.PermissionId, Tips = "更新操作成功"
            });
        }
Ejemplo n.º 2
0
        public async Task <CreateOperationOutput> Create(CreateOperationInput input)
        {
            var exsitOperation = await _operationRepository.FirstOrDefaultAsync(p => p.Name == input.Name);

            if (exsitOperation != null)
            {
                throw new BusinessException($"系统中已经存在{input.Name}的操作,请勿重复添加");
            }
            var menu = await _menuRepository.SingleOrDefaultAsync(p => p.PermissionId == input.PermissionId);

            if (menu == null)
            {
                throw new BusinessException($"不存在PermissionId为{input.PermissionId}的菜单信息");
            }
            var operation  = input.MapTo <Operation>();
            var permission = input.MapTo <Permission>();

            var operationCount = await _operationRepository.GetCountAsync(p => p.MenuId == input.PermissionId);

            operation.Code  = menu.Code + HeroConstants.CodeRuleRestrain.CodeSeparator + (operationCount + 1).ToString().PadLeft(HeroConstants.CodeRuleRestrain.CodeCoverBit, HeroConstants.CodeRuleRestrain.CodeCoverSymbol);
            operation.Level = menu.Level + 1;

            await UnitOfWorkAsync(async (conn, trans) =>
            {
                var permissionId       = await _permissionRepository.InsertAndGetIdAsync(permission, conn, trans);
                operation.PermissionId = permissionId;
                operation.MenuId       = menu.Id;
                var operationId        = await _operationRepository.InsertAndGetIdAsync(operation, conn, trans);
                if (input.ActionIds != null && input.ActionIds.Any())
                {
                    foreach (var actionId in input.ActionIds)
                    {
                        var action = await _actionRepository.SingleOrDefaultAsync(p => p.Id == actionId);
                        if (action == null)
                        {
                            throw new BusinessException($"系统中不存在Id为{actionId}的方法");
                        }
                        var operationAction = new OperationActionRelation()
                        {
                            ActionId = actionId, OperationId = operationId, ServiceId = action.ServiceId
                        };
                        await _operationActionRepository.InsertAsync(operationAction, conn, trans);
                    }
                }
            }, Connection);

            return(new CreateOperationOutput()
            {
                Id = operation.Id, PermissionId = operation.PermissionId, Tips = "新增操作成功"
            });
        }
Ejemplo n.º 3
0
        public async Task <UpdateOperationOutput> Update(UpdateOperationInput input)
        {
            var operation = await _operationRepository.SingleOrDefaultAsync(p => p.Id == input.Id);

            if (operation == null)
            {
                throw new BusinessException($"不存在Id为{input.Id}的操作信息");
            }
            var permission = await _permissionRepository.SingleOrDefaultAsync(p => p.Id == operation.PermissionId);

            if (permission == null)
            {
                throw new BusinessException($"不存在Id为{operation.PermissionId}的权限信息");
            }
            var menu = await _menuRepository.SingleOrDefaultAsync(p => p.Id == operation.MenuId);

            if (menu == null)
            {
                throw new BusinessException($"不存在Id为{operation.MenuId}的菜单信息");
            }
            operation = input.MapTo(operation);
            //permission = input.MapTo(permission);
            permission.Name = input.Name;
            permission.Memo = input.Memo;

            using (var locker = await _lockerProvider.CreateLockAsync("UpdateOperation"))
            {
                return(await locker.Lock(async() =>
                {
                    await UnitOfWorkAsync(async(conn, trans) =>
                    {
                        await _permissionRepository.UpdateAsync(permission, conn, trans);
                        await _operationRepository.UpdateAsync(operation, conn, trans);
                        await _operationActionRepository.DeleteAsync(p => p.OperationId == operation.Id, conn, trans);
                        if (input.ActionIds != null && input.ActionIds.Any())
                        {
                            foreach (var actionId in input.ActionIds)
                            {
                                var action =
                                    await _actionRepository.SingleOrDefaultAsync(p => p.Id == actionId, conn, trans);
                                if (action == null)
                                {
                                    throw new BusinessException($"系统中不存在Id为{actionId}的方法");
                                }
                                if (operation.Mold == OperationMold.Look || operation.Mold == OperationMold.Query)
                                {
                                    if (await _operationActionRepository.GetCountAsync(p =>
                                                                                       p.ServiceId == action.ServiceId, conn, trans) > 1)
                                    {
                                        throw new BusinessException($"一个查询接口仅被允许分配给一个操作");
                                    }
                                }
                                var operationAction = new OperationActionRelation
                                {
                                    ActionId = actionId, OperationId = operation.Id, ServiceId = action.ServiceId
                                };
                                await _operationActionRepository.InsertAsync(operationAction, conn, trans);
                            }
                        }
                    }, Connection);
                    return new UpdateOperationOutput
                    {
                        Id = operation.Id, PermissionId = operation.PermissionId, Tips = "更新操作成功"
                    };
                }));
            }
        }