public async Task <IActionResult> Edit(ArticleDto article) { Article editArticle = await _articleRep.FindByIDAsync(article.Id); if (editArticle == null) { return(View("Error")); } if (!ModelState.IsValid) { ViewBag.Categories = new SelectList(_unitOfWork.GetRepository <Category>().FindAll(), "Id", "Title"); return(View(article)); } ModelValid model = new ModelValid(); if (article.IsPublish) { if (editArticle.Image != null) { model = article.IsValid(article, false); } else { model = article.IsValid(article); } } if (!model.Valid) { foreach (var item in model.Errors) { ModelState.AddModelError("", item); } ViewBag.Categories = new SelectList(_unitOfWork.GetRepository <Category>().FindAll(), "Id", "Title"); return(View(article)); } if (article.FormFile != null) { if (editArticle.Image != null) { await _fileWorker.RemoveFileAsync(editArticle.Image, "Files"); } editArticle.Image = await _fileWorker.SaveFileAsync(article.FormFile, "Files"); } if (article.IsPublish) { editArticle.PublishDate = DateTime.Now; } editArticle.IsPublish = article.IsPublish; editArticle.Tags = article.Tags; editArticle.TimeToRead = article.TimeToRead; editArticle.Title = article.Title; editArticle.CategoryId = article.CategoryId; editArticle.Content = article.Content; editArticle.Description = article.Description; _articleRep.Update(editArticle); await _unitOfWork.Commit(); return(Redirect("/admin/articles")); }
public async Task <IActionResult> Create(ArticleDto article) { if (!ModelState.IsValid) { ViewBag.Categories = new SelectList(_unitOfWork.GetRepository <Category>().FindAll(), "Id", "Title"); //article.FormFile return(View(article)); } ModelValid model = new ModelValid(); if (article.IsPublish) { model = article.IsValid(article); } if (!model.Valid) { foreach (var item in model.Errors) { ModelState.AddModelError("", item); } ViewBag.Categories = new SelectList(_unitOfWork.GetRepository <Category>().FindAll(), "Id", "Title"); return(View(article)); } Article createArticle = new Article { CategoryId = article.CategoryId, Content = article.Content, Tags = article.Tags, Description = article.Description, TimeToRead = article.TimeToRead, IsPublish = article.IsPublish, Title = article.Title, View = 0, }; if (article.FormFile != null) { createArticle.Image = await _fileWorker.SaveFileAsync(article.FormFile, "Files"); } if (article.IsPublish) { createArticle.PublishDate = DateTime.Now; await SendArticleNewEmail(article); } await _articleRep.CreateAsync(createArticle); await _unitOfWork.Commit(); Popup.PopupModel = new Popup("افزودن مقاله", $"افزودن مقاله با عنوان {article.Title} با موفقیت انجام شد", IconType.Success); return(Redirect("/admin/articles")); }