public void Update(MenuDTO menuDTO)
        {
            //get persisted item
            var persisted = _Repository.Get(menuDTO.Id);

            if (persisted != null) //if customer exist
            {
                var current = menuDTO.ToModel();
                current.Created = persisted.Created;    //不修改创建时间

                if (current.Name.IsNullOrBlank())
                {
                    throw new DataExistsException(UserSystemResource.Common_Name_Empty);
                }

                if (_Repository.Exists(current))
                {
                    throw new DataExistsException(UserSystemResource.Menu_Exists);
                }

                //Merge changes
                _Repository.Merge(persisted, current);

                //commit unit of work
                _Repository.UnitOfWork.Commit();
            }
            else
            {
                // Not Exists
            }
        }
Esempio n. 2
0
        public MenuDTO Add(MenuDTO menuDTO)
        {
            var menu = menuDTO.ToModel();

            menu.Id      = IdentityGenerator.NewSequentialGuid();
            menu.Created = DateTime.UtcNow;
            if (menu.Name.IsNullOrBlank())
            {
                throw new DataExistsException(UserSystemResource.Common_Name_Empty);
            }
            if (_Repository.Exists(menu))
            {
                throw new DataExistsException(UserSystemResource.Menu_Exists);
            }
            _Repository.Add(menu);
            //commit the unit of work
            _Repository.UnitOfWork.Commit();
            return(menu.ToDto());
        }
        public MenuDTO Add(MenuDTO menuDTO)
        {
            var menu = menuDTO.ToModel();

            menu.Id      = IdentityGenerator.NewSequentialGuid();
            menu.Created = DateTime.UtcNow;
            if (menuDTO.Module != null)
            {
                menu.Module = _ModuleRepository.Get(menuDTO.Module.Id);
            }
            if (menu.Parent == null || menu.Parent.Id == Guid.Empty)
            {
                menu.Depth = 1;
            }
            else
            {
                menu.Parent = _Repository.Get(menuDTO.Parent.Id);
                menu.Depth  = menu.Parent.Depth + 1;
            }

            if (menu.Name.IsNullOrBlank())
            {
                throw new DefinedException(UserSystemMessagesResources.Name_Empty);
            }

            if (menu.Module == null || menu.Module.Id == Guid.Empty)
            {
                throw new DefinedException(UserSystemMessagesResources.Module_Empty);
            }

            if (_Repository.Exists(menu))
            {
                throw new DataExistsException(string.Format(UserSystemMessagesResources.Menu_Exists_WithValue, menu.Name));
            }

            foreach (var p in menu.Permissions)
            {
                if (p.Id == Guid.Empty)
                {
                    p.Id      = IdentityGenerator.NewSequentialGuid();
                    p.Created = DateTime.UtcNow;
                }
            }

            menu.Module = _ModuleRepository.Get(menu.Module.Id);

            _Repository.Add(menu);

            #region 操作日志

            var menuDto = menu.ToDto();

            OperateRecorder.RecordOperation(menuDto.Id.ToString(),
                                            UserSystemMessagesResources.Add_Menu,
                                            menuDto.GetOperationLog());

            #endregion

            //commit the unit of work
            _Repository.UnitOfWork.Commit();

            return(menu.ToDto());
        }
        public void Update(MenuDTO menuDTO)
        {
            //get persisted item
            var menu = _Repository.Get(menuDTO.Id);

            if (menu == null)
            {
                throw new DataNotFoundException(UserSystemMessagesResources.Menu_NotExists);
            }

            var oldDTO = menu.ToDto();

            var current = menuDTO.ToModel();

            menu.Name   = current.Name;
            menu.Module = _ModuleRepository.Get(menuDTO.Module.Id);
            if (current.Parent == null || current.Parent.Id == Guid.Empty)
            {
                menu.Depth = 1;
            }
            else
            {
                menu.Parent = _Repository.Get(current.Parent.Id);
                menu.Depth  = current.Parent.Depth + 1;
            }
            menu.SortOrder = current.SortOrder;
            menu.Url       = current.Url;
            menu.Code      = current.Code;


            if (menu.Name.IsNullOrBlank())
            {
                throw new DefinedException(UserSystemMessagesResources.Name_Empty);
            }
            if (current.Module == null || current.Module.Id == Guid.Empty)
            {
                throw new DefinedException(UserSystemMessagesResources.Module_Empty);
            }

            if (_Repository.Exists(menu))
            {
                throw new DataExistsException(string.Format(UserSystemMessagesResources.Menu_Exists_WithValue, menu.Name));
            }
            menu.Module = _ModuleRepository.Get(menu.Module.Id);

            if (menu.Module.Id != oldDTO.Module.Id)
            {
                // 如果更换了模块
                var oldModuleMenus = _Repository.FindBy(oldDTO.Module.Id, string.Empty, 1, int.MaxValue);
                var allChildMenus  = new List <Menu>();
                GetAllChildMenus(menu.Id, oldModuleMenus, ref allChildMenus);
                if (allChildMenus.Any())
                {
                    // 所有子菜单都更换为新的Module
                    foreach (var m in allChildMenus)
                    {
                        m.Module = menu.Module;

                        #region 操作日志

                        var mDto = m.ToDto();

                        OperateRecorder.RecordOperation(mDto.Id.ToString(),
                                                        UserSystemMessagesResources.Update_Menu,
                                                        UserSystemMessagesResources.Menu_UpdateChildMenuModule + ":" +
                                                        string.Format("{0} => {1}", oldDTO.Module.Name, menu.Module.Name));

                        #endregion
                    }
                }
            }

            #region 操作日志

            var menuDto = menu.ToDto();

            OperateRecorder.RecordOperation(menuDto.Id.ToString(),
                                            UserSystemMessagesResources.Update_Menu,
                                            menuDto.GetOperationLog(oldDTO));

            #endregion

            //commit unit of work
            _Repository.UnitOfWork.Commit();
        }