Пример #1
0
        public async Task <ActionResult <EventDto> > AddVotes(long id, [FromBody] VoteRequest request)
        {
            var command  = new VoteCommand(id, request.Name, request.Votes);
            var eventDto = await _mediator.Send(command);

            return(Ok(eventDto));
        }
Пример #2
0
        public async Task Should_HandleUpdatingDownVoteToUpVote()
        {
            string user1Id = (await TestUtil.CreateNewUser(AppTestSettings.Instance.TestDBConnectionString)).ToString();

            Symbol jpmSymbol = await _symbolRepo.GetSymbol("JPM");

            var downVoteCommand = new VoteCommand {
                SymbolId = jpmSymbol.Id, Direction = VoteDirection.DownVote
            };

            await SendVoteForCustomUser(downVoteCommand, user1Id);

            var upVoteCommand = new VoteCommand {
                SymbolId = jpmSymbol.Id, Direction = VoteDirection.UpVote
            };

            await SendVoteForCustomUser(upVoteCommand, user1Id);

            Dictionary <int, TotalVotes> voteData = await GetVoteData(user1Id, new int[] { jpmSymbol.Id });

            Assert.Single(voteData);
            Assert.True(voteData.ContainsKey(jpmSymbol.Id));

            var totalVotes = voteData[jpmSymbol.Id];

            Assert.Equal(1, totalVotes.Count);
            Assert.Equal(VoteDirection.UpVote, totalVotes.MyVoteDirection);
            Assert.Equal(user1Id, totalVotes.UserId.ToString());
        }
Пример #3
0
        public async Task <IActionResult> SaveVote(VoteCommand command)
        {
            Guid userId = User.GetUserId();
            var  vote   = new Vote(userId, command.SymbolId, command.Direction, DateTime.UtcNow);

            await _voteRepo.SaveVote(vote);

            return(Ok());
        }
Пример #4
0
        public async Task <IActionResult> MakeDecision(int id, [FromBody] VoteCommand command)
        {
            command.EvaluationId = id;
            command.UserId       = UserId;

            var result = await Mediator.Send(command);

            return(Ok(result));
        }
Пример #5
0
        private async Task SendVoteForCustomUser(VoteCommand voteCommand, string userId)
        {
            var httpMessage = new HttpRequestMessage(HttpMethod.Post, ApiPath.Votes);

            httpMessage.Headers.Add(TestAuthHandler.CustomUserIdHeader, userId);
            httpMessage.Content = voteCommand.ToJsonPayload();

            var saveResponse = await _client.SendAsync(httpMessage);

            saveResponse.EnsureSuccessStatusCode();
        }
Пример #6
0
        public async Task Should_SumVotesForMultipleUserAndSymbols()
        {
            string user1Id = (await TestUtil.CreateNewUser(AppTestSettings.Instance.TestDBConnectionString)).ToString();
            string user2Id = (await TestUtil.CreateNewUser(AppTestSettings.Instance.TestDBConnectionString)).ToString();
            string user3Id = (await TestUtil.CreateNewUser(AppTestSettings.Instance.TestDBConnectionString)).ToString();

            Symbol jpmSymbol = await _symbolRepo.GetSymbol("JPM");

            Symbol bacSymbol = await _symbolRepo.GetSymbol("BAC");

            var voteCommandJpm1 = new VoteCommand {
                SymbolId = jpmSymbol.Id, Direction = VoteDirection.DownVote
            };
            var voteCommandJpm2 = new VoteCommand {
                SymbolId = jpmSymbol.Id, Direction = VoteDirection.None
            };
            var voteCommandJpm3 = new VoteCommand {
                SymbolId = jpmSymbol.Id, Direction = VoteDirection.UpVote
            };

            var voteCommandBac1 = new VoteCommand {
                SymbolId = bacSymbol.Id, Direction = VoteDirection.DownVote
            };
            var voteCommandBac2 = new VoteCommand {
                SymbolId = bacSymbol.Id, Direction = VoteDirection.DownVote
            };
            var voteCommandBac3 = new VoteCommand {
                SymbolId = bacSymbol.Id, Direction = VoteDirection.DownVote
            };

            await SendVoteForCustomUser(voteCommandJpm1, user1Id);
            await SendVoteForCustomUser(voteCommandJpm2, user2Id);
            await SendVoteForCustomUser(voteCommandJpm3, user3Id);

            await SendVoteForCustomUser(voteCommandBac1, user1Id);
            await SendVoteForCustomUser(voteCommandBac2, user2Id);
            await SendVoteForCustomUser(voteCommandBac3, user3Id);

            Dictionary <int, TotalVotes> voteData = await GetVoteData(user1Id, new int[] { jpmSymbol.Id, bacSymbol.Id });

            Assert.Equal(2, voteData.Count);
            Assert.True(voteData.ContainsKey(jpmSymbol.Id));
            Assert.True(voteData.ContainsKey(bacSymbol.Id));

            var jpmVoteData = voteData[jpmSymbol.Id];
            var bacVoteData = voteData[bacSymbol.Id];

            Assert.Equal(0, jpmVoteData.Count);
            Assert.Equal(-3, bacVoteData.Count);

            Assert.Equal(voteCommandJpm1.Direction, jpmVoteData.MyVoteDirection);
            Assert.Equal(voteCommandBac1.Direction, bacVoteData.MyVoteDirection);
        }
Пример #7
0
        public void ToVote(VoteCommand command)
        {
            //var vote = _context.Votes.Find(command.Id);
            var vote = _context.Votes.Include(v => v.VoteItems).Single(x => x.Id == command.Id);

            if (vote == null)
            {
                throw new Exception("Unable to find the vote");
            }

            command.ToVote(vote);
            _context.SaveChanges();
        }
        public void Handle_ShouldThrowException_WhenEventIsNotFound()
        {
            var eventId = 1L;
            var name    = "Name";
            var date    = new LocalDate(2000, 01, 01);
            var dates   = new List <LocalDate> {
                date
            };
            var command = new VoteCommand(eventId, name, dates);

            _eventRepository
            .GetById(eventId, new CancellationToken())
            .Returns((EventEntity)null);

            Should.Throw <NotFoundException>(() => _handler.Handle(command, new CancellationToken()));
        }
        public void Handle_ShouldVote()
        {
            var eventId = 1L;
            var name    = "Name";
            var date    = new LocalDate(2000, 01, 01);
            var dates   = new List <LocalDate> {
                date
            };
            var command = new VoteCommand(eventId, name, dates);

            _eventRepository.GetById(eventId, new CancellationToken())
            .Returns(new EventEntity(name, dates));

            var result = _handler.Handle(command, new CancellationToken());

            result.Result.Votes.Single().Date.ShouldBe(date);
            result.Result.Votes.Single().People.Single().ShouldBe(name);
        }
        public void Handle_ShouldSaveVote()
        {
            var eventId = 1L;
            var name    = "Name";
            var date    = new LocalDate(2000, 01, 01);
            var dates   = new List <LocalDate> {
                date
            };
            var command = new VoteCommand(eventId, name, dates);

            _eventRepository
            .GetById(eventId, new CancellationToken())
            .Returns(new EventEntity(name, dates));

            var result = _handler.Handle(command, new CancellationToken());

            _unitOfWork.Received(1).Save(Arg.Any <CancellationToken>());
        }
Пример #11
0
        public async Task Should_GetVotesOnlyWhenSymbolMatches()
        {
            Symbol jpmSymbol = await _symbolRepo.GetSymbol("JPM");

            var voteCommand = new VoteCommand {
                SymbolId = jpmSymbol.Id, Direction = VoteDirection.UpVote
            };

            var saveResponse = await _client.PostAsync(ApiPath.Votes, voteCommand.ToJsonPayload());

            saveResponse.EnsureSuccessStatusCode();

            var fetchResponse = await _client.GetAsync($"{ApiPath.Votes}?sid={int.MaxValue}");

            Dictionary <int, TotalVotes> voteData = await fetchResponse.Content.ReadAsAsync <Dictionary <int, TotalVotes> >();

            Assert.Empty(voteData);
        }
Пример #12
0
        public IActionResult Vote(VoteCommand command)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _voteService.ToVote(command);
                    // 默认这个跳转并不会刷新,需要进一步研究,搜索“ASP.NET Core 中的响应缓存”,目前是由前端刷新
                    return(RedirectToAction(nameof(Vote), new { id = command.Id }));
                }
            }
            catch (Exception)
            {
                // Add a model-level error by using an empty string key
                ModelState.AddModelError(string.Empty, "An error occured to vote");
            }

            // If we got to here, something went wrong
            return(RedirectToAction(nameof(Vote), new { id = command.Id }));
        }
Пример #13
0
        public Configuration(JObject j)
        {
            VoteCommand = j["VoteCommand"]?.Value <string>();
            if (string.IsNullOrWhiteSpace(VoteCommand))
            {
                throw new RuleImportException("'VoteCommand' must be specified.");
            }
            if (VoteCommand.Contains(" "))
            {
                throw new RuleImportException("'VoteCommand' must not contain spaces.");
            }

            TempChannelName = ParseChannelNameConfig(j, "TempChannelName");
            VoteChannel     = ParseChannelNameConfig(j, "VoteChannel");

            var vptProp = j["VotePassThreshold"];

            if (vptProp == null)
            {
                throw new RuleImportException("'VotePassThreshold' must be specified.");
            }
            if (vptProp.Type != JTokenType.Integer)
            {
                throw new RuleImportException("'VotePassThreshold' must be an integer.");
            }
            VotePassThreshold = vptProp.Value <int>();
            if (VotePassThreshold <= 0)
            {
                throw new RuleImportException("'VotePassThreshold' must be greater than zero.");
            }

            ChannelDuration = ParseTimeConfig(j, "ChannelDuration");
            VotingDuration  = ParseTimeConfig(j, "VotingDuration");
            VotingCooldown  = ParseTimeConfig(j, "VotingCooldown");

            VoteStarters = new EntityList(j["VoteStarters"]);
            // Accepts empty lists.
        }
Пример #14
0
        public async Task Should_StillReturnTotalVote_WhenUserHasNeverVoted()
        {
            string user1Id = (await TestUtil.CreateNewUser(AppTestSettings.Instance.TestDBConnectionString)).ToString();
            string user2Id = (await TestUtil.CreateNewUser(AppTestSettings.Instance.TestDBConnectionString)).ToString();

            Symbol jpmSymbol = await _symbolRepo.GetSymbol("JPM");

            var voteCommand = new VoteCommand {
                SymbolId = jpmSymbol.Id, Direction = VoteDirection.DownVote
            };

            await SendVoteForCustomUser(voteCommand, user1Id);

            Dictionary <int, TotalVotes> voteData = await GetVoteData(user2Id, new int[] { jpmSymbol.Id });

            Assert.Single(voteData);
            Assert.True(voteData.ContainsKey(jpmSymbol.Id));

            var jpmVoteData = voteData[jpmSymbol.Id];

            Assert.Equal((int)VoteDirection.DownVote, jpmVoteData.Count);

            Assert.Equal(VoteDirection.None, jpmVoteData.MyVoteDirection);
        }
Пример #15
0
 public void VoteChanged()
 {
     VoteCommand.RaiseCanExecuteChanged();
 }
Пример #16
0
 public async Task <IActionResult> Vote(VoteCommand request)
 {
     return(Ok(await _mediator.Send(request)));
 }
        public IHttpActionResult Post(VoteCommand command)
        {
            new VoteCommandHandler().Dispatch(command);

            return Ok();
        }