コード例 #1
0
        public async Task <IResultModel> Sync(ButtonSyncModel model)
        {
            if (model.Buttons == null || !model.Buttons.Any())
            {
                return(ResultModel.Failed("请选择按钮"));
            }

            var menu = await _menuRepository.GetAsync(model.MenuId);

            if (menu == null)
            {
                return(ResultModel.Failed("菜单不存在"));
            }

            foreach (var info in model.Buttons)
            {
                if (info.Name.IsNull())
                {
                    return(ResultModel.Failed("按钮名称不能为空"));
                }

                if (info.Code.IsNull())
                {
                    return(ResultModel.Failed($"按钮({info.Name})编码不能为空"));
                }
            }

            using (var tran = _buttonRepository.BeginTransaction())
            {
                foreach (var button in model.Buttons)
                {
                    button.MenuId = model.MenuId;
                    button.Code   = button.Code.ToLower();

                    if (!await _buttonRepository.Exists(button, tran))
                    {
                        if (!await _buttonRepository.AddAsync(button, tran))
                        {
                            tran.Rollback();
                            return(ResultModel.Failed("同步失败"));
                        }
                    }
                    else
                    {
                        if (!await _buttonRepository.UpdateForSync(button, tran))
                        {
                            tran.Rollback();
                            return(ResultModel.Failed("同步失败"));
                        }
                    }
                }

                tran.Commit();
            }

            return(ResultModel.Success());
        }
コード例 #2
0
ファイル: ButtonService.cs プロジェクト: zjmsky/NetModular
        public async Task <IResultModel> Add(ButtonAddModel model)
        {
            var menu = await _menuRepository.GetAsync(model.MenuId);

            if (menu == null)
            {
                return(ResultModel.Failed("菜单不存在"));
            }

            if (await _buttonRepository.Exists(model.Code))
            {
                return(ResultModel.Failed("编码已存在"));
            }

            var button = _mapper.Map <Button>(model);

            button.Code = button.Code.ToLower();
            var result = await _buttonRepository.AddAsync(button);

            return(ResultModel.Result(result));
        }
コード例 #3
0
        /// <summary>
        /// 同步按钮
        /// </summary>
        /// <param name="menu"></param>
        /// <param name="buttons"></param>
        /// <param name="transaction"></param>
        /// <returns></returns>
        private async Task <bool> SyncButton(MenuEntity menu, List <MenuButtonAddModel> buttons, IDbTransaction transaction)
        {
            if (menu.Type != MenuType.Route)
            {
                return(true);
            }

            if (buttons == null)
            {
                buttons = new List <MenuButtonAddModel>();
            }

            var oldButtons = await _buttonRepository.QueryByMenu(menu.RouteName, transaction);

            #region ==更新以及新增按钮==

            foreach (var newBtn in buttons)
            {
                var  oldBtn = oldButtons.FirstOrDefault(m => m.Code.EqualsIgnoreCase(newBtn.Code));
                bool result;
                if (oldBtn != null)
                {
                    oldBtn.Name = newBtn.Text;
                    oldBtn.Icon = newBtn.Icon;
                    result      = await _buttonRepository.UpdateAsync(oldBtn, transaction);
                }
                else
                {
                    oldBtn = new ButtonEntity
                    {
                        MenuCode = menu.RouteName,
                        Code     = newBtn.Code.ToLower(),
                        Icon     = newBtn.Icon,
                        Name     = newBtn.Text
                    };
                    result = await _buttonRepository.AddAsync(oldBtn, transaction);
                }

                if (!result)
                {
                    return(false);
                }

                if (!await SyncButtonPermission(oldBtn, newBtn.Permissions, transaction))
                {
                    return(false);
                }
            }

            #endregion

            foreach (var oldBtn in oldButtons)
            {
                var isDel = !buttons.Any(m => m.Code.EqualsIgnoreCase(oldBtn.Code));
                if (isDel)
                {
                    if (!await _buttonRepository.DeleteAsync(oldBtn.Id, transaction) || !await _buttonPermissionRepository.DeleteByButton(oldBtn.Code, transaction))
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
コード例 #4
0
        public async Task PressButton(string userId, int timeSpan)
        {
            await _repository.AddAsync(userId, TimeSpan.FromSeconds(timeSpan));

            await Clients.All.SendAsync("ButtonPressed");
        }