public ProductsApplicationAutoMapperProfile(IAttributeOptionIdsSerializer attributeOptionIdsSerializer) { /* You can configure your AutoMapper mapping configuration here. * Alternatively, you can split your mapping configurations * into multiple profile classes for a better organization. */ CreateMap <Product, ProductDto>() .Ignore(dto => dto.ProductGroupDisplayName) .Ignore(dto => dto.Sold) .Ignore(dto => dto.MinimumPrice) .Ignore(dto => dto.MaximumPrice); CreateMap <ProductDetail, ProductDetailDto>(); CreateMap <ProductAttribute, ProductAttributeDto>(); CreateMap <ProductAttributeOption, ProductAttributeOptionDto>(); CreateMap <ProductSku, ProductSkuDto>() .ForSourceMember(entity => entity.SerializedAttributeOptionIds, opt => opt.DoNotValidate()) .Ignore(dto => dto.AttributeOptionIds) .Ignore(dto => dto.Price) .Ignore(dto => dto.DiscountedPrice) .Ignore(dto => dto.Inventory) .Ignore(dto => dto.Sold) .AfterMap(async(src, dest) => dest.AttributeOptionIds = (await attributeOptionIdsSerializer.DeserializeAsync(src.SerializedAttributeOptionIds)).ToList()); CreateMap <CreateUpdateProductDetailDto, ProductDetail>(MemberList.Source) .ForSourceMember(dto => dto.StoreId, opt => opt.DoNotValidate()); CreateMap <CreateUpdateProductAttributeDto, ProductAttribute>(MemberList.Source); CreateMap <CreateUpdateProductAttributeOptionDto, ProductAttributeOption>(MemberList.Source); CreateMap <Category, CategoryDto>(); CreateMap <Category, CategorySummaryDto>(); CreateMap <ProductCategory, ProductCategoryDto>(); CreateMap <ProductHistory, ProductHistoryDto>(); CreateMap <ProductDetailHistory, ProductDetailHistoryDto>(); CreateMap <ProductView, ProductViewDto>(); CreateMap <Product, ProductView>(MemberList.Destination); }
protected virtual async Task CheckSkuAttributeOptionsAsync(Product product, ProductSku productSku) { var attributeOptionIds = (await _attributeOptionIdsSerializer.DeserializeAsync(productSku.SerializedAttributeOptionIds)) .ToList(); if (!product.ProductAttributes.TrueForAll(attribute => attribute.ProductAttributeOptions.Select(option => option.Id).Intersect(attributeOptionIds) .Count() == 1)) { throw new ProductSkuIncorrectAttributeOptionsException(product.Id, productSku.SerializedAttributeOptionIds); } if (product.ProductSkus.Where(sku => sku.Id != productSku.Id).FirstOrDefault(sku => sku.SerializedAttributeOptionIds.Equals(productSku.SerializedAttributeOptionIds)) != null) { throw new ProductSkuDuplicatedException(product.Id, productSku.SerializedAttributeOptionIds); } }
protected virtual async Task UpdateProductAttributesAsync(Product product, CreateUpdateProductDto input) { var isProductSkusEmpty = product.ProductSkus.IsNullOrEmpty(); var usedAttributeOptionIds = new HashSet <Guid>(); foreach (var serializedAttributeOptionIds in product.ProductSkus.Select(sku => sku.SerializedAttributeOptionIds)) { foreach (var attributeOptionId in await _attributeOptionIdsSerializer.DeserializeAsync( serializedAttributeOptionIds)) { usedAttributeOptionIds.Add(attributeOptionId); } } foreach (var attributeDto in input.ProductAttributes) { var attribute = product.ProductAttributes.FirstOrDefault(a => a.DisplayName == attributeDto.DisplayName); if (attribute == null) { if (!isProductSkusEmpty) { throw new ProductAttributesModificationFailedException(); } attribute = new ProductAttribute(GuidGenerator.Create(), attributeDto.DisplayName, attributeDto.Description); product.ProductAttributes.Add(attribute); } foreach (var optionDto in attributeDto.ProductAttributeOptions) { var option = attribute.ProductAttributeOptions.FirstOrDefault(o => o.DisplayName == optionDto.DisplayName); if (option == null) { option = new ProductAttributeOption(GuidGenerator.Create(), optionDto.DisplayName, optionDto.Description); attribute.ProductAttributeOptions.Add(option); } } var removedOptionNames = attribute.ProductAttributeOptions.Select(o => o.DisplayName) .Except(attributeDto.ProductAttributeOptions.Select(o => o.DisplayName)).ToList(); if (!isProductSkusEmpty && removedOptionNames.Any() && usedAttributeOptionIds .Intersect(attribute.ProductAttributeOptions .Where(option => removedOptionNames.Contains(option.DisplayName)) .Select(option => option.Id)).Any()) { throw new ProductAttributeOptionsDeletionFailedException(); } attribute.ProductAttributeOptions.RemoveAll(o => removedOptionNames.Contains(o.DisplayName)); } var removedAttributeNames = product.ProductAttributes.Select(a => a.DisplayName) .Except(input.ProductAttributes.Select(a => a.DisplayName)).ToList(); if (!isProductSkusEmpty && removedAttributeNames.Any()) { throw new ProductAttributesModificationFailedException(); } product.ProductAttributes.RemoveAll(a => removedAttributeNames.Contains(a.DisplayName)); }