/// <summary> /// Merge from other product, without any deletion, only update and create allowed /// /// </summary> /// <param name="product"></param> public void MergeFrom(CatalogProduct product) { Id = product.Id; var imgComparer = AnonymousComparer.Create((Image x) => x.Url); Images = Images.Concat(product.Images).Distinct(imgComparer).ToList(); var assetComparer = AnonymousComparer.Create((Asset x) => x.Url); Assets = Assets.Concat(product.Assets).Distinct(assetComparer).ToList(); var reviewsComparer = AnonymousComparer.Create((EditorialReview x) => string.Join(":", x.ReviewType, x.LanguageCode)); Reviews = Reviews.Concat(product.Reviews).Distinct(reviewsComparer).ToList(); var properyValueComparer = AnonymousComparer.Create((PropertyValue x) => x.PropertyName); PropertyValues = product.PropertyValues.Concat(PropertyValues).Distinct(properyValueComparer).ToList(); var seoComparer = AnonymousComparer.Create((SeoInfo x) => string.Join(":", x.SemanticUrl, x.LanguageCode)); SeoInfos = SeoInfos.Concat(product.SeoInfos).Distinct(seoComparer).ToList(); }
/// <summary> /// Merge from other product, without any deletion, only update and create allowed /// /// </summary> /// <param name="product"></param> public void MergeFrom(CatalogProduct product) { Id = product.Id; if (string.IsNullOrEmpty(Code)) { Code = product.Code; } if (string.IsNullOrEmpty(Name)) { Name = product.Name; } if (string.IsNullOrEmpty(CategoryId)) { CategoryId = product.CategoryId; } if (Category == null || (Category != null && string.IsNullOrEmpty(Category.Path))) { Category = product.Category; } if (string.IsNullOrEmpty(ProductType)) { ProductType = product.ProductType; } if (string.IsNullOrEmpty(Vendor)) { Vendor = product.Vendor; } if (string.IsNullOrEmpty(Gtin)) { Gtin = product.Gtin; } if (string.IsNullOrEmpty(OuterId)) { OuterId = product.OuterId; } if (string.IsNullOrEmpty(PackageType)) { PackageType = product.PackageType; } if (string.IsNullOrEmpty(ManufacturerPartNumber)) { ManufacturerPartNumber = product.ManufacturerPartNumber; } if (string.IsNullOrEmpty(WeightUnit)) { WeightUnit = product.WeightUnit; } if (string.IsNullOrEmpty(MeasureUnit)) { MeasureUnit = product.MeasureUnit; } if (string.IsNullOrEmpty(DownloadType)) { DownloadType = product.DownloadType; } if (string.IsNullOrEmpty(ShippingType)) { ShippingType = product.ShippingType; } if (string.IsNullOrEmpty(TaxType)) { TaxType = product.TaxType; } Weight ??= product.Weight; Height ??= product.Height; Length ??= product.Length; Width ??= product.Width; MaxQuantity ??= product.MaxQuantity; MinQuantity ??= product.MinQuantity; if (Priority == default) { Priority = product.Priority; } EndDate ??= product.EndDate; foreach (var image in product.Images) { var existedImage = Images.FirstOrDefault(x => x.Url.Equals(image.Url, StringComparison.InvariantCultureIgnoreCase)); if (existedImage != null) { existedImage.Id = image.Id; } else { Images.Add(image); } } var assetComparer = AnonymousComparer.Create((Asset x) => x.Url); Assets = Assets.Concat(product.Assets).Distinct(assetComparer).ToList(); var reviewsComparer = AnonymousComparer.Create((EditorialReview x) => string.Join(":", x.ReviewType, x.LanguageCode, x.Content)); Reviews = Reviews.Concat(product.Reviews).Distinct(reviewsComparer).ToList(); // Merge Properties - leave properties that are not presented in CSV and add all from the CSV (with merging metadata and replacing existing ones) var propertyComparer = AnonymousComparer.Create((Property x) => x.Name); var skippedExistingProperties = new List <Property>(); foreach (var property in Properties.OfType <CsvProperty>()) { var existingProperty = product.Properties.FirstOrDefault(x => propertyComparer.Equals(x, property)); if (existingProperty != null) { property.MergeFrom(existingProperty); skippedExistingProperties.Add(existingProperty); } } Properties = Properties.Where(x => !x.Name.IsNullOrEmpty()) .Concat(product.Properties .Where(x => !skippedExistingProperties.Any(existingProperty => propertyComparer.Equals(x, existingProperty)))) .ToList(); //merge seo infos var seoComparer = AnonymousComparer.Create((SeoInfo x) => string.Join(":", x.SemanticUrl, x.LanguageCode?.ToLower(), x.StoreId)); foreach (var seoInfo in SeoInfos.OfType <CsvSeoInfo>()) { var existingSeoInfo = product.SeoInfos.FirstOrDefault(x => seoComparer.Equals(x, seoInfo)); if (existingSeoInfo != null) { seoInfo.MergeFrom(existingSeoInfo); product.SeoInfos.Remove(existingSeoInfo); } } SeoInfos = SeoInfos.Where(x => !x.SemanticUrl.IsNullOrEmpty()).Concat(product.SeoInfos).ToList(); }
/// <summary> /// Merge from other product, without any deletion, only update and create allowed /// /// </summary> /// <param name="product"></param> public void MergeFrom(CatalogProduct product) { Id = product.Id; if (string.IsNullOrEmpty(Code)) { Code = product.Code; } if (string.IsNullOrEmpty(Name)) { Name = product.Name; } if (string.IsNullOrEmpty(CategoryId)) { CategoryId = product.CategoryId; } if (Category == null || (Category != null && string.IsNullOrEmpty(Category.Path))) { Category = product.Category; } if (string.IsNullOrEmpty(ProductType)) { ProductType = product.ProductType; } if (string.IsNullOrEmpty(Vendor)) { Vendor = product.Vendor; } var imgComparer = AnonymousComparer.Create((Image x) => x.Url); Images = Images.Concat(product.Images).Distinct(imgComparer).ToList(); var assetComparer = AnonymousComparer.Create((Asset x) => x.Url); Assets = Assets.Concat(product.Assets).Distinct(assetComparer).ToList(); var reviewsComparer = AnonymousComparer.Create((EditorialReview x) => string.Join(":", x.ReviewType, x.LanguageCode)); Reviews = Reviews.Concat(product.Reviews).Distinct(reviewsComparer).ToList(); var properyValueComparer = AnonymousComparer.Create((PropertyValue x) => x.PropertyName); foreach (var propertyValue in PropertyValues) { var array = product.PropertyValues.Where(x => properyValueComparer.Equals(x, propertyValue)).ToArray(); foreach (var productPropertyValue in array) { product.PropertyValues.Remove(productPropertyValue); } } PropertyValues = product.PropertyValues.Concat(PropertyValues).ToList(); //merge seo infos var seoComparer = AnonymousComparer.Create((SeoInfo x) => string.Join(":", x.SemanticUrl, x.LanguageCode?.ToLower(), x.StoreId)); foreach (var seoInfo in SeoInfos.OfType <CsvSeoInfo>()) { var existingSeoInfo = product.SeoInfos.FirstOrDefault(x => seoComparer.Equals(x, seoInfo)); if (existingSeoInfo != null) { seoInfo.MergeFrom(existingSeoInfo); product.SeoInfos.Remove(existingSeoInfo); } } SeoInfos = SeoInfos.Where(x => !x.SemanticUrl.IsNullOrEmpty()).Concat(product.SeoInfos).ToList(); }