public async Task <PollResultViewModel> GetResultAsync(long userId, long pollId) { var poll = await(from p in Context.Polls.Include(e => e.Choices) where p.Id == pollId select p).SingleOrDefaultAsync(); if (poll == null) { throw new EntityNotFoundException(typeof(Poll), pollId); } else { var r = new PollResultViewModel(); r.Question = poll.Question; var choices = new List <DecisionResultViewModel>(); foreach (var choice in poll.Choices) { choices.Add(new DecisionResultViewModel { Text = choice.Text, Count = await GetCount(userId, choice.Id), TotalCount = await GetTotalCount(poll.Id), IsYour = await IsYourDecision(userId, choice.Id), }); } r.Choices = choices; r.UserId = userId; return(r); } }
public IActionResult PollResult(PollViewModel model) { string userIp = _accessor.HttpContext.Connection.RemoteIpAddress.ToString(); _pollsRepository.InsertVote(model.SelectedOptionId, model.Id, userIp); _unitOfWork.Commit(); Poll poll = _pollsRepository.GetPollById(model.Id); List <PollOption> options = _pollsRepository.GetOptions(model.Id); int totalVotes = options.Sum(o => o.Votes); var pollResult = new PollResultViewModel { QuestionText = poll.QuestionText, TotalVotes = totalVotes, Items = options.Select(option => new PollResultItemViewModel { AnswerText = option.OptionText, Votes = option.Votes, Percentage = Math.Round((double)100 / totalVotes * option.Votes, 1) }) }; ViewBag.IsPostBack = true; return(View(pollResult)); }
public async Task <IEnumerable <PollResultViewModel> > GetAllResultsAsync(long userId) { var polls = await Context.Polls.Include(e => e.Choices).ThenInclude(f => f.Decisions).ToListAsync(); var result = new List <PollResultViewModel>(); foreach (var poll in polls) { var r = new PollResultViewModel(); r.Question = poll.Question; var choices = new List <DecisionResultViewModel>(); foreach (var choice in poll.Choices) { choices.Add(new DecisionResultViewModel { Text = choice.Text, Count = await GetCount(userId, choice.Id), TotalCount = await GetTotalCount(poll.Id), IsYour = await IsYourDecision(userId, choice.Id), }); } r.Choices = choices; result.Add(r); } return(result); }
public PollResultViewModel GetPollWithResult(int id) { PollPartRecord poll = GetPoll(id); PollResultViewModel model = new PollResultViewModel { Question = poll.Question, Shown = poll.Shown, Choices = GetChoices(id).Select(c => new PollResultEntry { Choice = c, Result = GetResult(c.Id) }).ToList() }; return(model); }