示例#1
0
        public Task <VariantsResponce> Handle(VariantsRequest request, CancellationToken cancellationToken)
        {
            var variationReferences = _relationRepository.GetChildren <ProductVariation>(request.ProductReference);
            var variations          = _contentLoader.GetItems(variationReferences.Select(x => x.Child), new LoaderOptions {
                LanguageLoaderOption.FallbackWithMaster()
            }).OfType <MovieVariant>();

            var prices   = _customerPriceService.GetPrices(variations.Select(x => x.Code)).ToDictionary(x => x.CatalogKey.CatalogEntryCode, y => y);
            var discount = _customerPriceService.GetDiscountPrices(variations.Select(x => x.ContentLink)).ToDictionary(x => x.EntryLink, y => y.DiscountPrices);
            var result   = variations.Select(x => new Variant()
            {
                Code        = x.Code,
                Name        = x.Name,
                Primary     = x.IsPrimary,
                DisplayName = x.MediaTypes,
                NormalPrice = prices.GetPrice(x.Code),
                Discounts   = discount.GetDiscounts(x.ContentLink)
            }).ToList();

            var responce = new VariantsResponce()
            {
                Variants = result
            };

            if (string.IsNullOrEmpty(request.CurrentVariantCode))
            {
                responce.ActiveVariant = result.Where(x => x.Primary).FirstOrDefault();
            }
            else
            {
                responce.ActiveVariant = result.Where(x => x.Code == request.CurrentVariantCode).FirstOrDefault();
            }
            return(Task.FromResult(responce));
        }
        public async Task <CartContentResponce> Handle(CartContentRequest request, CancellationToken cancellationToken)
        {
            var cart = _cartFactory.LoadOrCreateCart();

            var allLineItems  = cart.GetAllLineItems();
            var lineItemCodes = allLineItems.Select(x => x.Code).Distinct();
            var variants      = _contentLoader.GetItems(_referenceConverter.GetContentLinks(lineItemCodes).Select(x => x.Value), new LoaderOptions {
                LanguageLoaderOption.FallbackWithMaster()
            }).OfType <MovieVariant>();
            var prices    = _customerPriceService.GetPrices(variants.Select(x => x.Code)).ToDictionary(x => x.CatalogKey.CatalogEntryCode, x => x);
            var discounts = _customerPriceService.GetDiscountPrices(variants.Select(x => x.ContentLink)).ToDictionary(x => x.EntryLink, x => x);
            var products  = variants.Select(
                x => new
            {
                variant = x.ContentLink,
                product = _contentLoader.GetItems(x.GetParentProducts(), new LoaderOptions {
                    LanguageLoaderOption.FallbackWithMaster()
                }).OfType <MovieProduct>()
            }).ToDictionary(x => x.variant, x => x.product.FirstOrDefault());

            var lineItems = cart.GetAllLineItems().Select(x => new LineItem()
            {
                Code             = x.Code,
                DisplayName      = x.DisplayName,
                Quantity         = Convert.ToInt32(x.Quantity),
                ImageUrl         = "",//products[variants.Where(y => y.Code == x.Code).Select(y => y.ContentLink).First()].PosterPath,
                Price            = prices[x.Code].UnitPrice.ToString(),
                DiscountPrice    = discounts[variants.First(y => y.Code == x.Code).ContentLink].DiscountPrices.Last().Price.ToString(),
                ProductReference = products[variants.Where(y => y.Code == x.Code).Select(y => y.ContentLink).First()].ContentLink
            });

            var total            = _orderGroupCalculator.GetOrderGroupTotals(cart);
            var tax              = _orderGroupCalculator.GetTaxTotal(cart);
            var orderDiscount    = _orderGroupCalculator.GetOrderDiscountTotal(cart);
            var lineItemDiscount = new Money(cart.GetAllLineItems().Sum(x => x.GetEntryDiscount()), cart.Currency);
            var noDiscount       = new Money(allLineItems.Sum(x => prices[x.Code].UnitPrice.Amount * x.Quantity), cart.Currency);
            var model            = new CartContentResponce()
            {
                LineItems     = lineItems,
                Total         = total.Total.ToString(),
                ItemsDiscount = lineItemDiscount.ToString(),
                OrderDiscount = orderDiscount.ToString(),
                NoDiscount    = noDiscount.ToString()
            };

            return(await Task.FromResult(model));
        }