Esempio n. 1
0
        public async Task <IActionResult> Index()
        {
            string tempCartId             = httpContextAccessor.HttpContext.Request.Cookies["tempCartId"];
            List <TempCartItem> tempCarts = cartServices.GetTempCartList(tempCartId);

            if (tempCarts != null)
            {
                foreach (TempCartItem item in tempCarts)
                {
                    cartServices.addToCart(UserName, item.Product.Id, item.Quantity);
                }
                await cartServices.DeleteTempCart(tempCartId);
            }
            List <CartItem>   cart      = cartServices.GetCartList(UserName);
            CartListViewModel viewModel = new CartListViewModel();

            if (cart != null)
            {
                viewModel.CartItems = cart
                                      .Select(x => new CartViewModel()
                {
                    Id           = x.Id,
                    ImageAddress = x.Product.PictureAddress,
                    Price        = x.Product.Price,
                    Quantity     = x.Quantity,
                    Title        = x.Product.Title
                }).ToList();
                viewModel.TotalPrice = cart.Sum(x => x.Quantity * x.Product.Price);
            }
            else
            {
                viewModel = new CartListViewModel()
                {
                    CartItems  = new List <CartViewModel>(),
                    TotalPrice = 0
                };
            }
            return(View(viewModel));
        }