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); }
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); }
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); }
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); }
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); }
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; } }
public async Task <long> CountShowsAsync(Expression <Func <Show, bool> > predicate) { return(await _context.Set <Show>().CountAsync(predicate)); }