public IActionResult ConfirmDelete(Guid Id) { try { var CurrentUser = _context.Users.Find(userManager.GetUserId(User)); if (CurrentUser is null) { throw new DomainException(ErrorMessages.NotSignedIn); } var existingComment = _commentStore.GetAllComments().First(x => x.Id == Id); if (existingComment is null) { throw new DomainException(ErrorMessages.CommentDoesNotExist); } var CommentOwner = _commentStore.GetOwnerOfComment(existingComment.UserId); if (!CommentOwner.UserId.Equals(CurrentUser.Id)) { throw new ForbiddenException(ErrorMessages.ForbiddenAccess); } var DeleteCommentDTO = new DeleteCommentDTO() { UserId = CommentOwner.UserId, PostID = existingComment.PostId, CommentID = existingComment.Id, Content = existingComment.Content }; var PostID = new Guid(DeleteCommentDTO.PostID.ToString()); _commentStore.DeleteComment(existingComment); _context.SaveChanges(); return(RedirectToAction("ViewPost", "Posts", new { id = PostID })); } catch (DomainException ex) { _logger.LogError(ex.Message); if (ex.Message.Equals(ErrorMessages.NotSignedIn)) { return(RedirectToAction("Login", "Accounts")); } return(RedirectToAction("NotFound", "Accounts")); } catch (ForbiddenException ex) { _logger.LogError(ex.Message); return(RedirectToAction(ActionName.NotAuthorized, ControllerName.Accounts)); } catch (Exception ex) { _logger.LogError(ex.Message); return(RedirectToAction(ActionName.ServerError, ControllerName.Accounts)); } }
public IActionResult DeleteComment(Guid Id) { try { var CurrentUser = _context.Users.Find(userManager.GetUserId(User)); if (CurrentUser is null) { throw new DomainException(ErrorMessages.NotSignedIn); } var existingComment = _commentStore.GetAllComments().First(x => x.Id == Id); var CommentOwner = _commentStore.GetOwnerOfComment(existingComment.UserId); if (!CommentOwner.UserId.Equals(CurrentUser.Id)) { throw new ForbiddenException(ErrorMessages.ForbiddenAccess); } var existingAccount = _userStore.GetByIdentityUserId(CommentOwner.UserId); var DeleteCommentDTO = new DeleteCommentDTO() { UserId = CommentOwner.UserId, PostID = existingComment.PostId, PostedByUsername = existingAccount.Username, CommentID = existingComment.Id, Content = existingComment.Content }; return(View(DeleteCommentDTO)); } catch (ForbiddenException ex) { _logger.LogError(ex.Message); return(RedirectToAction(ActionName.NotAuthorized, ControllerName.Accounts)); } catch (DomainException ex) { _logger.LogError(ex.Message); if (ex.Message.Equals(ErrorMessages.PostDoesNotExist)) { return(RedirectToAction(ActionName.NotFound, ControllerName.Accounts)); } return(RedirectToAction(ActionName.Login, ControllerName.Accounts)); } catch (Exception ex) { _logger.LogError(ex.Message); return(RedirectToAction(ActionName.ServerError, ControllerName.Accounts)); } }
public IActionResult DeleteCommentById(DeleteCommentDTO deleteCommentDTO) { var apiJsonResponse = new ApiJsonResponse(); try { if (deleteCommentDTO != null) { if (deleteCommentDTO.ModifiedUserId != null) { using (FoodInfoServiceContext context = new FoodInfoServiceContext()) { try { var comment = context.Comments.Where(x => x.ID == deleteCommentDTO.Id && x.IsDeleted == false).FirstOrDefault(); comment.IsDeleted = true; comment.ModifiedDate = DateTime.Now; comment.ModifiedUserId = deleteCommentDTO.ModifiedUserId; } catch { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.CommentNotFound)); } context.SaveChanges(); return(apiJsonResponse.ApiOkContentResult(deleteCommentDTO)); } } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.ModifiedUserIdRequired)); } } else { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } } catch { return(apiJsonResponse.ApiBadRequestWithMessage(PublicConstants.SysErrorMessage)); } }