public async Task SetCountries(StrategyTargetingUpdateOptions <int> options) { // load var strategy = await _adGroupRepositoryAsync.Queryable() .Include(x => x.AdGroupGeoLocations) .Include(x => x.AdGroupGeoLocations.Select(y => y.GeoLocation)) .FirstAsync(x => x.AdGroupUuid == options.StrategyUuid); // remove var itemsToRemove = strategy.AdGroupGeoLocations.Where(x => x.GeoLocation != null && options.Targetings.Select(y => y.Id).Contains(x.GeoLocation.GeoCountryId)).ToArray(); foreach (var adGroupGeoLocation in itemsToRemove) { adGroupGeoLocation.ObjectState = ObjectState.Deleted; strategy.AdGroupGeoLocations.Remove(adGroupGeoLocation); } // update var ids = options.Targetings.Where(x => x.TargetingAction != (TargetingActionEnum)strategy.CountryTargetingMode).Select(x => x.Id).ToArray(); if (ids.Length > 0) { // modify or add var geoLocations = await _container.Resolve <IGeoLocationService>().GetGeoLocationsByGeoCountryIds(ids).ToListAsync(); foreach (var geoLocation in geoLocations) { var adGroupGeoLocation = itemsToRemove.FirstOrDefault(x => x.GeoLocationId == geoLocation.GeoLocationId); if (adGroupGeoLocation != null) { // using existing record to modify adGroupGeoLocation.ObjectState = ObjectState.Modified; } else { adGroupGeoLocation = new AdGroupGeoLocation { ObjectState = ObjectState.Added, AdGroupId = strategy.AdGroupId, GeoLocationId = geoLocation.GeoLocationId, }; } adGroupGeoLocation.TargetingActionId = (int)options.Targetings.Single(x => x.Id == geoLocation.GeoCountryId).TargetingAction; // new targeting action strategy.AdGroupGeoLocations.Add(adGroupGeoLocation); } } // save _adGroupRepositoryAsync.Update(strategy); await _brandscreenContext.SaveChangesAsync(); }
public async Task SetCities(StrategyTargetingUpdateOptions <int> options) { // load var strategy = await _adGroupRepositoryAsync.Queryable() .Include(x => x.AdGroupGeoLocations) .Include(x => x.AdGroupGeoLocations.Select(y => y.GeoLocation)) .FirstAsync(x => x.AdGroupUuid == options.StrategyUuid); // remove var itemsToRemove = strategy.AdGroupGeoLocations.Where(x => x.GeoLocation?.GeoCityId != null && options.Targetings.Select(y => y.Id).Contains(x.GeoLocation.GeoCityId.Value)).ToArray(); foreach (var adGroupGeoLocation in itemsToRemove) { adGroupGeoLocation.ObjectState = ObjectState.Deleted; strategy.AdGroupGeoLocations.Remove(adGroupGeoLocation); } // update var ids = options.Targetings.Select(x => x.Id).ToArray(); var geoLocations = await _container.Resolve <IGeoLocationService>().GetGeoLocationsByGeoCityIds(ids).ToListAsync(); foreach (var geoLocation in geoLocations) { // parent var defaultTargetingAction = strategy.AdGroupGeoLocations.Where(x => x.GeoLocation != null && x.GeoLocation.GeoRegionId == geoLocation.GeoRegionId && x.GeoLocation.GeoCityId == null && x.GeoLocation.GeoMetroId == null) .Select(x => x.TargetingActionId) // parent region .Union(strategy.AdGroupGeoLocations.Where(x => x.GeoLocation != null && x.GeoLocation.GeoCountryId == geoLocation.GeoCountryId && x.GeoLocation.GeoRegionId == null && x.GeoLocation.GeoCityId == null && x.GeoLocation.GeoMetroId == null) .Select(x => x.TargetingActionId)) // parent country .Union(new[] { strategy.CityTargetingMode }) // fallback to default city targeting mode .Cast <TargetingActionEnum>() .First(); // skip: if the same as default targeting var geoCityId = geoLocation.GeoCityId.GetValueOrDefault(0); var target = options.Targetings.Single(y => y.Id == geoCityId); if (target.TargetingAction == defaultTargetingAction) { continue; } // modify or add var adGroupGeoLocation = itemsToRemove.FirstOrDefault(x => x.GeoLocationId == geoLocation.GeoLocationId); if (adGroupGeoLocation != null) { // using existing record to modify adGroupGeoLocation.ObjectState = ObjectState.Modified; } else { adGroupGeoLocation = new AdGroupGeoLocation { ObjectState = ObjectState.Added, AdGroupId = strategy.AdGroupId, GeoLocationId = geoLocation.GeoLocationId, }; } adGroupGeoLocation.TargetingActionId = (int)target.TargetingAction; // new targeting action strategy.AdGroupGeoLocations.Add(adGroupGeoLocation); } // save _adGroupRepositoryAsync.Update(strategy); await _brandscreenContext.SaveChangesAsync(); }