Пример #1
0
        public async Task <List <ShowViewModel> > GetShowsAsync(int pageNumber, int pageSize)
        {
            var skipCount    = pageNumber * pageSize;
            var showEntities = await _showsRepository.GetShowsAsync(skipCount, pageSize);

            var shows = Mapper.Map <List <ShowViewModel> >(showEntities);

            shows.ForEach(show => { show.Casts = show.Casts.OrderByDescending(p => p.BirthDay).ToList(); });
            return(shows);
        }
Пример #2
0
        public async Task <IActionResult> CountVotesAsync()
        {
            var table      = _context.GetTargetTable <User>();
            var voteResult = await table.GetItemAsync("VoteResult");

            if (voteResult != null)
            {
                return(NoContent());
            }

            var showIdToPhoneNumbers = (await _showsRepository.GetShowsAsync())
                                       .ToDictionary(show => show.Id, ignore => new List <string>());

            var users = await _usersRepository.GetUsersAsync();

            int totalVotes = 0;

            foreach (var user in users)
            {
                foreach (var showId in user.Votes)
                {
                    showIdToPhoneNumbers[showId].Add(user.Id);
                    totalVotes++;
                }
            }

            if (totalVotes == 0)
            {
                return(BadRequest("No votes"));
            }

            var result = showIdToPhoneNumbers.Select(pair =>
            {
                var doc           = new Document();
                doc["showId"]     = pair.Key;
                doc["votesCount"] = pair.Value.Count;
                doc["votes"]      = pair.Value;
                doc["voteRate"]   = (pair.Value.Count / (decimal)totalVotes).ToString("P");
                return(doc);
            })
                         .OrderByDescending(show => show["votesCount"].AsInt())
                         .ToList();

            var doc = new Document();

            doc["Id"]     = "VoteResult";
            doc["Result"] = result;
            await table.PutItemAsync(doc);

            return(NoContent());
        }
Пример #3
0
 public async Task <List <Show> > GetShowsAsync()
 {
     return(await _showsRepository.GetShowsAsync());
 }