コード例 #1
0
        private async Task <JsonResult> NotifyClientsAndGenerateVoteResultAsync(long countdownId, int currentUserAccountId)
        {
            CountdownAggregate countdownAggregate = await _countdownRepository.GetAggregateAsync(countdownId, currentUserAccountId);

            await _notificationService.ClearCountdownVoteNotificationsAsync(countdownId, countdownAggregate.CreatedByAccountId, currentUserAccountId);

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

            _notificationService.UpdateClientsAfterVote(countdownAggregate);

            var model = new CountdownVoteViewModel {
                CountdownId     = countdownId,
                VoteScore       = countdownAggregate.VoteScore,
                CurrentUserVote = countdownAggregate.CurrentUserVote
            };

            return(Json(model, JsonRequestBehavior.AllowGet));
        }
コード例 #2
0
        public async Task <ActionResult> Vote(CountdownVoteViewModel model)
        {
//            await Task.Delay(2000);
            Countdown countdown = await _countdownRepository.GetAsync(model.CountdownId);

            if (ModelState.IsValid && countdown.CreatedByAccountId != _contextService.CurrentUserAccountId)
            {
                Vote existingVote = await _voteRepository.GetByCountdownIdAsync(model.CountdownId, (int)_contextService.CurrentUserAccountId);

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

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

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

                return(await NotifyClientsAndGenerateVoteResultAsync(model.CountdownId, (int)_contextService.CurrentUserAccountId));
            }
            return(new HttpStatusCodeResult(400, "Bad Request"));
        }
コード例 #3
0
        public async Task PostFromAnAuthenticatedUserWithExistingVoteWithDifferentValue_DeletesVoteAndReturnsResultWithNewValue()
        {
            const int  voteValue   = 1;
            const int  accountId   = 7;
            const long countdownId = 3;
            var        systemTime  = new DateTime(2017, 10, 22, 7, 31, 53);

            TestableCountdownController controller = TestableCountdownController.Create();

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

            controller.CountdownRepository.Countdowns = new List <Countdown> {
                new Countdown {
                    Id = countdownId, CreatedByAccountId = 9
                }
            };

            controller.CountdownRepository.CountdownAggregates = new List <CountdownAggregate> {
                new CountdownAggregate {
                    Id = countdownId, CreatedByAccountId = 9, VoteScore = 3, CurrentUserVote = voteValue
                }
            };

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

            JsonResult result = await controller.Vote(new CountdownVoteViewModel { CountdownId = countdownId, Value = voteValue }) as JsonResult;

            Assert.IsNotNull(result);

            CountdownVoteViewModel model = result.Data as CountdownVoteViewModel;

            Assert.IsNotNull(model);
            Assert.AreEqual(countdownId, model.CountdownId);
            Assert.AreEqual(3, model.VoteScore);
            Assert.AreEqual(voteValue, model.CurrentUserVote);

            Core.Entities.Vote vote = controller.VoteRepository.Votes.FirstOrDefault();
            Assert.IsNotNull(vote);
            Assert.AreEqual(countdownId, vote.CountdownId);
            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 countdownId = 3;

            TestableCountdownController controller = TestableCountdownController.Create();

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

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

            controller.CountdownRepository.Countdowns = new List <Countdown> {
                new Countdown {
                    Id = countdownId, CreatedByAccountId = 9
                }
            };

            controller.CountdownRepository.CountdownAggregates = new List <CountdownAggregate> {
                new CountdownAggregate {
                    Id = countdownId, CreatedByAccountId = 9, VoteScore = 3, CurrentUserVote = null
                }
            };

            JsonResult result = await controller.Vote(new CountdownVoteViewModel { CountdownId = countdownId, Value = voteValue }) as JsonResult;

            Assert.IsNotNull(result);

            CountdownVoteViewModel model = result.Data as CountdownVoteViewModel;

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