コード例 #1
0
        public IEnumerable <ShowViewModel> GetShows(int page, int pageSize)
        {
            var shows = tvMazeDbContext.Set <Show>()
                        .Include(s => s.ShowActors)
                        .ThenInclude(sp => sp.Actor)
                        .OrderBy(s => s.Id)
                        .Skip(page * pageSize)
                        .Take(pageSize)
                        .Select(show => new ShowViewModel
            {
                Id   = show.Id,
                Name = show.Name,
                Cast = show.ShowActors
                       .Select(c => c.Actor)
                       .OrderByDescending(a => a.DateOfBirth)
                       .Select(a =>
                               new CastViewModel
                {
                    Id       = a.Id,
                    Name     = a.Name,
                    BirthDay = a.DateOfBirth
                })
            });

            return(shows);
        }
コード例 #2
0
        private static List <ActorDto> GetActorsToAdd(TvMazeDbContext tvMazeDbContext, IEnumerable <ActorDto> actors)
        {
            var existingActorIds = tvMazeDbContext.Set <Actor>().Select(a => a.Id).ToList();
            var actorsToAdd      = actors.Where(actor => !existingActorIds.Contains(actor.Id))
                                   .GroupBy(a => a.Id).Select(a => a.First()).ToList();

            return(actorsToAdd);
        }
コード例 #3
0
        private async Task <bool> CheckIfShowExist(CancellationToken stoppingToken, TvMazeDbContext tvMazeDbContext,
                                                   ShowDto show)
        {
            if (await tvMazeDbContext.Set <Show>().AnyAsync(s => s.Id == show.Id, stoppingToken))
            {
                logger.LogInformation($"Show {show.Id} - {show.Name} already exists.");
                return(true);
            }

            return(false);
        }
コード例 #4
0
 private static async Task AddActors(CancellationToken stoppingToken, TvMazeDbContext tvMazeDbContext,
                                     IEnumerable <ActorDto> actorsToAdd)
 {
     await tvMazeDbContext.Set <Actor>().AddRangeAsync(actorsToAdd
                                                       .Select(actor => new Actor
     {
         Id          = actor.Id,
         Name        = actor.Name,
         DateOfBirth = actor.BirthDay
     }), stoppingToken);
 }
コード例 #5
0
 private static async Task AddShow(CancellationToken stoppingToken, TvMazeDbContext tvMazeDbContext, ShowDto show,
                                   IEnumerable <ActorDto> actorsToAdd)
 {
     await tvMazeDbContext.Set <Show>().AddAsync(new Show
     {
         Id         = show.Id,
         Name       = show.Name,
         ShowActors = actorsToAdd
                      .Select(actor => new ShowActor
         {
             ShowId = show.Id, ActorId = actor.Id
         }).ToList()
     }, stoppingToken);
 }
コード例 #6
0
        private static int CalculateNextPageNumber(TvMazeDbContext tvMazeDbContext, TvMazeOptions tvMazeConfiguration)
        {
            try
            {
                var page = 0;

                if (!tvMazeDbContext.Set <Show>().Any())
                {
                    return(page);
                }

                var lastAddedShowId = tvMazeDbContext.Set <Show>().OrderBy(s => s.Id).Last().Id;

                page = Convert.ToInt32(Math.Round(lastAddedShowId / tvMazeConfiguration.MaximumNumberOfShowsPerPage,
                                                  MidpointRounding.ToZero));

                return(page);
            }
            catch (Exception)
            {
                throw;
            }
        }
コード例 #7
0
 public async Task <long> CountShowsAsync(Expression <Func <Show, bool> > predicate)
 {
     return(await _context.Set <Show>().CountAsync(predicate));
 }