public async Task <IActionResult> AddItem(
            [Bind("ProductId", "Quantity", "ReturnUrl")] BasketAdderViewModel vm)
        {
            // Server-side validation for view components might be tricky. For now,
            // redirect the user to the Shop Index. jQuery client-side validation will
            // improve this experience once implemented
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index", "Shop"));
            }

            ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);

            Basket basket = user.CurrentBasketId.HasValue ?
                            await _productDbContext.Baskets.FindAsync(user.CurrentBasketId) : null;

            // If the user doesn't have an open basket yet, then create one
            if (basket is null || basket.Closed)
            {
                basket = (await _productDbContext.Baskets.AddAsync(new Basket()
                {
                    Closed = false,
                    CreationTime = DateTime.UtcNow,
                    UserId = user.Id
                })).Entity;

                await _productDbContext.SaveChangesAsync();

                // Set the user's current basket id to the new basket manually
                // since the user exists on a different context than the basket
                user.CurrentBasketId = basket.Id;
                await _userManager.UpdateAsync(user);
            }

            BasketItem item = await _productDbContext.BasketItems.FirstOrDefaultAsync(bi => bi.BasketId == basket.Id &&
                                                                                      bi.ProductId == vm.ProductId);

            // If the basket item doesn't exist then create it. Otherwise, just add the specified
            // quantity to the existing item
            if (item is null)
            {
                await _productDbContext.BasketItems.AddAsync(new BasketItem()
                {
                    BasketId  = basket.Id,
                    ProductId = vm.ProductId,
                    Quantity  = vm.Quantity,
                    UserId    = user.Id
                });
            }
            else
            {
                item.Quantity += vm.Quantity;
                _productDbContext.BasketItems.Update(item);
            }

            // Save changes and return the user to whence they came
            await _productDbContext.SaveChangesAsync();

            return(RedirectToLocal(vm.ReturnUrl));
        }
        public async Task <IActionResult> AddItem(BasketAdderViewModel vm)
        {
            //If something slips in the model toss the user back to the store page
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index", "Products"));
            }

            //pull the user from the current logged in HttpContext
            ApplicationUser user = await _userManager.GetUserAsync(HttpContext.User);

            //Set our Basket to the current one if it exists
            Basket basket = user.CurrentBasketId.HasValue ?
                            await _productDbContext.Baskets.FindAsync(user.CurrentBasketId) : null;

            // If the user has no basket open, then create one
            if (basket == null)
            {
                basket = (await _productDbContext.Baskets.AddAsync(new Basket()
                {
                    UserId = user.Id,
                    CheckedOut = false
                })).Entity;

                await _productDbContext.SaveChangesAsync();

                //Set the basket ID for the user
                user.CurrentBasketId = basket.Id;
                await _userManager.UpdateAsync(user);
            }

            BasketItem item = await _productDbContext.BasketItems.FirstOrDefaultAsync(bi => bi.BasketId == basket.Id);

            // Add the item to the basket or if it already exists add to the quantity
            if (item is null)
            {
                await _productDbContext.BasketItems.AddAsync(new BasketItem()
                {
                    BasketId  = basket.Id,
                    ProductId = vm.ProductId,
                    Quantity  = vm.Quantity,
                });
            }
            else
            {
                item.Quantity += vm.Quantity;
                _productDbContext.BasketItems.Update(item);
            }
            return(RedirectToAction("Index", "Products"));
        }