public async Task <ActionResult> EditArticle(Guid?articleId, string title, string content, string introContent, Guid[] categoryIds) { //id和内容和标题为空、未登录、登陆id不是这篇文章的拥有者、分类id不属于自己 if (articleId == null || articleId == Guid.Empty || title == null || content == null || title.Trim() == "" || content.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 == null || userId.Trim() == "") //用户未登录 { return(Json(new { status = "fail", result = "获取不到用户信息,请检查登陆状态" }, JsonRequestBehavior.AllowGet)); //返回错误信息 } //查找提交的文章id是否存在,是否属于当前登陆用户 IArticleManager articleManager = new ArticleManager(); if (!await articleManager.ExistsArticle(articleId.Value)) //文章id找不到则跳转文章不存在错误页面 { return(Json(new { status = "fail", result = "未能找到对应ID的文章,请稍后再试" }, JsonRequestBehavior.AllowGet)); //返回错误信息 } var data = await articleManager.GetOneArticleById(articleId.Value); //要经过上面的判断否则会出错 if (data.userId != Guid.Parse(userId)) //文章作者才可编辑文章 { return(Json(new { status = "fail", result = "非本人文章不可进行编辑" }, JsonRequestBehavior.AllowGet)); //返回错误信息 } //提交的分类信息不为空,循环自己所有的分类,对比是否正确 if (categoryIds != null) { List <BlogCategoryDto> categoryDtoes = await articleManager.GetAllCategories(Guid.Parse(userId)); //获取分类对象集合 List <Guid> currentUserCategoryIds = new List <Guid>(); //将分类对象中的分类id整合进一个集合中 foreach (BlogCategoryDto category in categoryDtoes) { currentUserCategoryIds.Add(category.Id); } for (int i = 0; i < categoryIds.Length; i++) //循环检查提交的分类id是否和自身的分类id有对应 { if (!currentUserCategoryIds.Contains(categoryIds[i])) //如果提交的分类id与自身的分类id没有匹配项,提示错误 { return(Json(new { status = "fail", result = "提交的分类与用户所拥有的分类不匹配,请重试!" }, JsonRequestBehavior.AllowGet)); } } } await articleManager.EditArticle(articleId.Value, title, content, introContent, categoryIds); return(Json(new { status = "ok", result = "提交成功!", articleId = articleId.Value }, JsonRequestBehavior.AllowGet)); }
public async Task <ActionResult> EditArticle(EditArticleViewModel model) { if (!ModelState.IsValid) { return(View(model)); } IArticleManager articleManager = new ArticleManager(); await articleManager.EditArticle(model.Id, model.Title, model.Content, model.CategoryIds); return(RedirectToAction("ArticleList")); }
private Task <StatusCodeResult> PostAsyncPartTwoArticle(Article s) { if (am.EditArticle(s)) { return(Task.FromResult(new StatusCodeResult(200))); //success } else { return(Task.FromResult(new StatusCodeResult(404))); //not found } }
public async Task <ActionResult> EditArticle(EditArticleViewModel model) { if (ModelState.IsValid) { IArticleManager articleManager = new ArticleManager(); await articleManager.EditArticle(model.ArticleId, model.Title, model.Content, model.CategoryIds); return(RedirectToAction(nameof(ArticleList))); } var userId = Guid.Parse(Session["userId"].ToString()); ViewBag.CategoryIds = await new ArticleManager().GetAllCategories(userId); return(View(model)); }
public async Task <ActionResult> EditArticle(Models.ArticleViewModels.EditArticleViewModel model) { if (ModelState.IsValid) { IBLL.IArticleManager articleManager = new ArticleManager(); await articleManager.EditArticle(model.Id, model.Title, model.Content, model.CategoryIds); return(RedirectToAction("AritcleList2")); } else { var userid = Guid.Parse(Session["userid"].ToString()); ViewBag.CategoryIds = await new ArticleManager().GetAllCategories(userid); return(View(model)); } }
public ActionResult EditArticle(Post post) { var _article = articleManager.GetByArticleNo(post.PostNo); if (_article == null) { throw new ArgumentNullException("_article"); } _article.Title = post.Title; _article.Tags = post.Tags; _article.Content = post.Content; try { articleManager.EditArticle(_article); return(RedirectToAction("Success")); } catch (Exception) { throw; } }