public async Task <IActionResult> DeletePostMainComment(int id) { var maincomment = _repo.GetPostMainComment(id); bool hasSubs = maincomment.SubComments.Count() > 0; if (hasSubs == true) { var comment = new PostMainComment { Id = maincomment.Id, CreatedDate = maincomment.CreatedDate, Message = "This comment has been removed.", UserId = null, PostId = maincomment.PostId }; _repo.UpdatePostMainComment(comment); } else { _repo.DeletePostMainComment(id); } await _repo.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> PostComment(PostCommentViewModel vm) { if (!ModelState.IsValid) { return(RedirectToAction("Post", new { id = vm.PostId, slug = vm.PostSlug })); } else { var user = await _userManager.GetUserAsync(HttpContext.User); if (vm.MainCommentId == 0) { var comment = new PostMainComment { PostId = vm.PostId, PostSlug = vm.PostSlug, Message = vm.Message, CreatedDate = DateTime.Now, User = user }; _repo.AddPostMainComment(comment); string notifyMailText = EmailHelper.BuildTemplate(_templatesPath, "NewCommentTemplate.html"); notifyMailText = notifyMailText.Replace("[username]", user.UserName).Replace("[slug]", vm.PostSlug).Replace("[message]", vm.Message); await _emailService.SendAsync("*****@*****.**", "New Post Main Comment Added", notifyMailText, true); } else { var comment = new PostSubComment { PostMainCommentId = vm.MainCommentId, Message = vm.Message, CreatedDate = DateTime.Now, User = user }; _repo.AddPostSubComment(comment); string notifyMailText = EmailHelper.BuildTemplate(_templatesPath, "NewCommentTemplate.html"); notifyMailText = notifyMailText.Replace("[username]", user.UserName).Replace("[slug]", vm.MainCommentId.ToString()).Replace("[message]", vm.Message); await _emailService.SendAsync("*****@*****.**", "New Post Sub Comment Added", notifyMailText, true); } await _repo.SaveChangesAsync(); return(RedirectToAction("Post", new { id = vm.PostId, slug = vm.PostSlug })); } }
//Used only to change a post main comment to a blank if user/admin want to delete the comment and //it has sub comments, so sub comments aren't lost on a thread. public void UpdatePostMainComment(PostMainComment comment) { _ctx.PostMainComments.Update(comment); }
//Post Main Comments public void AddPostMainComment(PostMainComment comment) { _ctx.PostMainComments.Add(comment); }
public async Task <IActionResult> OnPostAsync() { var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } RequirePassword = await _userManager.HasPasswordAsync(user); if (RequirePassword) { if (!await _userManager.CheckPasswordAsync(user, Input.Password)) { ModelState.AddModelError(string.Empty, "Incorrect password."); return(Page()); } } //For deleting user comments var model = new ApplicationUser() { UserPostMainComments = _repo.GetAllPostMainComments(user.Id), UserPostSubComments = _repo.GetAllPostSubComments(user.Id), UserPortfolioItemMainComments = _repo.GetAllPortfolioItemMainComments(user.Id), UserPortfolioItemSubComments = _repo.GetAllPortfolioItemSubComments(user.Id) }; foreach (var comment in model.UserPostSubComments) { _repo.DeletePostSubComment(comment.Id); await _repo.SaveChangesAsync(); } ; foreach (var comment in model.UserPostMainComments) { var maincomment = _repo.GetPostMainComment(comment.Id); bool hasSubs = maincomment.SubComments.Count() > 0; if (hasSubs == true) { var mc = new PostMainComment { Id = maincomment.Id, CreatedDate = maincomment.CreatedDate, Message = "This comment has been removed.", UserId = null, PostId = maincomment.PostId }; _repo.UpdatePostMainComment(mc); } else { _repo.DeletePostMainComment(comment.Id); } await _repo.SaveChangesAsync(); } ; foreach (var comment in model.UserPortfolioItemSubComments) { _repo.DeletePortfolioItemSubComment(comment.Id); await _repo.SaveChangesAsync(); } ; foreach (var comment in model.UserPortfolioItemMainComments) { var maincomment = _repo.GetPortfolioItemMainComment(comment.Id); bool hasSubs = maincomment.SubComments.Count() > 0; if (hasSubs == true) { var mc = new PortfolioItemMainComment { Id = maincomment.Id, CreatedDate = maincomment.CreatedDate, Message = "This comment has been removed.", UserId = null, PortfolioItemId = maincomment.PortfolioItemId }; _repo.UpdatePortfolioItemMainComment(mc); } else { _repo.DeletePortfolioItemMainComment(comment.Id); } await _repo.SaveChangesAsync(); } ; var result = await _userManager.DeleteAsync(user); var userId = await _userManager.GetUserIdAsync(user); if (!result.Succeeded) { throw new InvalidOperationException($"Unexpected error occurred deleting user with ID '{userId}'."); } await _signInManager.SignOutAsync(); _logger.LogInformation("User with ID '{UserId}' deleted themselves.", userId); return(Redirect("~/")); }