예제 #1
0
        public async Task <ActionResult> EditCategory(Guid categoryId, string newCategoryName)
        {
            //未登陆、系统内置、重名、信息为空、当前用户不是分类的拥有者不可编辑
            if (categoryId == null || newCategoryName == null || categoryId == Guid.Empty || newCategoryName.Trim() == "")
            {
                return(Json(new { status = "fail", result = "提交的数据不完整,请重试!" }, JsonRequestBehavior.AllowGet));
            }
            //获取当前登陆的id,cookie的id需要解密
            string userCookieId = ""; string message;

            if (Request.Cookies["userId"] != null)
            {
                if (!JwtHelper.GetJwtDecode(Request.Cookies["userId"].Value, out userCookieId, out message))
                {
                    return(Json(new { status = "fail", result = message }, JsonRequestBehavior.AllowGet));
                }
            }
            string userId = Session["userId"] == null ? userCookieId : Session["userId"].ToString();

            if (userId.Trim() == "")
            {
                return(Json(new { status = "fail", result = "还未登陆无法编辑" }, JsonRequestBehavior.AllowGet));
            }
            IArticleManager articleManager = new ArticleManager();

            if (!await articleManager.ExistsCategory(categoryId))//分类id不存在
            {
                return(Json(new { status = "fail", result = "未能找到对应ID的分类,请稍后再试" }, JsonRequestBehavior.AllowGet));
            }
            var data = await articleManager.GetOneCategoryById(categoryId); //要经过上面的判断否则会出错

            if (data.UserId != Guid.Parse(userId))                          //分类作者才可编辑分类
            {
                if (data.UserId == Guid.Parse("00000000-0000-0000-0000-000000000001"))
                {
                    return(Json(new { status = "fail", result = "系统内置分类不可进行编辑" }, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new { status = "fail", result = "非本人分类不可进行编辑" }, JsonRequestBehavior.AllowGet));
                }
            }
            //循环自己所有的分类,对比是否有重名
            List <BlogCategoryDto> categories = await articleManager.GetAllCategories(Guid.Parse(userId));

            foreach (BlogCategoryDto category in categories)
            {
                if (category.BlogCategoryName == newCategoryName)//修改后的名字和现有的重复,则提示失败
                {
                    return(Json(new { status = "fail", result = "该名字已存在,请修改后重试!" }, JsonRequestBehavior.AllowGet));
                }
            }
            await articleManager.EditCategory(categoryId, newCategoryName);

            return(Json(new { status = "ok", result = "编辑成功!" }, JsonRequestBehavior.AllowGet));
        }