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)); }
public async Task <ActionResult> DeleteCategory(Guid?Id) { //获取当前登陆的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 == null || userId.Trim() == "") //用户未登录 { return(Json(new { status = "fail", result = "获取不到用户信息,请检查登陆状态" }, JsonRequestBehavior.AllowGet)); //返回错误信息 } IArticleManager articleManager = new ArticleManager(); if (Id == null || !await articleManager.ExistsCategory(Id.Value)) //文章id找不到则跳转分类不存在错误页面 { return(Json(new { status = "fail", result = "未能找到对应ID的分类,请稍后再试" })); //返回错误信息 }//要经过上面的判断否则会出错 BlogCategory data = await articleManager.GetOneCategoryById(Id.Value); //获取分类id的所有信息 if (data.UserId != Guid.Parse(userId)) //文章作者才可删除文章 { if (data.UserId == Guid.Parse("00000000-0000-0000-0000-000000000001")) //如果是01用户,说明是系统内置分类,不可删除 { return(Json(new { status = "fail", result = "系统内置分类不可进行删除" })); //返回错误信息 } else { return(Json(new { status = "fail", result = "非本人分类不可进行删除" }));//返回错误信息 } } if (await articleManager.GetArticleDataCount(Guid.Parse(userId), Id.Value) != 0) { return(Json(new { status = "fail", result = "已有文章引用了该分类,如需删除请先编辑文章取消引用该分类" }));//返回错误信息 } await articleManager.RemoveCategory(Id.Value); return(Json(new { status = "ok", result = "删除成功!" })); }