public async Task <List <ActorModel> > GetAllAsync(CancellationToken cancelationToken) { try { var actorList = await actorRepository.GetAllAsync(cancelationToken); if (actorList == null) { return(null); } actorList = actorList.OrderByDescending(a => a.Name).ToList(); Log.Information($"Actor list count: {actorList.Count} {nameof(GetAllAsync)}"); return(mapper.Map <List <ActorModel> >(actorList)); } catch (Exception ex) { Log.Error(ex, $"Error occurs in {nameof(GetAllAsync)}"); throw; } }
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { while (!stoppingToken.IsCancellationRequested) { try { var actorsFromWeb = await _IMDBScraperService.GetActorsAsync(stoppingToken).ConfigureAwait(false);; var actorsFromDB = await _actorRepository.GetAllAsync().ConfigureAwait(false); IList <Actor> actorToInsert = new List <Actor>(actorsFromWeb.Count()); IList <Actor> actorToUpdate = new List <Actor>(actorsFromDB.Count()); IList <Actor> actorToDelete = new List <Actor>(actorsFromDB.Count()); foreach (var actor in actorsFromWeb) { if (actorsFromDB.Any(a => a.Name.Trim().ToLower() == actor.Name.Trim().ToLower())) { actorToUpdate.Add(actor); } else { actorToInsert.Add(actor); } } foreach (var actor in actorsFromDB) { if (!actorsFromWeb.Any(a => a.Name.Trim().ToLower() == actor.Name.Trim().ToLower())) { actorToDelete.Add(actor); } } //Insert Actor foreach (var actor in actorToInsert) { try { await _actorRepository.InsertAsync(actor); _logger.LogInformation($"Actor: '{actor.Name}' Added"); } catch (Exception exp) { _logger.LogError($"Failed to add actor: '{actor.Name}'", exp); } } //Update Actor foreach (var actor in actorToUpdate) { try { await _actorRepository.UpdateAsync(actor); _logger.LogDebug($"Actor: '{actor.Name}' Updated"); } catch (Exception exp) { _logger.LogError($"Failed to update actor: '{actor.Name}'", exp); } } //Delete Actor foreach (var actor in actorToDelete) { try { await _actorRepository.DeleteAsync(actor); _logger.LogInformation($"Actor: '{actor.Name}' Deleted"); } catch (Exception exp) { _logger.LogError($"Failed to delete actor: '{actor.Name}'", exp); } } } catch (OperationCanceledException) { } catch (Exception ex) { ErrorHandle(ex); } } return; }