Beispiel #1
0
        public override async Task <IDisplayResult> UpdateAsync(PriceVariantsPart part, IUpdateModel updater, UpdatePartEditorContext context)
        {
            var updateModel = new PriceVariantsPartViewModel();

            if (await updater.TryUpdateModelAsync(updateModel, Prefix, t => t.VariantsValues, t => t.VariantsCurrencies))
            {
                // Remove any content or the variants would be merged and not be cleared
                part.Content.Variants.RemoveAll();

                part.Variants = updateModel.VariantsValues
                                .Where(x => x.Value.HasValue &&
                                       updateModel.VariantsCurrencies?.ContainsKey(x.Key) == true &&
                                       updateModel.VariantsCurrencies[x.Key] != Currency.UnspecifiedCurrency.CurrencyIsoCode)
                                .ToDictionary(x => x.Key,
                                              x => _moneyService.Create(x.Value.Value, updateModel.VariantsCurrencies[x.Key]));
            }

            return(Edit(part, context));
        }
Beispiel #2
0
        private Task BuildViewModel(PriceVariantsPartViewModel model, PriceVariantsPart part)
        {
            model.ContentItem       = part.ContentItem;
            model.PriceVariantsPart = part;

            var allVariantsKeys = _predefinedValuesProductAttributeService.GetProductAttributesCombinations(part.ContentItem);

            model.Variants = part.Variants ?? new Dictionary <string, Amount>();

            model.VariantsValues = allVariantsKeys.ToDictionary(x => x,
                                                                x => model.Variants.TryGetValue(x, out var amount)
                    ? new decimal?(amount.Value)
                    : null);

            model.VariantsCurrencies = allVariantsKeys.ToDictionary(x => x,
                                                                    x => model.Variants.TryGetValue(x, out var amount)
                    ? amount.Currency.CurrencyIsoCode
                    : Currency.UnspecifiedCurrency.CurrencyIsoCode);

            return(Task.CompletedTask);
        }