public async Task <IActionResult> DeleteComment(int id) { CommentDataModel commentDataModel = this.applicationDbContext.Comments.FirstOrDefault(c => c.Id == id); if (!(this.User.FindFirstValue(ClaimTypes.NameIdentifier).Equals(commentDataModel.CreatorId) || this.User.IsInRole("Administrator"))) { return(RedirectToAction("NoAccess", "Home")); } BlogPostDataModel blogPostDataModel = this.applicationDbContext.BlogPosts.FirstOrDefault(c => c.Id == commentDataModel.BlogPostId); blogPostDataModel.Comments -= 1; this.applicationDbContext.BlogPosts.Update(blogPostDataModel); this.applicationDbContext.Remove(commentDataModel); await this.applicationDbContext.SaveChangesAsync(); BigViewModel bigViewModel = new BigViewModel(); bigViewModel.BlogPostDataModel = blogPostDataModel; return(RedirectToAction("DetailsBlogPost", "Blog", new { bigViewModel.BlogPostDataModel.Id, bigViewModel })); }
public async Task <IActionResult> NewComment(int id, BigViewModel bigViewModel) { if (bigViewModel.CommentViewModel.Content == null || bigViewModel.CommentViewModel.Content.Length <= 0) { ModelState.AddModelError("", "Text field is required!"); return(RedirectToAction("DetailsBlogPost", "Blog", new { id, bigViewModel })); } BlogPostDataModel blogPostDataModel = this.applicationDbContext.BlogPosts.FirstOrDefault(c => c.Id == id); blogPostDataModel.Comments += 1; this.applicationDbContext.BlogPosts.Update(blogPostDataModel); this.applicationDbContext.Comments.Add(new CommentDataModel { BlogPostId = id, Content = bigViewModel.CommentViewModel.Content, UploadDate = DateTime.Now, CreatorId = this.User.FindFirstValue(ClaimTypes.NameIdentifier), CreatorName = HttpContext.User.Identity.Name }); await this.applicationDbContext.SaveChangesAsync(); return(RedirectToAction("DetailsBlogPost", "Blog", new { blogPostDataModel.Id, bigViewModel })); }
public IActionResult EditBlogPost(int id, BlogPostViewModel blogPostViewModel, IFormFile _formFile) { BlogPostDataModel target = this.applicationDbContext.BlogPosts.FirstOrDefault(bp => bp.Id == id); if (ModelState.IsValid) { if (target != null) { target.Title = blogPostViewModel.Title; target.Preview = blogPostViewModel.Preview; target.Content = blogPostViewModel.Content; target.LastChangeDate = DateTime.Now; if (_formFile != null) { target.Cover = FileToByteArray(_formFile); } } this.applicationDbContext.BlogPosts.Update(target); this.applicationDbContext.SaveChanges(); return(RedirectToAction("Index", "Home")); } blogPostViewModel.Title = target.Title; blogPostViewModel.Content = target.Content; return(View(blogPostViewModel)); }
public IActionResult EditBlogPost(int id) { BlogPostDataModel target = this.applicationDbContext.BlogPosts.FirstOrDefault(bp => bp.Id == id); if (!(this.User.FindFirstValue(ClaimTypes.NameIdentifier).Equals(target.CreatorId) || this.User.IsInRole("Administrator"))) { return(RedirectToAction("NoAccess", "Home")); } BlogPostViewModel blogPostViewModel = new BlogPostViewModel(); blogPostViewModel.Title = target.Title; blogPostViewModel.Preview = target.Preview; blogPostViewModel.Content = target.Content; ViewBag.CoverToBytes = target.Cover; return(View(blogPostViewModel)); }
public async Task <IActionResult> DeleteBlogPost(int id) { BlogPostDataModel target = this.applicationDbContext.BlogPosts.FirstOrDefault(bp => bp.Id == id); if (!(this.User.FindFirstValue(ClaimTypes.NameIdentifier).Equals(target.CreatorId) || this.User.IsInRole("Administrator"))) { return(RedirectToAction("NoAccess", "Home")); } foreach (CommentDataModel commentDataModel in this.applicationDbContext.Comments.Where(c => c.BlogPostId == target.Id)) { this.applicationDbContext.Comments.Remove(commentDataModel); } if (target != null) { this.applicationDbContext.BlogPosts.Remove(target); } await this.applicationDbContext.SaveChangesAsync(); return(RedirectToAction("Index", "Home")); }