public void LoadSeoForObjects(coreModel.ISeoSupport[] seoSupportObjects)
 {
     using (var repository = _repositoryFactory())
     {
         var objectIds = seoSupportObjects.Where(x => x.Id != null).Select(x => x.Id).ToArray();
         var seoInfos = repository.SeoUrlKeywords.Where(x => objectIds.Contains(x.ObjectId))
                                                 .ToArray()
                                                 .Select(x => x.ToCoreModel())
                                                 .ToArray();
         foreach (var seoSupportObject in seoSupportObjects)
         {
             seoSupportObject.SeoInfos = seoInfos.Where(x => x.ObjectId == seoSupportObject.Id && x.ObjectType == seoSupportObject.GetType().Name).ToList();
         }
     }
 }
        public IHttpActionResult ValidateAddress(domainModel.Address address)
        {
            IHttpActionResult retVal = BadRequest();
            LogInvoker<AvalaraLogger.TaxRequestContext>.Execute(log =>
            {
                if (!_taxSettings.IsValidateAddress)
                {
                    retVal = BadRequest("AvaTax address validation disabled");
                    throw new Exception((retVal as BadRequestErrorMessageResult).Message);
                }

                if (!string.IsNullOrEmpty(_taxSettings.Username) && !string.IsNullOrEmpty(_taxSettings.Password)
                    && !string.IsNullOrEmpty(_taxSettings.ServiceUrl)
                    && !string.IsNullOrEmpty(_taxSettings.CompanyCode))
                {
                    var addressSvc = new JsonAddressSvc(_taxSettings.Username, _taxSettings.Password, _taxSettings.ServiceUrl);
                    
                    var request = address.ToValidateAddressRequest(_taxSettings.CompanyCode);
                    
                    var validateAddressResult = addressSvc.Validate(request);
                    if (!validateAddressResult.ResultCode.Equals(SeverityLevel.Success))
                    {
                        var error = string.Join(Environment.NewLine,
                            validateAddressResult.Messages.Where(ms => ms.Severity == SeverityLevel.Error).Select(
                            m => m.Summary + string.Format(" [{0} - {1}] ", m.RefersTo, m.Details == null ? string.Empty : string.Join(", ", m.Details))));
                        retVal = BadRequest(error);
                        throw new Exception((retVal as BadRequestErrorMessageResult).Message);
                    }

                    retVal = Ok(validateAddressResult);
                }

                if (!(retVal is OkNegotiatedContentResult<ValidateResult>))
                {
                    retVal = BadRequest("AvaTax credentials not provided");
                    throw new Exception((retVal as BadRequestErrorMessageResult).Message);
                }
            })
                .OnError(_logger, AvalaraLogger.EventCodes.AddressValidationError)
                .OnSuccess(_logger, AvalaraLogger.EventCodes.ValidateAddress);

            return retVal;
        }
        public coreModel.FulfillmentCenter UpsertFulfillmentCenter(coreModel.FulfillmentCenter center)
        {
            if (center == null)
                throw new ArgumentNullException("center");

            coreModel.FulfillmentCenter retVal = null;
            using (var repository = _repositoryFactory())
            {
                var sourceEntry = center.ToDataModel();
                var targetEntry = repository.FulfillmentCenters.FirstOrDefault(x => x.Id == center.Id);
                if (targetEntry == null)
                {
                    repository.Add(sourceEntry);
                }
                else
                {
                    sourceEntry.Patch(targetEntry);
                }

                CommitChanges(repository);
                retVal = repository.FulfillmentCenters.First(x => x.Id == sourceEntry.Id).ToCoreModel();
            }
            return retVal;
        }
		public coreModel.SeoInfo UpsertSeo(coreModel.SeoInfo seo)
		{
			if (seo == null)
				throw new ArgumentNullException("seo");

			coreModel.SeoInfo retVal = null;
			using (var repository = _repositoryFactory())
			{
				var sourceEntry = seo.ToDataModel();
				var targetEntry = repository.SeoUrlKeywords.FirstOrDefault(x => x.Id == seo.Id || (x.ObjectId == sourceEntry.ObjectId && x.ObjectType == sourceEntry.ObjectType));
				if (targetEntry == null)
				{
					repository.Add(sourceEntry);
				}
				else
				{
					sourceEntry.Patch(targetEntry);
				}
				CommitChanges(repository);
				seo.Id = sourceEntry.Id ?? targetEntry.Id;
				retVal = repository.SeoUrlKeywords.First(x => x.Id == seo.Id).ToCoreModel();
			}
			return retVal;
		}
 public IHttpActionResult CreateCurrency(coreModel.Currency currency)
 {
     _commerceService.UpsertCurrencies(new[] { currency });
     return Ok();
 }
 public IHttpActionResult BatchUpdateSeoInfos(coreModel.SeoInfo[] seoInfos)
 {
     _commerceService.UpsertSeoInfos(seoInfos);
     return Ok();
 }
        public void UpsertSeoForObjects(coreModel.ISeoSupport[] seoSupportObjects)
        {
            if (seoSupportObjects == null)
            {
                throw new ArgumentNullException("seoSupportObjects");
            }
            foreach (var seoObject in seoSupportObjects.Where(x => x.Id != null))
            {
                var objectType = seoObject.GetType().Name;

                using (var repository = _repositoryFactory())
                using (var changeTracker = GetChangeTracker(repository))
                {
                    if (seoObject.SeoInfos != null)
                    {
                        //Normalize seoInfo
                        foreach (var seoInfo in seoObject.SeoInfos)
                        {
                            if (seoInfo.ObjectId == null)
                                seoInfo.ObjectId = seoObject.Id;
                            if (seoInfo.ObjectType == null)
                                seoInfo.ObjectType = objectType;
                        }
                    }
                    if (seoObject.SeoInfos != null && seoObject.SeoInfos.Any())
                    {
                        var target = new { SeoInfos = new ObservableCollection<dataModel.SeoUrlKeyword>(repository.GetObjectSeoUrlKeywords(objectType, seoObject.Id)) };
                        var source = new { SeoInfos = new ObservableCollection<dataModel.SeoUrlKeyword>(seoObject.SeoInfos.Select(x => x.ToDataModel())) };

                        changeTracker.Attach(target);

                        source.SeoInfos.Patch(target.SeoInfos, new SeoUrlKeywordComparer(), (sourceSeoUrlKeyword, targetSeoUrlKeyword) => sourceSeoUrlKeyword.Patch(targetSeoUrlKeyword));
                    }
                    CommitChanges(repository);
                }
            }
        }
        public void UpsertCurrencies(coreModel.Currency[] currencies)
        {
            if (currencies == null)
                throw new ArgumentNullException("currencies");

            using (var repository = _repositoryFactory())
            {
                //Ensure that only one Primary currency
                if (currencies.Any(x => x.IsPrimary))
                {
                    var oldPrimaryCurrency = repository.Currencies.FirstOrDefault(x => x.IsPrimary);
                    if (oldPrimaryCurrency != null)
                    {
                        oldPrimaryCurrency.IsPrimary = false;
                    }
                }

                foreach (var currency in currencies)
                {
                    var sourceEntry = currency.ToDataModel();
                    var targetEntry = repository.Currencies.FirstOrDefault(x => x.Code == currency.Code);
                    if (targetEntry == null)
                    {
                        repository.Add(sourceEntry);
                    }
                    else
                    {
                        sourceEntry.Patch(targetEntry);
                    }
                }

                CommitChanges(repository);
            }
        }
        public void DeleteSeoForObject(coreModel.ISeoSupport seoSupportObject)
        {
            if (seoSupportObject == null)
            {
                throw new ArgumentNullException("seoSupportObjects");
            }

            if (seoSupportObject.Id != null)
            {
                using (var repository = _repositoryFactory())
                {

                    var objectType = seoSupportObject.GetType().Name;

                    var objectId = seoSupportObject.Id;

                    var seoUrlKeywords = repository.GetObjectSeoUrlKeywords(objectType, objectId);
                    foreach (var seoUrlKeyword in seoUrlKeywords)
                    {
                        repository.Remove(seoUrlKeyword);
                    }
                    CommitChanges(repository);
                }
            }
        }