コード例 #1
0
ファイル: CommentController.cs プロジェクト: n1ghtmare/Kauntr
        public async Task <ActionResult> Vote(CommentVoteViewModel model)
        {
            Comment comment = await _commentRepository.GetAsync(model.CommentId);

            if (ModelState.IsValid && comment.CreatedByAccountId != _contextService.CurrentUserAccountId)
            {
                Vote existingVote = await _voteRepository.GetByCommentIdAsync(model.CommentId, (int)_contextService.CurrentUserAccountId);

                if (existingVote != null)
                {
                    await _voteRepository.DeleteAsync(existingVote.Id);

                    if (existingVote.Value == model.Value)
                    {
                        return(await NotifyClientsAndGenerateVoteResultAsync(model.CommentId, (int)_contextService.CurrentUserAccountId));
                    }
                }

                var vote = new Vote {
                    CommentId         = model.CommentId,
                    Value             = model.Value,
                    CastedByAccountId = (int)_contextService.CurrentUserAccountId,
                    CastedOn          = _systemClock.UtcNow
                };
                await _voteRepository.CreateAsync(vote);

                return(await NotifyClientsAndGenerateVoteResultAsync(model.CommentId, (int)_contextService.CurrentUserAccountId));
            }
            return(new HttpStatusCodeResult(400, "Bad Request"));
        }
コード例 #2
0
ファイル: CommentController.cs プロジェクト: n1ghtmare/Kauntr
        private async Task <JsonResult> NotifyClientsAndGenerateVoteResultAsync(long commentId, int currentUserAccountId)
        {
            CommentAggregate commentAggregate = await _commentRepository.GetAggregateAsync(commentId, currentUserAccountId);

            await _notificationService.ClearCommentVoteNotificationsAsync(commentId, commentAggregate.CreatedByAccountId, currentUserAccountId);

            if (commentAggregate.CurrentUserVote != 0)
            {
                var notificationChange = new NotificationChange {
                    CreatedByAccountId     = currentUserAccountId,
                    CreatedOn              = _systemClock.UtcNow,
                    NotificationActionType = commentAggregate.CurrentUserVote > 0 ? NotificationActionType.Upvoted : NotificationActionType.Downvoted
                };
                await _notificationService.NotifyCommentOwnerAsync(commentId, notificationChange);
            }

            _notificationService.UpdateClientsAfterVote(commentAggregate);

            var model = new CommentVoteViewModel {
                CommentId       = commentId,
                VoteScore       = commentAggregate.VoteScore,
                CurrentUserVote = commentAggregate.CurrentUserVote
            };

            return(Json(model, JsonRequestBehavior.AllowGet));
        }
コード例 #3
0
        public async Task PostFromAnAuthenticatedUserWithExistingVoteWithDifferentValue_DeletesVoteAndReturnsResultWithNewValue()
        {
            const int  voteValue  = 1;
            const int  accountId  = 7;
            const long commentId  = 3;
            var        systemTime = new DateTime(2017, 10, 22, 7, 31, 53);

            TestableCommentController controller = TestableCommentController.Create();

            controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(accountId);
            controller.MockSystemClock.Setup(x => x.UtcNow).Returns(systemTime);

            controller.VoteRepository.Votes.Add(new Core.Entities.Vote {
                CommentId         = commentId,
                CastedByAccountId = accountId,
                Value             = -1,
                CastedOn          = new DateTime(2017, 10, 11, 7, 31, 53)
            });

            controller.CommentRepository.Comments = new List <Comment> {
                new Comment {
                    Id = commentId, CreatedByAccountId = 9
                }
            };

            controller.CommentRepository.CommentAggregates = new List <CommentAggregate> {
                new CommentAggregate {
                    Id = commentId, CreatedByAccountId = 9, VoteScore = 3, CurrentUserVote = voteValue
                }
            };

            JsonResult result = await controller.Vote(new CommentVoteViewModel { CommentId = commentId, Value = voteValue }) as JsonResult;

            Assert.IsNotNull(result);

            CommentVoteViewModel model = result.Data as CommentVoteViewModel;

            Assert.IsNotNull(model);
            Assert.AreEqual(commentId, model.CommentId);
            Assert.AreEqual(3, model.VoteScore);
            Assert.AreEqual(voteValue, model.CurrentUserVote);

            Core.Entities.Vote vote = controller.VoteRepository.Votes.FirstOrDefault();
            Assert.IsNotNull(vote);
            Assert.AreEqual(commentId, vote.CommentId);
            Assert.AreEqual(voteValue, vote.Value);
            Assert.AreEqual(accountId, vote.CastedByAccountId);
            Assert.AreEqual(systemTime, vote.CastedOn);
        }
コード例 #4
0
        public async Task PostFromAnAuthenticatedUserWithAnExistingVoteWithSameValue_DeletesVoteAndReturnsResultWithANeutralValue()
        {
            const int  voteValue = 1;
            const int  accountId = 7;
            const long commentId = 3;

            TestableCommentController controller = TestableCommentController.Create();

            controller.MockContextService.Setup(x => x.CurrentUserAccountId).Returns(accountId);

            controller.VoteRepository.Votes.Add(new Core.Entities.Vote {
                CommentId         = commentId,
                CastedByAccountId = accountId,
                Value             = voteValue
            });

            controller.CommentRepository.Comments = new List <Comment> {
                new Comment {
                    Id = commentId, CreatedByAccountId = 9
                }
            };

            controller.CommentRepository.CommentAggregates = new List <CommentAggregate> {
                new CommentAggregate {
                    Id = commentId, CreatedByAccountId = 9, VoteScore = 3, CurrentUserVote = null
                }
            };

            JsonResult result = await controller.Vote(new CommentVoteViewModel { CommentId = commentId, Value = voteValue }) as JsonResult;

            Assert.IsNotNull(result);

            CommentVoteViewModel model = result.Data as CommentVoteViewModel;

            Assert.IsNotNull(model);
            Assert.AreEqual(commentId, model.CommentId);
            Assert.AreEqual(0, model.Value);
            Assert.AreEqual(3, model.VoteScore);
            Assert.IsNull(model.CurrentUserVote);
            Assert.IsEmpty(controller.VoteRepository.Votes);
        }