public async Task <PollPreview> GetByIdAsync(int id) { var poll = await _pollService.GetByIdAsync(id); if (poll == null) { return(null); } var pollPreview = _mapper.Map <PollPreview>(poll); var pollAnswers = await _pollAnswerService.GetByPollIdAsync(id); var total = pollAnswers.Count; var pollPreviewAnswers = _mapper.Map <ICollection <PollPreviewAnswer> >(pollAnswers); var answersByOption = pollPreviewAnswers .GroupBy(x => x.PollOptionId) .ToDictionary(x => x.Key, x => x.ToList()); foreach (var option in pollPreview.Options) { if (!answersByOption.TryGetValue(option.Id, out var answers)) { continue; } var totalOptionAnswers = answers.Count; option.TotalAnswers = totalOptionAnswers; option.AnswersPercent = (decimal)totalOptionAnswers / total * 100; option.Answers = poll.Type == PollType.Public ? answers : new List <PollPreviewAnswer>(); } return(pollPreview); }
public async Task <IActionResult> GetByPollId(int pollId) { var result = await _pollAnswerService.GetByPollIdAsync(pollId); return(Ok(result)); }