public async Task SynchronizeAsync() { Logger.Information("Starting shows synchronization"); const int pageSize = 250; var latestShowId = await _showsRepository.GetMaxShowIdAsync(); var currentPage = latestShowId / pageSize; List <Show> newShows; do { newShows = await _tvMazeApi.GetShowsAsync(currentPage); var newShowsToAdd = newShows.Where(p => p.Id > latestShowId); foreach (var newShow in newShowsToAdd) { var showCasts = await _tvMazeApi.GetCastAsync(newShow.Id); var showEntity = MapShowEntity(newShow, showCasts); await _showsRepository.AddNewShowAsync(showEntity); if (_cts.IsCancellationRequested) { break; } } if (_cts.IsCancellationRequested) { break; } currentPage++; } while (newShows.Any()); Logger.Information("Finished shows synchronization"); }