public async Task SetPartners(StrategyTargetingUpdateOptions <int> options) { // load var strategy = await _adGroupRepositoryAsync.Queryable() .Include(x => x.AdGroupSupplySources) .FirstAsync(x => x.AdGroupUuid == options.StrategyUuid); // remove var itemsToRemove = strategy.AdGroupSupplySources.Where(x => options.Targetings.Select(y => y.Id).Contains(x.PartnerId)).ToArray(); foreach (var adGroupSupplySource in itemsToRemove) { adGroupSupplySource.ObjectState = ObjectState.Deleted; strategy.AdGroupSupplySources.Remove(adGroupSupplySource); } // update foreach (var strategyTargetingItem in options.Targetings.Where(x => x.TargetingAction != (TargetingActionEnum)strategy.ExchangeTargetingMode)) { var adGroupSupplySource = itemsToRemove.FirstOrDefault(x => x.PartnerId == strategyTargetingItem.Id); if (adGroupSupplySource != null) { // using existing record to modify adGroupSupplySource.ObjectState = ObjectState.Modified; } else { adGroupSupplySource = new AdGroupSupplySource { ObjectState = ObjectState.Added, AdGroupId = strategy.AdGroupId, PartnerId = strategyTargetingItem.Id, PublisherId = -1 }; } adGroupSupplySource.TargetingActionId = (int)strategyTargetingItem.TargetingAction; // new targeting action strategy.AdGroupSupplySources.Add(adGroupSupplySource); } // save _adGroupRepositoryAsync.Update(strategy); await _brandscreenContext.SaveChangesAsync(); }
public async Task SetPublishers(StrategyTargetingUpdateOptions <int> options) { // load var strategy = await _adGroupRepositoryAsync.Queryable() .Include(x => x.AdGroupSupplySources) .FirstAsync(x => x.AdGroupUuid == options.StrategyUuid); // remove var itemsToRemove = strategy.AdGroupSupplySources.Where(x => options.Targetings.Select(y => y.Id).Contains(x.PublisherId)).ToArray(); foreach (var adGroupSupplySource in itemsToRemove) { adGroupSupplySource.ObjectState = ObjectState.Deleted; strategy.AdGroupSupplySources.Remove(adGroupSupplySource); } // update var ids = options.Targetings.Select(x => x.Id).ToArray(); var publishers = await _container.Resolve <IPublisherService>().GetPublishers().Where(x => ids.Contains(x.PublisherId)).ToListAsync(); foreach (var publisher in publishers) { // parent var defaultTargetingAction = strategy.AdGroupSupplySources.Where(x => x.PartnerId == publisher.PartnerId && x.PublisherId == -1) .Select(x => x.TargetingActionId) // parent country .Union(new[] { strategy.PublisherTargetingMode }) // fallback to default publisher targeting mode .Cast <TargetingActionEnum>() .First(); // skip: if the same as default targeting var target = options.Targetings.Single(y => y.Id == publisher.PublisherId); if (target.TargetingAction == defaultTargetingAction) { continue; } // modify or add var adGroupSupplySource = itemsToRemove.FirstOrDefault(x => x.PublisherId == publisher.PublisherId); if (adGroupSupplySource != null) { // using existing record to modify adGroupSupplySource.ObjectState = ObjectState.Modified; } else { adGroupSupplySource = new AdGroupSupplySource { ObjectState = ObjectState.Added, AdGroupId = strategy.AdGroupId, PartnerId = publisher.PartnerId, PublisherId = publisher.PublisherId }; } adGroupSupplySource.TargetingActionId = (int)target.TargetingAction; // new targeting action strategy.AdGroupSupplySources.Add(adGroupSupplySource); } // save _adGroupRepositoryAsync.Update(strategy); await _brandscreenContext.SaveChangesAsync(); }