private async Task UpdateProductAttributesAsync(Product entityToUpdate, Delta <ProductDto> productDtoDelta)
        {
            // If no product attribute mappings are specified means we don't have to update anything
            if (productDtoDelta.Dto.ProductAttributeMappings == null)
            {
                return;
            }

            // delete unused product attribute mappings
            var toBeUpdatedIds           = productDtoDelta.Dto.ProductAttributeMappings.Where(y => y.Id != 0).Select(x => x.Id);
            var productAttributeMappings = await _productAttributeService.GetProductAttributeMappingsByProductIdAsync(entityToUpdate.Id);

            var unusedProductAttributeMappings = productAttributeMappings.Where(x => !toBeUpdatedIds.Contains(x.Id)).ToList();

            foreach (var unusedProductAttributeMapping in unusedProductAttributeMappings)
            {
                await _productAttributeService.DeleteProductAttributeMappingAsync(unusedProductAttributeMapping);
            }

            foreach (var productAttributeMappingDto in productDtoDelta.Dto.ProductAttributeMappings)
            {
                if (productAttributeMappingDto.Id > 0)
                {
                    // update existing product attribute mapping
                    var productAttributeMappingToUpdate = productAttributeMappings.FirstOrDefault(x => x.Id == productAttributeMappingDto.Id);
                    if (productAttributeMappingToUpdate != null)
                    {
                        productDtoDelta.Merge(productAttributeMappingDto, productAttributeMappingToUpdate, false);

                        await _productAttributeService.UpdateProductAttributeMappingAsync(productAttributeMappingToUpdate);

                        await UpdateProductAttributeValuesAsync(productAttributeMappingDto, productDtoDelta);
                    }
                }
                else
                {
                    var newProductAttributeMapping = new ProductAttributeMapping
                    {
                        ProductId = entityToUpdate.Id
                    };

                    productDtoDelta.Merge(productAttributeMappingDto, newProductAttributeMapping);

                    // add new product attribute
                    await _productAttributeService.InsertProductAttributeMappingAsync(newProductAttributeMapping);
                }
            }
        }
Ejemplo n.º 2
0
        private async Task MergeFramesAsync(AbcMattressModel model, ProductAttribute pa, Product product)
        {
            var pam = (await _productAttributeService.GetProductAttributeMappingsByProductIdAsync(product.Id))
                      .Where(pam => pam.ProductAttributeId == pa.Id)
                      .FirstOrDefault();
            var abcMattressEntry = _abcMattressEntryService.GetAbcMattressEntriesByModelId(model.Id)
                                   .Where(ame => pa.Name == $"Frame ({ame.Size})")
                                   .FirstOrDefault();

            if (abcMattressEntry == null)
            {
                return;
            }

            var frames = _abcMattressFrameService.GetAbcMattressFramesBySize(abcMattressEntry.Size);

            if (pam == null && frames.Any())
            {
                var sizeAttrs = await GetSizeAttributesAsync(product, abcMattressEntry);

                pam = new ProductAttributeMapping()
                {
                    ProductId             = product.Id,
                    ProductAttributeId    = pa.Id,
                    IsRequired            = false,
                    AttributeControlType  = AttributeControlType.DropdownList,
                    DisplayOrder          = 30,
                    TextPrompt            = "Frame",
                    ConditionAttributeXml = $"<Attributes><ProductAttribute ID=\"{sizeAttrs.pam.Id}\"><ProductAttributeValue><Value>{sizeAttrs.pav.Id}</Value></ProductAttributeValue></ProductAttribute></Attributes>"
                };
                await _productAttributeService.InsertProductAttributeMappingAsync(pam);
            }
            else if (pam != null && !frames.Any())
            {
                await _productAttributeService.DeleteProductAttributeMappingAsync(pam);
            }
            else if (pam != null)
            {
                await UpdatePamAsync(product, pam, abcMattressEntry);
            }

            if (!frames.Any())
            {
                return;
            }

            var existingFrames = (await _productAttributeService.GetProductAttributeValuesAsync(pam.Id))
                                 .Where(pav =>
                                        pav.ProductAttributeMappingId == pam.Id
                                        );
            var newFrames = frames.Select(np => np.ToProductAttributeValue(
                                              pam.Id
                                              )).OrderBy(f => f.PriceAdjustment).ToList();

            ApplyDisplayOrder(newFrames);

            var toBeDeleted = existingFrames
                              .Where(e => !newFrames.Any(n => n.Name == e.Name && n.DisplayOrder == e.DisplayOrder &&
                                                         n.PriceAdjustment == e.PriceAdjustment));
            var toBeInserted = newFrames
                               .Where(n => !existingFrames.Any(e => n.Name == e.Name && n.DisplayOrder == e.DisplayOrder &&
                                                               n.PriceAdjustment == e.PriceAdjustment));

            toBeInserted.ToList().ForEach(async n => await _productAttributeService.InsertProductAttributeValueAsync(n));
            toBeDeleted.ToList().ForEach(async e => await _productAttributeService.DeleteProductAttributeValueAsync(e));
        }