Exemple #1
0
        public async Task Handler_GivenInvalidPostId_ReturnsEmptyCommentsDTOCollection()
        {
            // Arrange
            var query = new GetCommentsByPostIdQuery {
                PostId = 99
            };

            // Act
            var handler = new GetCommentsByPostIdQuery.GetCommentsByPostIdQueryHandler(Context, Mapper);
            var result  = await handler.Handle(query, CancellationToken.None);

            // Assert
            result.ShouldBeEmpty();
        }
Exemple #2
0
        public async Task Handler_GivenValidPostId_ReturnsNotEmptyCommentsDTOCollection()
        {
            // Arrange
            var query = new GetCommentsByPostIdQuery {
                PostId = 1
            };

            // Act
            var handler = new GetCommentsByPostIdQuery.GetCommentsByPostIdQueryHandler(Context, Mapper);
            var result  = await handler.Handle(query, CancellationToken.None);

            // Assert
            result.ShouldBeOfType <List <CommentDTO> >();
            result.ShouldNotBeNull();
        }
Exemple #3
0
        public async Task <IActionResult> Read(int id)
        {
            if (id == default)
            {
                return(NotFound());
            }

            // Get post.
            var postQuery = new GetPostQuery {
                Id = id
            };
            var post = await _mediator.Send(postQuery);

            // Add author information.
            var authorQuery = new GetAuthorQuery {
                Id = post.AuthorId
            };
            var author = await _mediator.Send(authorQuery);

            post.Author = author.FirstName + " " + author.LastName;

            // Add topic information.
            var topicQuery = new GetTopicQuery {
                Id = post.TopicId
            };
            var topic = await _mediator.Send(topicQuery);

            post.Topic = topic.Text;

            // Get comments for current post.
            var commentsQuery = new GetCommentsByPostIdQuery {
                PostId = post.Id
            };
            var comments = await _mediator.Send(commentsQuery);

            // Create post view model.
            var model = _mapper.Map <PostDTO, PostViewModel>(post);

            model.AuthorAvatar = author.Avatar;

            // Check current user if he/she is an author of the current post.
            var userName = HttpContext.User.Identity.Name;

            if (userName == null)
            {
                model.CurrentReaderId = default;
            }
            else
            {
                var userId = await _identityService.GetUserIdByNameAsync(userName);

                var reader = await _mediator.Send(new GetAuthorByUserIdQuery { UserId = userId });

                if (reader == null)
                {
                    model.CurrentReaderId = default;
                }
                else
                {
                    model.CurrentReaderId = reader.Id;
                }
            }

            // Check post comments
            model.Comments = _mapper.Map <ICollection <CommentDTO>, ICollection <CommentViewModel> >(comments);

            foreach (var comment in model.Comments)
            {
                var commentAuthorQuery = new GetAuthorQuery {
                    Id = comment.AuthorId
                };
                var commentAuthor = await _mediator.Send(commentAuthorQuery);

                comment.Author = commentAuthor.FirstName + " " + commentAuthor.LastName;

                (var age, var units) = comment.Date.Age();
                comment.Age          = age;
                comment.AgeUnits     = units;
                comment.AuthorAvatar = commentAuthor.Avatar;
            }

            return(View(model));
        }