public async Task Add_New_DictionaryItems_To_Property_And_Then_Choose_DictItem_For_Product() { var propDictionaryService = new Mock <IPropertyDictionaryItemService>().Object; var propDictionarySearchService = new Mock <IPropertyDictionaryItemSearchService>(); var productService = new Mock <IItemService>(); var colorProperty = new Property { Id = "Color", Name = "Color", CatalogId = "Electronics", Type = PropertyType.Product, ValueType = PropertyValueType.ShortText, Dictionary = true, Multilanguage = true, Multivalue = true }; var greenDictItem = new PropertyDictionaryItem { Alias = "Green", PropertyId = colorProperty.Id, LocalizedValues = new[] { new PropertyDictionaryItemLocalizedValue { LanguageCode = "en", Value = "Green" }, new PropertyDictionaryItemLocalizedValue { LanguageCode = "de", Value = "grün" } } }; propDictionarySearchService.Setup(x => x.SearchAsync(It.IsAny <PropertyDictionaryItemSearchCriteria>())) .Returns(Task.FromResult(new PropertyDictionaryItemSearchResult { TotalCount = 1, Results = new[] { greenDictItem } })); productService.Setup(x => x.GetByIdAsync(It.IsAny <string>(), It.IsAny <string>(), null)) .Returns(Task.FromResult(new CatalogProduct { PropertyValues = new List <PropertyValue>() })); //Add the new dictionary item to the property await propDictionaryService.SaveChangesAsync(new[] { greenDictItem }); var product = await productService.Object.GetByIdAsync("Shoes", ItemResponseGroup.ItemProperties.ToString()); //Find the desired dictionary value from all available greenDictItem = (await propDictionarySearchService.Object.SearchAsync(new PropertyDictionaryItemSearchCriteria { PropertyIds = new[] { colorProperty.Id }, Keyword = "Green" })) .Results.FirstOrDefault(); //Choose dictionary item for product property product.PropertyValues.Add(new PropertyValue { Alias = greenDictItem.Alias, PropertyId = greenDictItem.PropertyId, ValueId = greenDictItem.Id }); await productService.Object.SaveChangesAsync(new[] { product }); }
public virtual ExportablePropertyDictionaryItem FromModel(PropertyDictionaryItem propertyDictionaryItem) { Id = propertyDictionaryItem.Id; PropertyId = propertyDictionaryItem.PropertyId; Alias = propertyDictionaryItem.Alias; SortOrder = propertyDictionaryItem.SortOrder; LocalizedValues = propertyDictionaryItem.LocalizedValues; return(this); }
public virtual PropertyDictionaryItem ToModel(PropertyDictionaryItem propDictItem) { if (propDictItem == null) { throw new ArgumentNullException(nameof(propDictItem)); } propDictItem.Id = Id; propDictItem.Alias = Alias; propDictItem.PropertyId = PropertyId; propDictItem.LocalizedValues = DictionaryItemValues.Select(x => x.ToModel(AbstractTypeFactory <PropertyDictionaryItemLocalizedValue> .TryCreateInstance())).ToList(); return(propDictItem); }
public virtual PropertyDictionaryItemEntity FromModel(PropertyDictionaryItem propDictItem, PrimaryKeyResolvingMap pkMap) { if (propDictItem == null) { throw new ArgumentNullException(nameof(propDictItem)); } pkMap.AddPair(propDictItem, this); Id = propDictItem.Id; Alias = propDictItem.Alias; PropertyId = propDictItem.PropertyId; if (propDictItem.LocalizedValues != null) { DictionaryItemValues = new ObservableCollection <PropertyDictionaryValueEntity>(propDictItem.LocalizedValues.Select(x => AbstractTypeFactory <PropertyDictionaryValueEntity> .TryCreateInstance().FromModel(x, pkMap))); } return(this); }
private async Task ResolvePropertyDictionaryItems(List <CsvProduct> csvProducts, ExportImportProgressInfo progressInfo, Action <ExportImportProgressInfo> progressCallback) { var allDictPropertyIds = csvProducts.SelectMany(x => x.Properties).Where(x => x.Dictionary) .Select(x => x.Id).Distinct() .ToArray(); var allDictItems = (await _propDictItemSearchService.SearchAsync(new PropertyDictionaryItemSearchCriteria { PropertyIds = allDictPropertyIds, Take = int.MaxValue })).Results; foreach (var dictProperty in csvProducts.SelectMany(x => x.Properties).Where(x => x.Dictionary && x.Values?.Any(v => v != null) == true)) { foreach (var propertyValue in dictProperty.Values.Where(x => x.Value != null)) { // VP-5516: // For imported propertyValue the Alias field is empty - need to fill it from value. // For existing propertyValue Alias should be already filled, we shouldn't rewrite it. propertyValue.Alias = string.IsNullOrEmpty(propertyValue.Alias) ? propertyValue.Value.ToString() : propertyValue.Alias; var existentDictItem = allDictItems.FirstOrDefault(x => x.PropertyId == propertyValue.PropertyId && x.Alias.EqualsInvariant(propertyValue.Alias)); if (existentDictItem == null) { if (CreatePropertyDictionatyValues) { existentDictItem = new PropertyDictionaryItem { Alias = propertyValue.Alias, PropertyId = propertyValue.PropertyId }; allDictItems.Add(existentDictItem); await _propDictItemService.SaveChangesAsync(new[] { existentDictItem }); } else { progressInfo.Errors.Add($"The '{propertyValue.Alias}' dictionary item is not found in '{propertyValue.PropertyName}' dictionary"); progressCallback(progressInfo); } } propertyValue.ValueId = existentDictItem?.Id; } } }
private void ResolvePropertyDictionaryItems(List <CsvProduct> csvProducts, ExportImportProgressInfo progressInfo, Action <ExportImportProgressInfo> progressCallback) { var allDictPropertyIds = csvProducts.SelectMany(x => x.Properties).Where(x => x.Dictionary) .Select(x => x.Id).Distinct() .ToArray(); var allDictItems = _propDictItemSearchService.Search(new PropertyDictionaryItemSearchCriteria { PropertyIds = allDictPropertyIds, Take = int.MaxValue }).Results; foreach (var dictPropValue in csvProducts.SelectMany(x => x.PropertyValues).Where(x => x.Property != null && x.Property.Dictionary && !string.IsNullOrEmpty(x.Value?.ToString()))) { dictPropValue.Alias = dictPropValue.Value.ToString(); var existDictItem = allDictItems.FirstOrDefault(x => x.PropertyId == dictPropValue.Property.Id && x.Alias.EqualsInvariant(dictPropValue.Alias)); if (existDictItem == null) { if (_createPropertyDictionatyValues) { existDictItem = new PropertyDictionaryItem { Alias = dictPropValue.Alias, PropertyId = dictPropValue.Property.Id }; allDictItems.Add(existDictItem); _propDictItemService.SaveChanges(new[] { existDictItem }); } else { progressInfo.Errors.Add($"The property dictionary '{dictPropValue.Alias}' not found in '{dictPropValue.Property.Name}' dictionary"); progressCallback(progressInfo); } } dictPropValue.ValueId = existDictItem?.Id; } }