Ejemplo n.º 1
0
 public IActionResult GetVotingSummary(Guid sessionId)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     if (sessionId == Guid.Empty)
     {
         return(BadRequest("SessionId is an empty Guid"));
     }
     try
     {
         var votings       = Service.GetVotingsBySessionId(sessionId).ToList();
         var votingSummary = new VotingSummary
         {
             SessionId = sessionId,
             UpVotes   = votings.Where(v => v.Value > 0)
                         .Aggregate(0, (acc, x) => acc + x.Value),
             DownVotes = votings.Where(v => v.Value < 0)
                         .Aggregate(0, (acc, x) => acc + (x.Value * -1))
         };
         return(Ok(votingSummary));
     }
     catch (IndexOutOfRangeException)
     {
         return(BadRequest());
     }
 }
Ejemplo n.º 2
0
        public void DisplayResults(VotingSummary results)
        {
            IEnumerable <CandidateVotes> candidateResults = results.AggregatedVoteses;

            if (candidateResults.Count() > 0)
            {
                lblNoResults.Visible           = false;
                dgvResults.AutoGenerateColumns = false;
                dgvResults.DataSource          = candidateResults;
                dgvResults.Visible             = true;
            }
            else
            {
                dgvResults.Visible   = false;
                lblNoResults.Visible = true;
            }

            lblInvalidVotes.Text   = results.InvalidVotes.ToString();
            lblIssuedBallouts.Text = results.IssuedBallots.ToString();
        }
Ejemplo n.º 3
0
        public void VotingSummary_SholdCountAllVotes()
        {
            // Given
            _constituencyA.InvalidVotes  = 1;
            _constituencyA.IssuedBallots = 2;
            _constituencyA.Votes         = new Dictionary <CandidatesModel, int>();
            _constituencyA.Votes.Add(_candidateA, 1000);
            _constituencyA.Votes.Add(_candidateB, 2000);

            _constituencyB.InvalidVotes  = 3;
            _constituencyB.IssuedBallots = 4;
            _constituencyB.Votes         = new Dictionary <CandidatesModel, int>();
            _constituencyB.Votes.Add(_candidateA, 3000);
            _constituencyB.Votes.Add(_candidateB, 4000);

            _repositoryMock.Setup(t => t.GetConstituencies())
            .Returns(() => new[] { _constituencyA, _constituencyB }.AsQueryable());

            // When
            var votingSummaryResult = _service.GetVotingSummary(null);

            // Then
            var votingSummaryExpected = new VotingSummary()
            {
                InvalidVotes      = 4,
                IssuedBallots     = 6,
                AggregatedVoteses = new[]
                {
                    new CandidateVotes {
                        CandidateId = _candidateA.Id, CandidateName = _candidateA.Name, Amount = 4000
                    },
                    new CandidateVotes {
                        CandidateId = _candidateB.Id, CandidateName = _candidateB.Name, Amount = 6000
                    },
                }
            };

            Assert.IsNotNull(votingSummaryResult);
            Assert.AreEqual(votingSummaryExpected, votingSummaryResult);
        }