コード例 #1
0
        public async Task <ActionResult <PollView> > Get(Guid id, OptionSorting sort = OptionSorting.Votes)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Invalid payload"));
            }

            var poll = await pollService.GetPollViewAsync(id, sort);

            if (poll is null)
            {
                return(NotFound("Poll doesn't exist"));
            }

            return(Ok(poll));
        }
コード例 #2
0
        public async Task <PollView?> GetPollViewAsync(Guid id, OptionSorting sort = OptionSorting.Votes)
        {
            var poll = await GetPollAsync(id);

            var view = PollView.Of(poll);

            if (view is not null)
            {
                view.Options = sort switch
                {
                    OptionSorting.Name => view.Options.OrderBy(option => option.Description),
                    OptionSorting.Votes => view.Options.OrderBy(option => option.Votes),
                    _ => throw new NotImplementedException(),
                };
                view.Options = view.Options.ToList();
                MathUtils.NormalizePercentages(view.Options);
                view.Voters = pollRepository.GetVotersCount(id);
            }
            return(view);
        }