public async Task <IActionResult> Delete(string id) { if (id == null) { return(NotFound()); } ArticleDeleteViewModel articleDeleteViewModel = (await this.articlesService.GetArticleById(id)).To <ArticleDeleteViewModel>(); if (articleDeleteViewModel == null) { return(this.Redirect("/")); throw new ArgumentNullException(nameof(articleDeleteViewModel)); } return(this.View(articleDeleteViewModel)); }
public ActionResult ArticleDelete(int ArticleId) { bool Barticle = false, Bcomments = false, BcommentsRates = false, Bphotos = false, Brates = false, BtagsConnections = false; var article = db.Articles.Where(aid => aid.ArticleId == ArticleId).First(); db.Articles.Remove(article); db.SaveChanges(); if (db.Articles.Where(aid => aid.ArticleId == ArticleId).Count() == 0) { Barticle = true; } var articleComments = db.ArticlesComments.Where(aid => aid.ArticleId == ArticleId).ToList(); foreach (var comment in articleComments) { db.ArticlesComments.Remove(comment); db.SaveChanges(); } if (db.ArticlesComments.Where(aid => aid.ArticleId == ArticleId).Count() == 0) { Bcomments = true; } var articleCommentsRates = db.ArticlesCommentsRates.Where(aid => aid.ArticleId == ArticleId).ToList(); foreach (var rate in articleCommentsRates) { db.ArticlesCommentsRates.Remove(rate); db.SaveChanges(); } if (db.ArticlesCommentsRates.Where(aid => aid.ArticleId == ArticleId).Count() == 0) { BcommentsRates = true; } var articlePhotos = db.ArticlesPhotos.Where(aid => aid.ArticleId == ArticleId).ToList(); foreach (var photo in articlePhotos) { var path = Path.Combine(Server.MapPath(AppConfig.ArticlesImagesFolderRelated), photo.FilePath); if (System.IO.File.Exists(path) == true) { System.IO.File.Delete(path); } db.ArticlesPhotos.Remove(photo); db.SaveChanges(); } if (db.ArticlesPhotos.Where(aid => aid.ArticleId == ArticleId).Count() == 0) { Bphotos = true; } var articleRates = db.ArticlesRates.Where(aid => aid.ArticleId == ArticleId).ToList(); foreach (var rate in articleRates) { db.ArticlesRates.Remove(rate); db.SaveChanges(); } if (db.ArticlesRates.Where(aid => aid.ArticleId == ArticleId).Count() == 0) { Brates = true; } var articleTagsConnections = db.ArticlesTagsConnections.Where(aid => aid.ArticleId == ArticleId).ToList(); foreach (var connection in articleTagsConnections) { db.ArticlesTagsConnections.Remove(connection); db.SaveChanges(); } if (db.ArticlesTagsConnections.Where(aid => aid.ArticleId == ArticleId).Count() == 0) { BtagsConnections = true; } ArticleDeleteViewModel VM = new ArticleDeleteViewModel { Success = false }; if (Barticle && Bcomments && BcommentsRates && Bphotos && Brates && BtagsConnections) { VM = new ArticleDeleteViewModel { ArticleId = ArticleId, Comments = articleComments.Count(), CommentsRates = articleCommentsRates.Count(), Photos = articlePhotos.Count(), Rates = articleRates.Count(), TagConnections = articleTagsConnections.Count(), Success = true }; } return(PartialView(VM)); }
public IActionResult Delete(ArticleDeleteViewModel model) => this.View(model);