public static webModel.Pricelist ToWebModel(this coreModel.Pricelist priceList, coreCatalogModel.CatalogProduct[] products = null, coreCatalogModel.Catalog[] catalogs = null, ConditionExpressionTree etalonEpressionTree = null) { var retVal = new webModel.Pricelist(); retVal.InjectFrom(priceList); retVal.Currency = priceList.Currency; if (priceList.Prices != null) { retVal.ProductPrices = new List<webModel.ProductPrice>(); foreach(var group in priceList.Prices.GroupBy(x=>x.ProductId)) { var productPrice = new webModel.ProductPrice(group.Key, group.Select(x=> x.ToWebModel())); retVal.ProductPrices.Add(productPrice); if (products != null) { var product = products.FirstOrDefault(x => x.Id == productPrice.ProductId); if(product != null) { productPrice.ProductName = product.Name; } } } } if(priceList.Assignments != null) { retVal.Assignments = priceList.Assignments.Select(x => x.ToWebModel(catalogs, etalonEpressionTree)).ToList(); } return retVal; }
public static webModel.PricelistAssignment ToWebModel(this coreModel.PricelistAssignment assignment, coreCatalogModel.Catalog[] catalogs = null, ConditionExpressionTree etalonEpressionTree = null) { var retVal = new webModel.PricelistAssignment(); retVal.InjectFrom(assignment); if(catalogs != null) { var catalog = catalogs.FirstOrDefault(x => x.Id == assignment.CatalogId); if(catalog != null) { retVal.CatalogName = catalog.Name; } } retVal.DynamicExpression = etalonEpressionTree; if (!String.IsNullOrEmpty(assignment.PredicateVisualTreeSerialized)) { retVal.DynamicExpression = JsonConvert.DeserializeObject<ConditionExpressionTree>(assignment.PredicateVisualTreeSerialized); if (etalonEpressionTree != null) { //Copy available elements from etalon because they not persisted var sourceBlocks = ((DynamicExpression)etalonEpressionTree).Traverse(x => x.Children); var targetBlocks = ((DynamicExpression)retVal.DynamicExpression).Traverse(x => x.Children); foreach (var sourceBlock in sourceBlocks) { foreach (var targetBlock in targetBlocks.Where(x => x.Id == sourceBlock.Id)) { targetBlock.AvailableChildren = sourceBlock.AvailableChildren; } } //copy available elements from etalon retVal.DynamicExpression.AvailableChildren = etalonEpressionTree.AvailableChildren; } } return retVal; }
public static webModel.CatalogItem ToWebModel(this coreModel.CatalogProduct product, IBlobUrlResolver blobUrlResolver = null, coreModel.Property[] properties = null) { webModel.CatalogItem retVal = new webModel.Product(); if (product.MainProductId != null) { retVal = new webModel.ProductVariation(); } retVal.InjectFrom(product); if (product.Assets != null) { retVal.Images = product.Assets.Where(x => x.Type == coreModel.ItemAssetType.Image) .Select(x => x.ToImageWebModel(blobUrlResolver)) .ToArray(); retVal.Assets = product.Assets.Where(x => x.Type == coreModel.ItemAssetType.File) .Select(x => x.ToAssetWebModel(blobUrlResolver)) .ToArray(); } if (product.Variations != null && product.Variations.Any()) { ((webModel.Product)retVal).Variations = product.Variations.Select(x => x.ToWebModel(blobUrlResolver, properties)) .OfType<webModel.ProductVariation>() .ToArray(); } if (product.Reviews != null) { retVal.EditorialReviews = product.Reviews.Select(x => new webModel.EditorialReview().InjectFrom(x)) .Cast<webModel.EditorialReview>() .ToArray(); } if (product.Links != null) { retVal.Categories = product.Links.Select(x => x.CategoryId).ToArray(); } if (product.SeoInfos != null) { retVal.Seo = product.SeoInfos.Select(x => x.ToWebModel()).ToArray(); } if (product.Associations != null && product.Associations.Any()) { retVal.Associations = product.Associations.Select(x => x.ToWebModel()).ToArray(); } if(product.PropertyValues != null) { retVal.Properties = new webModel.PropertyDictionary(); retVal.VariationProperties = new webModel.PropertyDictionary(); // dictionary properties are returned as object[], all other properties are returned as primitives foreach (var propValueGroup in product.PropertyValues.GroupBy(x => x.PropertyName)) { var displayName = propValueGroup.Key; var propertyValue = propValueGroup.FirstOrDefault(x => x.Value != null); var propertyCollection = retVal.Properties; if (propertyValue != null) { if(properties != null) { var propertyMetaInfo = properties.FirstOrDefault(x => string.Equals(propValueGroup.Key, x.Name, StringComparison.OrdinalIgnoreCase)); if(propertyMetaInfo != null && propertyMetaInfo.DisplayNames != null) { //TODO: use display name for specific language displayName = propertyMetaInfo.DisplayNames.Select(x=>x.Name).FirstOrDefault(x => !String.IsNullOrEmpty(x)) ?? displayName; } if(propertyMetaInfo != null && propertyMetaInfo.Type == coreModel.PropertyType.Variation) { propertyCollection = retVal.VariationProperties; } } propertyCollection.Add(displayName, propertyValue.Value); } } } return retVal; }
/// <summary> /// Converting to model type /// </summary> /// <returns></returns> public static coreModel.CatalogProduct ToCoreModel(this dataModel.Item dbItem, coreModel.Catalog catalog, coreModel.Category category, coreModel.CatalogProduct[] associatedProducts) { var retVal = new coreModel.CatalogProduct(); retVal.InjectFrom(dbItem); retVal.Catalog = catalog; retVal.CatalogId = catalog.Id; if (category != null) { retVal.Category = category; retVal.CategoryId = category.Id; } retVal.MainProductId = dbItem.ParentId; retVal.IsActive = dbItem.IsActive; retVal.IsBuyable = dbItem.IsBuyable; retVal.TrackInventory = dbItem.TrackInventory; retVal.MaxQuantity = (int)dbItem.MaxQuantity; retVal.MinQuantity = (int)dbItem.MinQuantity; #region Links retVal.Links = dbItem.CategoryLinks.Select(x => x.ToCoreModel()).ToList(); #endregion #region Images if (dbItem.Images != null) { retVal.Images = dbItem.Images.OrderBy(x => x.SortOrder).Select(x => x.ToCoreModel()).ToList(); } #endregion #region Assets if (dbItem.Assets != null) { retVal.Assets = dbItem.Assets.OrderBy(x=>x.CreatedDate).Select(x => x.ToCoreModel()).ToList(); } #endregion #region Property values if (dbItem.ItemPropertyValues != null) { retVal.PropertyValues = dbItem.ItemPropertyValues.OrderBy(x=>x.Name).Select(x => x.ToCoreModel(null)).ToList(); } #endregion #region Variations retVal.Variations = new List<coreModel.CatalogProduct>(); foreach (var variation in dbItem.Childrens) { var productVaraition = variation.ToCoreModel(catalog, category, associatedProducts: null); productVaraition.MainProduct = retVal; productVaraition.MainProductId = retVal.Id; retVal.Variations.Add(productVaraition); } #endregion #region EditorialReviews if (dbItem.EditorialReviews != null) { retVal.Reviews = dbItem.EditorialReviews.Select(x => x.ToCoreModel()).ToList(); } #endregion #region Associations if (dbItem.AssociationGroups != null && associatedProducts != null) { retVal.Associations = new List<coreModel.ProductAssociation>(); foreach (var association in dbItem.AssociationGroups.SelectMany(x => x.Associations)) { var associatedProduct = associatedProducts.FirstOrDefault(x => x.Id == association.ItemId); if (associatedProduct != null) { var productAssociation = association.ToCoreModel(associatedProduct); retVal.Associations.Add(productAssociation); } } } #endregion //TaxType category inheritance if(retVal.TaxType == null && category != null) { retVal.TaxType = category.TaxType; } #region Variation property, assets, review inheritance if (dbItem.Parent != null) { //TaxType from main product inheritance if(dbItem.TaxType == null && dbItem.Parent.TaxType != null) { retVal.TaxType = dbItem.Parent.TaxType; } var allProductPropertyNames = dbItem.Parent.ItemPropertyValues.Select(x => x.Name).Distinct().ToArray(); //Property inheritance if (allProductPropertyNames != null) { //Need copy not overridden property values from main product to variation var overriddenPropertyNames = retVal.PropertyValues.Select(x => x.PropertyName).ToArray(); var inheritedPropertyNames = allProductPropertyNames.Except(overriddenPropertyNames); var dbInheritedPropertyValues = dbItem.Parent.ItemPropertyValues.Where(x => inheritedPropertyNames.Contains(x.Name)); foreach (var dbInheritedPropertyValue in dbInheritedPropertyValues) { //Reset id for correct value override var propertyValue = dbInheritedPropertyValue.ToCoreModel(null); propertyValue.Id = null; retVal.PropertyValues.Add(propertyValue); } } //Image inheritance if (!retVal.Images.Any() && dbItem.Parent.Images != null) { retVal.Images = dbItem.Parent.Images.OrderBy(x => x.SortOrder).Select(x => x.ToCoreModel()).ToList(); foreach (var image in retVal.Images) { //Reset id for correct override image.Id = null; } } //Review inheritance if ((retVal.Reviews == null || !retVal.Reviews.Any()) && dbItem.Parent.EditorialReviews != null) { retVal.Reviews = dbItem.Parent.EditorialReviews.Select(x => x.ToCoreModel()).ToList(); foreach (var review in retVal.Reviews) { //Reset id for correct override review.Id = null; } } } #endregion return retVal; }
public void Update(coreModel.CatalogProduct[] items) { var now = DateTime.UtcNow; using (var repository = _catalogRepositoryFactory()) using (var changeTracker = base.GetChangeTracker(repository)) { var dbItems = repository.GetItemByIds(items.Select(x => x.Id).ToArray(), coreModel.ItemResponseGroup.ItemLarge); foreach (var dbItem in dbItems) { var item = items.FirstOrDefault(x => x.Id == dbItem.Id); if (item != null) { //Need skip inherited properties without overridden value if (dbItem.ParentId != null && item.PropertyValues != null) { var dbParentItem = repository.GetItemByIds(new[] { dbItem.ParentId }, coreModel.ItemResponseGroup.ItemProperties).First(); item.MainProduct = dbParentItem.ToCoreModel(new coreModel.Catalog { Id = dbItem.CatalogId }, new coreModel.Category { Id = dbItem.CategoryId }, null); } changeTracker.Attach(dbItem); item.Patch(dbItem); } //Patch seoInfo if (item.SeoInfos != null) { foreach (var seoInfo in item.SeoInfos) { seoInfo.ObjectId = item.Id; seoInfo.ObjectType = typeof(coreModel.CatalogProduct).Name; } var seoInfos = new ObservableCollection<SeoInfo>(_commerceService.GetObjectsSeo(new[] { item.Id })); seoInfos.ObserveCollection(x => _commerceService.UpsertSeo(x), x => _commerceService.DeleteSeo(new[] { x.Id })); item.SeoInfos.Patch(seoInfos, (source, target) => _commerceService.UpsertSeo(source)); } } CommitChanges(repository); } }
public void Update(coreModel.Property[] properties) { using (var repository = _catalogRepositoryFactory()) using (var changeTracker = base.GetChangeTracker(repository)) { var dbProperties = repository.GetPropertiesByIds(properties.Select(x => x.Id).ToArray()); foreach (var dbProperty in dbProperties) { var property = properties.FirstOrDefault(x => x.Id == dbProperty.Id); if (property != null) { changeTracker.Attach(dbProperty); var dbPropertyChanged = property.ToDataModel(); dbPropertyChanged.Patch(dbProperty); } } CommitChanges(repository); } }
public void Update(coreModel.CatalogProduct[] items) { var pkMap = new PrimaryKeyResolvingMap(); var now = DateTime.UtcNow; using (var repository = _catalogRepositoryFactory()) using (var changeTracker = base.GetChangeTracker(repository)) { var dbItems = repository.GetItemByIds(items.Select(x => x.Id).ToArray(), coreModel.ItemResponseGroup.ItemLarge); foreach (var dbItem in dbItems) { var item = items.FirstOrDefault(x => x.Id == dbItem.Id); if (item != null) { changeTracker.Attach(dbItem); item.Patch(dbItem, pkMap); } } CommitChanges(repository); pkMap.ResolvePrimaryKeys(); } //Update seo for products _commerceService.UpsertSeoForObjects(items); }
public static webModel.CatalogItem ToWebModel(this coreModel.CatalogProduct product, IBlobUrlResolver blobUrlResolver = null, coreModel.Property[] properties = null, coreInvModel.InventoryInfo inventory = null) { webModel.CatalogItem retVal = new webModel.Product(); if (product.MainProductId != null) { retVal = new webModel.ProductVariation(); } retVal.InjectFrom(product); if(product.Images != null && product.Images.Any()) { //Back compability check group to detect primary image (remove later) var primaryImage = product.Images.FirstOrDefault(x => String.Equals(x.Group, "primaryimage", StringComparison.InvariantCultureIgnoreCase)); if(primaryImage == null) { primaryImage = product.Images.OrderBy(x => x.SortOrder).First(); } retVal.PrimaryImage = primaryImage.ToWebModel(blobUrlResolver); retVal.Images = product.Images.Skip(1).Select(x => x.ToWebModel(blobUrlResolver)).ToArray(); } if (product.Assets != null) { retVal.Assets = product.Assets.Select(x => x.ToWebModel(blobUrlResolver)).ToArray(); } if (product.Variations != null && product.Variations.Any()) { ((webModel.Product)retVal).Variations = product.Variations.Select(x => x.ToWebModel(blobUrlResolver, properties)) .OfType<webModel.ProductVariation>() .ToArray(); } if (product.Reviews != null) { retVal.EditorialReviews = product.Reviews.Select(x => new webModel.EditorialReview().InjectFrom(x)) .Cast<webModel.EditorialReview>() .ToArray(); } if (product.SeoInfos != null) { retVal.Seo = product.SeoInfos.Select(x => x.ToWebModel()).ToArray(); } if (product.Associations != null && product.Associations.Any()) { retVal.Associations = product.Associations.Select(x => x.ToWebModel()).ToArray(); } if(product.PropertyValues != null) { retVal.Properties = new webModel.PropertyDictionary(); retVal.VariationProperties = new webModel.PropertyDictionary(); // dictionary properties are returned as object[], all other properties are returned as primitives foreach (var propValueGroup in product.PropertyValues.GroupBy(x => x.PropertyName)) { var displayName = propValueGroup.Key; var propertyValue = propValueGroup.FirstOrDefault(x => x.Value != null); var propertyCollection = retVal.Properties; if (propertyValue != null) { if(properties != null) { var propertyMetaInfo = properties.FirstOrDefault(x => string.Equals(propValueGroup.Key, x.Name, StringComparison.OrdinalIgnoreCase)); if(propertyMetaInfo != null && propertyMetaInfo.DisplayNames != null) { //TODO: use display name for specific language displayName = propertyMetaInfo.DisplayNames.Select(x=>x.Name).FirstOrDefault(x => !String.IsNullOrEmpty(x)) ?? displayName; } if(propertyMetaInfo != null && propertyMetaInfo.Type == coreModel.PropertyType.Variation) { propertyCollection = retVal.VariationProperties; } } propertyCollection.Add(displayName, propertyValue.Value); } } } if (inventory != null) { retVal.Inventory = inventory.ToWebModel(); } return retVal; }