Esempio n. 1
0
        public async Task Should_Put_a_Comment_On_A_Post()
        {
            Result <int> commentPostResult;
            var          author    = new User((Nickname)"author1", (FullName)"author one", Password.Create("password").Value, (Email)"*****@*****.**", "my bio");
            var          commenter = new User((Nickname)"commenter1", (FullName)"commenter one", Password.Create("password").Value, (Email)"*****@*****.**", "my bio");
            var          post      = new Post(author, (Picture)Convert.FromBase64String(DatabaseFixture.GetTestPictureBase64()), (PostText)"test post");

            using (var session = _testFixture.OpenSession(_output))
            {
                await session.SaveAsync(author);

                await session.SaveAsync(commenter);

                await session.SaveAsync(post);
            }

            var command = new PublishCommentCommand(post.ID, "test comment", commenter.ID);

            using (var session = _testFixture.OpenSession(_output))
            {
                var sut = new PublishCommentCommandHandler(session, Log.Logger);
                commentPostResult = await sut.Handle(command, default);
            }

            using (new AssertionScope())
            {
                commentPostResult.IsSuccess.Should().BeTrue();
                using (var session = _testFixture.OpenSession(_output))
                {
                    (await session.GetAsync <Comment>(commentPostResult.Value)).Should().NotBeNull();
                    (await session.GetAsync <Post>(post.ID)).Comments.Count().Should().Be(1);
                }
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> PublishComment(PublishCommentModel newComment)
        {
            var command       = new PublishCommentCommand(newComment.PostID, newComment.CommentText, User.GetIdentifier());
            var commandResult = await _dispatcher.Send(command);

            if (commandResult.IsSuccess)
            {
                var newCommentNotification = new CommentPublishedEvent(
                    User.Identity.Name,
                    Url.Action("Profile", "Account", new { id = User.Identity.Name }),
                    newComment.PostID,
                    Url.Action("Detail", "Post", new { id = newComment.PostID })
                    );

                await _dispatcher.Publish(newCommentNotification);
            }
            var commentsQuery = new PostCommentsQuery(newComment.PostID);
            var comments      = await _dispatcher.Send(commentsQuery);

            return(PartialView("_CommentsPartial", comments));
        }
Esempio n. 3
0
        public async Task <Result <int> > Handle(PublishCommentCommand request, CancellationToken cancellationToken)
        {
            using (var tx = _session.BeginTransaction())
            {
                User author = null;
                try
                {
                    author = await _session.LoadAsync <User>(request.UserID, cancellationToken);

                    var post = await _session.LoadAsync <Post>(request.PostID, cancellationToken);

                    var comment = new Comment(post, author, (CommentText)request.Text);
                    post.AddComment(comment);

                    await tx.CommitAsync(cancellationToken);

                    _logger.Information("User [{NickName}({UserID})] wrote a new comment to post {PostID}",
                                        request.UserID,
                                        author.Nickname,
                                        request.PostID);

                    return(Result.Success(comment.ID));
                }
                catch (Exception ex)
                {
                    await tx.RollbackAsync(cancellationToken);

                    _logger.Error("Failed to save comment to post {PostID} written by user [{NickName}({UserID})]. Error message: {ErrorMessage}",
                                  request.PostID,
                                  author?.Nickname,
                                  request.UserID,
                                  ex.Message);

                    return(Result.Failure <int>(ex.Message));
                }
            }
        }