private PollResultsViewModel CalculateNewResult(Poll poll) { PollResultsViewModel resultVm = new PollResultsViewModel(); IEnumerable <PollVote> votes = pollDomainService.GetVotes(poll.Id); IEnumerable <KeyValuePair <Guid, int> > groupedVotes = from v in votes group v by v.PollOptionId into g select new KeyValuePair <Guid, int>(g.Key, g.Count()); resultVm.TotalVotes = votes.Count(); foreach (KeyValuePair <Guid, int> g in groupedVotes) { PollOptionResultsViewModel newOptionResult = new PollOptionResultsViewModel { OptionId = g.Key, VoteCount = g.Value, Percentage = ((g.Value / (decimal)resultVm.TotalVotes) * 100).ToString("N2", new CultureInfo("en-us")) }; resultVm.OptionResults.Add(newOptionResult); } return(resultVm); }
public void PollPassedToPollResultView() { // Arrange var fakeDB = new FakePollContext(); fakeDB.Polls = new FakePollSet(); fakeDB.Questions = new FakeDbSet <Question>(); fakeDB.Answers = new FakeDbSet <Answer>(); fakeDB.Responses = new FakeResponseSet(); fakeDB.Selections = new FakeDbSet <Selection>(); var poll1 = new Poll { ID = 1, Title = "Hello" }; fakeDB.Polls.Add(poll1); var answer1 = new Answer { ID = 1, Text = "Answer1", QuestionID = 1 }; fakeDB.Answers.Add(answer1); var question1 = new Question { ID = 1, Poll = poll1, Text = "Question1", Answers = new List <Answer> { answer1 } }; fakeDB.Questions.Add(question1); var selection1 = new Selection { ID = 1, Answer = answer1, AnswerID = 1 }; fakeDB.Selections.Add(selection1); var response1 = new Response { ID = 1, Poll = poll1, Selections = new List <Selection> { selection1 } }; fakeDB.Responses.Add(response1); PollsController controller = new PollsController(fakeDB); // Act ViewResult result = controller.Results(1) as ViewResult; PollResultsViewModel viewmodel = result.ViewData.Model as PollResultsViewModel; // Assert Assert.AreEqual(viewmodel.Poll.Title, "Hello"); }
public OperationResultVo PollVote(Guid currentUserId, Guid pollOptionId) { try { int pointsEarned = 0; Poll poll = pollDomainService.GetPollByOptionId(pollOptionId); if (poll == null) { return(new OperationResultVo("Unable to identify the poll you are voting for.")); } bool alreadyVoted = poll.Votes.Any(x => x.PollOptionId == pollOptionId && x.UserId == currentUserId); if (alreadyVoted) { return(new OperationResultVo("You already voted on this option.")); } IEnumerable <PollVote> userVotesOnThisPoll = poll.Votes.Where(x => x.UserId == currentUserId); if (poll.MultipleChoice || !userVotesOnThisPoll.Any()) { pollDomainService.AddVote(currentUserId, poll.Id, pollOptionId); } else { PollVote oldVote = userVotesOnThisPoll.FirstOrDefault(); if (oldVote != null) { pollDomainService.ReplaceVote(currentUserId, poll.Id, oldVote.PollOptionId, pollOptionId); } } if (!userVotesOnThisPoll.Any()) { pointsEarned = gamificationDomainService.ProcessAction(currentUserId, PlatformAction.PollVote); } unitOfWork.Commit(); PollResultsViewModel resultVm = CalculateNewResult(poll); return(new OperationResultVo <PollResultsViewModel>(resultVm, pointsEarned)); } catch (Exception ex) { return(new OperationResultVo(ex.Message)); } }
// GET: Polls/Results/5 public ActionResult Results(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } Poll poll = db.Polls.Find(id); if (poll == null) { return(HttpNotFound()); } PollResultsViewModel ViewModel = new PollResultsViewModel(poll); return(View(ViewModel)); }