Example #1
0
        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));
            }
        }
Example #2
0
        public List <ViewCommentDTO> GetPostComments()
        {
            var comments = from x in CommentStore.GetAllComments()
                           where x.PostId == ExistingPost.Id
                           orderby x.DatePosted
                           select x;
            var ListOfComments = new List <ViewCommentDTO>();

            foreach (var comment in comments)
            {
                var OwnerOfComment = CommentStore.GetOwnerOfComment(comment.UserId);
                var CommentViewDTO = new ViewCommentDTO
                {
                    UserId     = comment.UserId,
                    PostId     = comment.PostId,
                    Content    = comment.Content,
                    Username   = OwnerOfComment.Username,
                    DatePosted = comment.DatePosted,
                    CommentId  = comment.Id
                };
                ListOfComments.Add(CommentViewDTO);
            }
            return(ListOfComments);
        }