示例#1
0
        public async Task <IActionResult> Create(int productId, string userId, int quantity)
        {
            ApplicationUser user = await _user.Users.FirstOrDefaultAsync(u => u.Id == userId);

            var foundCart = await _dbContext.Carts.FirstOrDefaultAsync(c => c.OrderFulfilled == false && c.User == user);

            var lineItem = await _context.GetLineItemByProduct(foundCart.ID, productId);

            // updates the item quantity if the item is already in the cart
            if (lineItem != null)
            {
                lineItem.Quantity = (Quantity)quantity;
                await Edit(lineItem.ID, lineItem);
            }
            else
            {
                lineItem = new LineItem()
                {
                    ProductID = productId,
                    CartID    = foundCart.ID,
                    Quantity  = (Quantity)quantity
                };

                await _context.CreateLineItem(lineItem);
            }
            return(RedirectToAction("Index", "Products"));
        }