public async Task <IActionResult> SyncBusiness() { _logger.LogInformation("Start Re-syncing all businesses."); var regions = await _regionRepository.GetAll(); var allBusinesses = new List <BusinessCmsModel>(); _logger.LogInformation("Fetched all regions."); foreach (var r in regions) { allBusinesses.AddRange(await _cmsApiProxy.GetBusinesses(r.BusinessesApiPath)); } if (!allBusinesses.Any()) { return(BadRequest("No businesses to index.")); } var list = (await Task.WhenAll(allBusinesses.Select(MapToElasticModel))) .Where(result => result != null).ToList(); if (!list.Any() || list.Count <= 2) { _logger.LogWarning("Did not find any businesses or something went wrong when fetching businesses from cms-api. Count {BusinessCount}", list.Count); return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to fetch items.")); } var successful = await _businessRepository.DeleteIndex(); if (!successful) { _logger.LogError("Failed to delete index when syncing."); return(StatusCode(StatusCodes.Status500InternalServerError, "Failed to delete index.")); } _logger.LogInformation("Deleted elastic index for businesses."); var successfulInsert = await _businessRepository.Insert(list); if (!successfulInsert) { _logger.LogCritical("Insert of business list {Cound} failed to elasticsearch. Elastic index is now empty!", list.Count); return(StatusCode(StatusCodes.Status500InternalServerError, "Insert of business list failed.")); } _logger.LogInformation("Re-sync all {Count} businesses.", list.Count); return(Ok()); }