17
2017
04

.Net Core MVC 网站开发(Ninesky) 2.6、栏目模型调整

 写完栏目的添加、修改和删除后,总感觉不顺,感觉删除和修改的流程很混乱。按照原来的设想,栏目分三种类型:常规栏目,单页栏目,链接栏目。实际上有四个模型类Category,CategoryGeneral,CategoryPage,CategoryLink。Category作为一个主类,后面三个是导航属性,在添加、修改时要设计几个类的操作,用起来太复杂了。干脆合并到一个模型里去。如下图:

这样三种栏目类型的数据都放心一个类里,不同栏目类型显示模型类中的不同属性,但是添加修改事只要操作一个模型类就行了,这样就简单多了。下面是三个类型栏目的界面。



再看一下修改栏目的代码

1、控制器中的Modify action

2、UpdateAsync实现

 /// <summary>
        /// 更新栏目
        /// </summary>
        /// <param name="entity">栏目实体</param>
        /// <param name="isSave">立即保存</param>
        /// <returns></returns>
        public override async Task<OperationResult> UpdateAsync(Category category, bool isSave = true)
        {
            var oResult = new OperationResult() { Succeed = true, Message = "更新成功栏目" };
            var originalCategory = await FindAsync(category.CategoryId);
            //栏目是否存在
            if (originalCategory == null) return new OperationResult() { Succeed = false, Message = "栏目不存在,请确认栏目【" + category.Name + "】是否已被删除?" };
            //栏目类型是否更改
            else if (category.Type != originalCategory.Type) return new OperationResult() { Succeed = false, Message = "禁止修改栏目类型" };
            switch (category.Type)
            {
                //常规栏目
                case CategoryType.General:
                    //父栏目更改
                    if (category.ParentId != originalCategory.ParentId)
                    {
                        //查找新父栏目
                        var parentCategory = await FindAsync(category.ParentId);
                        //父栏目不存在
                        if (parentCategory == null) return new OperationResult() { Succeed = false, Message = "父栏目不存在" };
                        else
                        {
                            var originalParentPath = originalCategory.ParentPath;
                            var newParentPath = string.Empty;
                            //是否可以作为当前栏目的父栏目
                            var canParentResult = CanParent(parentCategory, originalCategory);
                            if (canParentResult.Succeed)
                            {
                                if (category.ParentId == 0)
                                {
                                    originalCategory.ParentId = category.ParentId;
                                    originalCategory.ParentPath = "0";
                                    originalCategory.Depth = 0;
                                }
                                else
                                {
                                    //更改原来父栏目的子栏目数
                                    var originalParent = await FindAsync(originalCategory.ParentId);
                                    if (originalParent != null)
                                    {
                                        originalParent.ChildNum--;
                                        await base.UpdateAsync(originalParent, false);
                                    }
                                    //更新新父栏目的子栏目数
                                    parentCategory.ChildNum++;
                                    await base.UpdateAsync(parentCategory, false);
                                    //更改栏目的父栏目ID,路径及深度
                                    originalCategory.ParentId = category.ParentId;
                                    originalCategory.ParentPath = parentCategory.ParentPath + "," + parentCategory.CategoryId;
                                    originalCategory.Depth = parentCategory.Depth + 1;
                                    newParentPath = originalCategory.ParentPath;
                                }
                                //循环更改子栏目的路径
                                if (originalCategory.ChildNum > 0)
                                {
                                    var childrenCategories = await FindListAsync(c => c.ParentPath.IndexOf(originalParentPath) == 0);
                                    foreach (var childCategory in childrenCategories)
                                    {
                                        childCategory.ParentPath = childCategory.ParentPath.Replace(originalParentPath, newParentPath);
                                    }
                                    await UpdateRangeAsync(childrenCategories.ToArray(), false);
                                }
                            }
                            else return canParentResult;
                        }
                    }
                    originalCategory.View = category.View;
                    originalCategory.Meta_Keywords = category.Meta_Keywords;
                    originalCategory.Meta_Description = category.Meta_Description;
                    originalCategory.ContentModuleId = category.ContentModuleId;
                    originalCategory.ContentView = category.ContentView;
                    originalCategory.ContentOrder = category.ContentOrder;
                    originalCategory.PageSize = category.PageSize;
                    break;
                case CategoryType.Page:
                    //父栏目更改
                    if (category.ParentId != originalCategory.ParentId)
                    {
                        //查找新父栏目
                        var parentCategory = await FindAsync(category.ParentId);
                        //父栏目不存在
                        if (parentCategory == null) return new OperationResult() { Succeed = false, Message = "父栏目不存在" };
                        else
                        {
                            var originalParentPath = originalCategory.ParentPath;
                            var newParentPath = string.Empty;
                            //是否可以作为当前栏目的父栏目
                            var canParentResult = CanParent(parentCategory, originalCategory);
                            if (canParentResult.Succeed)
                            {
                                if (category.ParentId == 0)
                                {
                                    originalCategory.ParentId = category.ParentId;
                                    originalCategory.ParentPath = "0";
                                    originalCategory.Depth = 0;
                                }
                                else
                                {
                                    //更改原父栏目的子栏目数
                                    var originalParent = await FindAsync(originalCategory.ParentId);
                                    if (originalParent != null)
                                    {
                                        originalParent.ChildNum--;
                                        await base.UpdateAsync(originalParent, false);
                                    }
                                    //更新新父栏目的子栏目数
                                    parentCategory.ChildNum++;
                                    await base.UpdateAsync(parentCategory, false);
                                    //更改栏目的父栏目ID,路径及深度
                                    originalCategory.ParentId = category.ParentId;
                                    originalCategory.ParentPath = parentCategory.ParentPath + "," + parentCategory.CategoryId;
                                    originalCategory.Depth = parentCategory.Depth + 1;
                                    newParentPath = originalCategory.ParentPath;
                                }
                            }
                            else return canParentResult;
                        }
                    }
                    originalCategory.View = category.View;
                    originalCategory.Meta_Keywords = category.Meta_Keywords;
                    originalCategory.Meta_Description = category.Meta_Description;
                    break;
                case CategoryType.Link:
                    //父栏目更改
                    if (category.ParentId != originalCategory.ParentId)
                    {
                        //查找新父栏目
                        var parentCategory = await FindAsync(category.ParentId);
                        //父栏目不存在
                        if (parentCategory == null) return new OperationResult() { Succeed = false, Message = "父栏目不存在" };
                        else
                        {
                            var originalParentPath = originalCategory.ParentPath;
                            var newParentPath = string.Empty;
                            //是否可以作为当前栏目的父栏目
                            var canParentResult = CanParent(parentCategory, originalCategory);
                            if (canParentResult.Succeed)
                            {
                                if (category.ParentId == 0)
                                {
                                    originalCategory.ParentId = category.ParentId;
                                    originalCategory.ParentPath = "0";
                                    originalCategory.Depth = 0;
                                }
                                else
                                {
                                    //更改原父栏目的子栏目数
                                    var originalParent = await FindAsync(originalCategory.ParentId);
                                    if (originalParent != null)
                                    {
                                        originalParent.ChildNum--;
                                        await base.UpdateAsync(originalParent, false);
                                    }
                                    //更新新父栏目的子栏目数
                                    parentCategory.ChildNum++;
                                    await base.UpdateAsync(parentCategory, false);
                                    //更改栏目的父栏目ID,路径及深度
                                    originalCategory.ParentId = category.ParentId;
                                    originalCategory.ParentPath = parentCategory.ParentPath + "," + parentCategory.CategoryId;
                                    originalCategory.Depth = parentCategory.Depth + 1;
                                    newParentPath = originalCategory.ParentPath;
                                }
                            }
                            else return canParentResult;
                        }
                    }
                    originalCategory.LinkUrl = category.LinkUrl;
                    break;
            }
            
            originalCategory.Name = category.Name;
            originalCategory.ShowOnMenu = category.ShowOnMenu;
            originalCategory.Target = category.Target;
            originalCategory.Order = category.Order;
            originalCategory.PicUrl = category.PicUrl;
            originalCategory.Description = category.Description;
            oResult = await base.UpdateAsync(originalCategory);
            return oResult;
        }
    }


这个看着比原来,容易理解多了。


这次修改后基本上把栏目的前几篇代码都推翻了,这里就不贴代码了,直接看源码包吧。

附件:Ninesky.2.6、栏目模型调整.rar

« 上一篇下一篇 »

评论列表:

1.redcat  2017-6-1 17:05:48 回复该留言
猴年马月

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。