public async Task <bool> RefreshStoreFromCensus() { var result = new List <World>(); var createdEntities = new List <World>(); IEnumerable <CensusWorldModel> worlds = new List <CensusWorldModel>(); try { worlds = await _censusWorld.GetAllWorlds(); } catch { _logger.LogError("Census API query failed: get all Worlds. Refreshing store from backup..."); return(false); } if (worlds != null && worlds.Any()) { var censusEntities = worlds.Select(ConvertToDbModel); using (var factory = _dbContextHelper.GetFactory()) { var dbContext = factory.GetDbContext(); var storedEntities = await dbContext.Worlds.ToListAsync(); foreach (var censusEntity in censusEntities) { var storeEntity = storedEntities.FirstOrDefault(storedEntity => storedEntity.Id == censusEntity.Id); if (storeEntity == null) { createdEntities.Add(censusEntity); } else { storeEntity = censusEntity; dbContext.Worlds.Update(storeEntity); } } if (createdEntities.Any()) { await dbContext.Worlds.AddRangeAsync(createdEntities); } await dbContext.SaveChangesAsync(); _logger.LogInformation($"Refreshed Worlds store"); } return(true); } else { return(false); } }
public async Task RefreshStore() { var result = new List <World>(); var createdEntities = new List <World>(); var worlds = await _censusWorld.GetAllWorlds(); if (worlds != null) { var censusEntities = worlds.Select(ConvertToDbModel); using (var factory = _dbContextHelper.GetFactory()) { var dbContext = factory.GetDbContext(); var storedEntities = await dbContext.Worlds.ToListAsync(); foreach (var censusEntity in censusEntities) { var storeEntity = storedEntities.FirstOrDefault(storedEntity => storedEntity.Id == censusEntity.Id); if (storeEntity == null) { createdEntities.Add(censusEntity); } else { storeEntity = censusEntity; dbContext.Worlds.Update(storeEntity); } } if (createdEntities.Any()) { await dbContext.Worlds.AddRangeAsync(createdEntities); } await dbContext.SaveChangesAsync(); } } }