Exemplo n.º 1
0
        public async Task FillCartAsync(Cart cart, ICatalogService catalogService, IFinanceService financeService)
        {
            foreach (CartItem item in cart.Items)
            {
                item.Product = await catalogService.FindAsync(item.ProductId);

                item.Total = item.Quantity * item.Product.Price;
            }

            cart.SubTotal = cart.Items.Sum(x => x.Quantity * x.Product.Price);
            cart.Tax      = financeService.Tax;
            cart.TaxTotal = financeService.CalculateTax(cart.SubTotal);
            cart.Total    = cart.SubTotal + cart.TaxTotal;
        }
Exemplo n.º 2
0
        public async Task FillCartAsync(Cart cart, ICatalogService catalogService, IFinanceService financeService)
        {
            foreach (CartItem item in cart.Items)
            {
                /// <summary>
                ///     Use the bellow code to properly empty cart when an item that does not exist
                ///         enters the pool of items.
                /// </summary>
                //string productID = "001";
                //item.Product = await catalogService.FindAsync(productID);

                /// <summary>
                ///     Comment below lines out to properly delete all items in cart.
                /// </summary>
                if (item.ProductId.Length < 3)
                {
                    string productId = item.ProductId;
                    productId = productId.PadLeft(3, '0');

                    item.Product = await catalogService.FindAsync(productId);
                }
                else
                {
                    item.Product = await catalogService.FindAsync(item.ProductId);
                }

                if (item.Product != null)
                {
                    item.Total = item.Quantity * item.Product.Price;
                }
            }

            cart.SubTotal = cart.Items.Sum(x => x.Quantity * x.Product.Price);
            cart.Tax      = financeService.Tax;
            cart.TaxTotal = financeService.CalculateTax(cart.SubTotal);
            cart.Total    = cart.SubTotal + cart.TaxTotal;
        }