public List<CampaignSetupModel.KeywordsModel> SaveNegativeKeywords(CampaignSetupModel model, int customerFk) { if (model.AdModelProp.NegativeKeywords.Any()) { GoogleViolation[] gv = ValidateGoogleNegativeKeywords(model.AdModelProp.NegativeKeywords); if (gv.Length > 0) throw new Exception(gv.First().shortFieldPath + ": " + gv.First().errorMessage); } using (var dbcontext = new SemplestModel.Semplest()) { var promo = GetPromoitionFromCampaign(dbcontext, customerFk, model); if (!promo.IsLaunched) { RefreshKeywords(model, promo); } else { var addKiops = new List<KeywordIdRemoveOppositePair>(); var addNewKiops = new List<string>(); var addDeletedKiops = new List<int>(); var qpka = promo.PromotionKeywordAssociations.ToList(); //check negative keywords that have been added to the gui if (model.AdModelProp.NegativeKeywords != null) { foreach (string negativeKeyword in model.AdModelProp.NegativeKeywords) { var kiop = new KeywordIdRemoveOppositePair(); var pka = qpka.SingleOrDefault(key => key.Keyword.Keyword1 == negativeKeyword); if (pka != null) { //means if the keyword existied and was positive it needs to be removed and added as a negative. //if the keyword is already negative then do nothing because we've already added it kiop.keywordId = pka.KeywordFK; if (!pka.IsNegative) { kiop.removeOpposite = true; addKiops.Add(kiop); } } else { var kw = dbcontext.Keywords.SingleOrDefault(key => key.Keyword1 == negativeKeyword); if (kw != null) { kiop.keywordId = kw.KeywordPK; kiop.removeOpposite = false; addKiops.Add(kiop); } else//this keyword doesn't exist in the database so when we call the stored proc get the id so it can be sent to the api addNewKiops.Add(negativeKeyword); } } } //check for negative keywords that have been removed from the gui foreach (PromotionKeywordAssociation k in qpka.Where(key => key.IsNegative == true).ToList()) { if (!model.AdModelProp.NegativeKeywords.Contains(k.Keyword.Keyword1)) addDeletedKiops.Add(k.Keyword.KeywordPK); } List<int> deletedKeywords = new List<int>(); var op = new System.Data.Objects.ObjectParameter("NegativeKeywordID", typeof(int)); var op2 = new System.Data.Objects.ObjectParameter("Exists", typeof(int)); foreach (string kw in model.AdModelProp.NegativeKeywords) { //keywords that need to be deleted var snr = dbcontext.SetNegativeKeyword(kw, promo.PromotionPK, op, op2).ToList(); if (snr.Any()) { deletedKeywords.AddRange(snr.Select(ids => ids.KeywordPK)); } if (!string.IsNullOrEmpty(op.Value.ToString())) { var kop = new KeywordIdRemoveOppositePair { keywordId = int.Parse(op.Value.ToString()), removeOpposite = bool.Parse(op2.Value.ToString()) }; addKiops.Add(kop); } } foreach (int dk in addDeletedKiops) promo.PromotionKeywordAssociations.Single(kw => kw.KeywordFK == dk).IsDeleted=true; dbcontext.SaveChanges(); _savedCampaign = true; var sw = new ServiceClientWrapper(); var adEngines = new List<string>(); adEngines.AddRange( promo.PromotionAdEngineSelecteds.Select(pades => pades.AdvertisingEngine.AdvertisingEngine1)); if (addDeletedKiops.Any()) sw.DeleteNegativeKeywords(promo.PromotionPK, addDeletedKiops, adEngines); if (addKiops.Any()) sw.AddNegativeKeywords(promo.PromotionPK, addKiops, adEngines); if (deletedKeywords.Any()) sw.DeleteKeywords(promo.PromotionPK, deletedKeywords, adEngines); } model.AllKeywords.Clear(); model.AllKeywords.AddRange( promo.PromotionKeywordAssociations.Where(key => !key.IsDeleted && !key.IsNegative).Select( key => new CampaignSetupModel.KeywordsModel { Name = key.Keyword.Keyword1, Id = key.Keyword.KeywordPK })); } return model.AllKeywords; }