Exemplo n.º 1
0
        private void SelectCandidate_Click(object sender, RoutedEventArgs e)
        {
            Button   Sender           = (Button)sender;
            long     Id               = (long)Sender.Tag;
            Category selectedCategory = (Category)CategoryList.SelectedItem;

            if (selectedCategory.CountSelected > selectedCategory.MaxVote - 1)
            {
                new Views.ErrorWindow("Maximum number of candidates selected!!!").ShowDialog();
                return;
            }
            foreach (Candidate candidate in Candidates)
            {
                if (candidate.UniqueId == Id)
                {
                    if (candidate.Selected)
                    {
                        break;
                    }
                    VoteFor.Add(candidate);
                    candidate.Selected              = true;
                    selectedCategory.CountSelected += 1;
                }
            }
        }
Exemplo n.º 2
0
        public async Task Should_Return_Error_On_Twice_Vote_On_Same_Question()
        {
            await LogInAsAdministrator();

            var createQuestion = new CreateQuestion(
                questionText: "Some question?",
                answers: new[] { "First answer", "Second answer" });
            var createQuestionResponse = await Mediator.Send(createQuestion);

            var createdQuestionId = AssertNotError.Of(createQuestionResponse);

            var getQuestion      = new GetQuestion(createdQuestionId);
            var questionResponse = await Mediator.Send(getQuestion);

            var createdQuestion = AssertNotError.Of(questionResponse);

            await LogInAsVoter();

            var voteFor = new VoteFor(
                questionId: createdQuestion.Id,
                answerId: createdQuestion.Answers[1].Id);
            var voteForResponse = await Mediator.Send(voteFor);

            var voteForDuplicate = new VoteFor(
                questionId: createdQuestion.Id,
                answerId: createdQuestion.Answers[0].Id);
            var voteForResponseDuplicate = await Mediator.Send(voteForDuplicate);

            AssertError <DomainError> .Of(voteForResponseDuplicate);
        }
Exemplo n.º 3
0
        public override async Task Execute(IReadOnlyList <string> args)
        {
            var selectedQuestion = await CommandLineHelper.AskToSelectQuestion(_mediator);

            if (selectedQuestion is null)
            {
                return;
            }

            var selectedAnswer = CommandLineHelper.AskToSelectAnswer(selectedQuestion);

            if (selectedAnswer is null)
            {
                return;
            }

            var voteFor = new VoteFor(
                selectedQuestion.Id,
                selectedAnswer.Id);

            try
            {
                await _mediator.Send(voteFor)
                .OnError(error => throw new InvalidOperationException(error.ToString()));
            }
            catch (Exception exception)
            {
                exception.WriteToConsole();
                return;
            }

            Console.WriteLine($"Voted for: {selectedAnswer.Text}");
        }
Exemplo n.º 4
0
        public async Task Check_If_Votes_Incremented()
        {
            await LogInAsAdministrator();

            var createQuestion = new CreateQuestion(
                questionText: "Some question?",
                answers: new[] { "First answer", "Second answer" });
            var createQuestionResponse = await Mediator.Send(createQuestion);

            var createdQuestionId = AssertNotError.Of(createQuestionResponse);

            var getQuestion      = new GetQuestion(createdQuestionId);
            var questionResponse = await Mediator.Send(getQuestion);

            var createdQuestion = AssertNotError.Of(questionResponse);

            await LogInAsVoter();

            var voteFor = new VoteFor(
                questionId: createdQuestion.Id,
                answerId: createdQuestion.Answers[1].Id);
            var voteForResponse = await Mediator.Send(voteFor);

            var getQuestionResult      = new GetQuestionResult(createdQuestion.Id);
            var questionResultResponse = await Mediator.Send(getQuestionResult);

            AssertNotError.Of(voteForResponse);
            var questionResult = AssertNotError.Of(questionResultResponse);

            Assert.Multiple(() =>
            {
                Assert.That(questionResult.Answers[0].Votes, Is.EqualTo(0));
                Assert.That(questionResult.Answers[1].Votes, Is.EqualTo(1));
            });
        }
Exemplo n.º 5
0
        public async Task Should_Return_Error_When_Not_Logged()
        {
            await LogInAsAdministrator();

            var createQuestion = new CreateQuestion(
                questionText: "Some question?",
                answers: new[] { "First answer", "Second answer" });
            var createQuestionResponse = await Mediator.Send(createQuestion);

            var createdQuestionId = AssertNotError.Of(createQuestionResponse);

            var getQuestion      = new GetQuestion(createdQuestionId);
            var questionResponse = await Mediator.Send(getQuestion);

            var createdQuestion = AssertNotError.Of(questionResponse);

            LogOut();

            var voteFor = new VoteFor(
                questionId: createdQuestion.Id,
                answerId: createdQuestion.Answers[1].Id);
            var voteForResponse = await Mediator.Send(voteFor);

            AssertError <AuthorizationError> .Of(voteForResponse);
        }
        public async Task Check_If_Votes_Exists_In_My_Votes_After_Voting_2_Of_3_Questions()
        {
            await LogInAsAdministrator();

            var createFirstQuestion = new CreateQuestion(
                questionText: "Some question?",
                answers: new[] { "First answer", "Second answer" });
            var createFirstQuestionResponse = await Mediator.Send(createFirstQuestion);

            var firstQuestionId = AssertNotError.Of(createFirstQuestionResponse);

            var getFirstQuestion      = new GetQuestion(firstQuestionId);
            var firstQuestionResponse = await Mediator.Send(getFirstQuestion);

            var firstQuestion = AssertNotError.Of(firstQuestionResponse);

            var createSecondQuestion = new CreateQuestion(
                questionText: "Another question?",
                answers: new[] { "Any answer", "Some answer" });
            var createSecondQuestionResponse = await Mediator.Send(createSecondQuestion);

            _ = AssertNotError.Of(createSecondQuestionResponse); // I will not vote on this question

            var createThirdQuestion = new CreateQuestion(
                questionText: "Third question?",
                answers: new[] { "Any answer", "Some answer", "I don't know answer" });
            var createThirdQuestionResponse = await Mediator.Send(createThirdQuestion);

            var thirdQuestionId = AssertNotError.Of(createThirdQuestionResponse);

            var getThirdQuestion      = new GetQuestion(thirdQuestionId);
            var thirdQuestionResponse = await Mediator.Send(getThirdQuestion);

            var thirdQuestion = AssertNotError.Of(thirdQuestionResponse);

            await LogInAsVoter();

            var voteForFirst = new VoteFor(
                questionId: firstQuestion.Id,
                answerId: firstQuestion.Answers[0].Id);
            await Mediator.Send(voteForFirst);

            var voteForThird = new VoteFor(
                questionId: thirdQuestion.Id,
                answerId: thirdQuestion.Answers[2].Id);
            await Mediator.Send(voteForThird);

            var getMyVotes         = new GetMyVotes();
            var getMyVotesResponse = await Mediator.Send(getMyVotes);

            var myVotes = AssertNotError.Of(getMyVotesResponse);

            CollectionAssert.AreEquivalent(
                new[] { firstQuestion.Id, thirdQuestion.Id },
                myVotes.QuestionsId);
        }
Exemplo n.º 7
0
        private void VoteSelected_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;

            foreach (Candidate candidate in VoteFor)
            {
                if (candidate.UniqueId == (long)button.Tag)
                {
                    VoteFor.Remove(candidate);
                    TheClient.Vote(candidate);
                    candidate.Voted = true;
                    CancellationTokenSource c = new CancellationTokenSource();
                    if (cancellationTokenSources.Count > 0)
                    {
                        cancellationTokenSources[cancellationTokenSources.Count - 1].Cancel();
                    }
                    cancellationTokenSources.Add(c);
                    NotifyStatus(String.Format("Vote Sent For {0}", candidate.CandidateName), "#FF8AFF8A", c.Token);
                    return;
                }
            }
        }
Exemplo n.º 8
0
        public async Task VoteRandomly()
        {
            if (_voterIdentity is null)
            {
                throw new InvalidOperationException(
                          $"call {nameof(LogInAsRandomVoter)} before");
            }

            await using var scope = _container.BeginLifetimeScope();
            var mediator = scope.Resolve <IMediator>();

            var authenticationService = scope.Resolve <AuthenticationService>();

            authenticationService.SetIdentity(_voterIdentity);

            var getQuestions         = new GetQuestions();
            var getQuestionsResponse = await mediator.Send(getQuestions);

            var questionsList = getQuestionsResponse
                                .OnError(error => throw new InvalidOperationException(error.ToString()));

            foreach (var question in questionsList.Questions)
            {
                var answerGuids = question.Answers
                                  .Select(a => a.Id)
                                  .ToArray();

                var selectedAnswerId = GetRandomGuid(answerGuids);

                var voteFor = new VoteFor(
                    questionId: question.Id,
                    answerId: selectedAnswerId);

                await mediator.Send(voteFor)
                .OnError(error => throw new InvalidOperationException(error.ToString()));
            }
        }
Exemplo n.º 9
0
        private void RemoveSelected_Click(object sender, RoutedEventArgs e)
        {
            Button   Sender           = (Button)sender;
            long     Id               = (long)Sender.Tag;
            Category selectedCategory = null;

            foreach (Candidate candidate in VoteFor)
            {
                if (candidate.UniqueId == Id)
                {
                    foreach (Category category in Categories)
                    {
                        if (category.Id == candidate.CatId)
                        {
                            selectedCategory = category;
                        }
                    }
                    VoteFor.Remove(candidate);
                    candidate.Selected              = false;
                    selectedCategory.CountSelected -= 1;
                    break;
                }
            }
        }
Exemplo n.º 10
0
        public async Task <IActionResult> Vote([FromBody] VoteFor request)
        {
            var response = await _mediator.Send(request);

            return(HandleErrors(response, _ => Ok()));
        }
Exemplo n.º 11
0
Arquivo: Log.cs Projeto: e2wugui/zeze
 internal bool CanVoteFor(string voteFor)
 {
     return(string.IsNullOrEmpty(VoteFor) || VoteFor.Equals(voteFor));
 }
        public async Task Check_If_Question_Results_Are_Valid()
        {
            await LogInAsAdministrator();

            var createFirstQuestion = new CreateQuestion(
                questionText: "Some question?",
                answers: new[] { "First answer", "Second answer" });
            var createFirstQuestionResponse = await Mediator.Send(createFirstQuestion);

            var firstQuestionId = AssertNotError.Of(createFirstQuestionResponse);

            var getFirstQuestion      = new GetQuestion(firstQuestionId);
            var firstQuestionResponse = await Mediator.Send(getFirstQuestion);

            var firstQuestion = AssertNotError.Of(firstQuestionResponse);

            var createSecondQuestion = new CreateQuestion(
                questionText: "Another question?",
                answers: new[] { "Any answer", "Some answer" });
            var createSecondQuestionResponse = await Mediator.Send(createSecondQuestion);

            var secondQuestionId = AssertNotError.Of(createSecondQuestionResponse);

            var getSecondQuestion      = new GetQuestion(secondQuestionId);
            var secondQuestionResponse = await Mediator.Send(getSecondQuestion);

            var secondQuestion = AssertNotError.Of(secondQuestionResponse);

            var firstVoteFor = new VoteFor(firstQuestion.Id, firstQuestion.Answers[0].Id);
            await Mediator.Send(firstVoteFor);

            var secondVoteFor = new VoteFor(secondQuestion.Id, secondQuestion.Answers[1].Id);
            await Mediator.Send(secondVoteFor);

            await LogInAsVoter();

            var thirdVoteFor = new VoteFor(firstQuestion.Id, firstQuestion.Answers[0].Id);
            await Mediator.Send(thirdVoteFor);

            var fourthVoteFor = new VoteFor(secondQuestion.Id, secondQuestion.Answers[0].Id);
            await Mediator.Send(fourthVoteFor);

            var getFirstResult      = new GetQuestionResult(firstQuestion.Id);
            var firstResultResponse = await Mediator.Send(getFirstResult);

            var firstResult = AssertNotError.Of(firstResultResponse);

            var getSecondResult      = new GetQuestionResult(secondQuestion.Id);
            var secondResultResponse = await Mediator.Send(getSecondResult);

            var secondResult = AssertNotError.Of(secondResultResponse);

            Assert.Multiple(() =>
            {
                Assert.That(firstResult.Answers[0].Votes, Is.EqualTo(2));
                Assert.That(firstResult.Answers[1].Votes, Is.EqualTo(0));

                Assert.That(secondResult.Answers[0].Votes, Is.EqualTo(1));
                Assert.That(secondResult.Answers[1].Votes, Is.EqualTo(1));
            });
        }
Exemplo n.º 13
0
 public async Task <KarmaValue> GetUserVote(VoteFor vote, GetUserVoteParams param)
 {
     return(await _karmaApi.GetUserVote(vote.ToString().ToLower(), param));
 }
Exemplo n.º 14
0
 public async Task <int> CountKarma(VoteFor vote, string itemId)
 {
     return(await _karmaApi.CountKarma(vote.ToString().ToLower(), itemId));
 }
Exemplo n.º 15
0
 public async Task RemoveVote(VoteFor vote, string itemId)
 {
     await _karmaApi.RemoveVote(vote.ToString().ToLower(), itemId);
 }