//[HttpPost] public IActionResult AddToCart(int id) { var product = _productService.GetByID(id); var userId = _userManager.GetUserId(User); _cartService.AddItemToCart(product, userId); return(RedirectToAction("Index")); }
/// <summary> /// This was one trickier, but on post we attach the user id to the cart. So when a new user is created, so is a cart and by doing so when a user clicks on add to cart, it attaches to the specific ids. /// </summary> /// <returns>Item in cart </returns> public async Task OnPostAsync() { var UserId = HttpContext.Request.Cookies["userId"]; CreateCart cart = await cartService.GetOne(UserId); CartItem cartItem = new CartItem() { MealId = Product.Id, Price = Product.Price, Quantity = cart.Quantity, CreateCartId = cart.Id }; CartItem record = await cartService.AddItemToCart(cartItem.MealId, cartItem.Price, cartItem.Quantity, cartItem.CreateCartId); }
public async Task <IActionResult> OnPost(int productId, int qty = 1) { var user = User.Identity.Name; ApplicationUser username = await _userManager.GetUserAsync(User); CartItems cartItem = await _cart.GetCartItemByProductIdForUserID(username.Id, productId); string email = username.Email; TempData["Email"] = email; int cartId = await _cart.GetCartIdForUserInt(user); if (cartId > 0) { if (cartItem == null) { CartItems cartitems = new CartItems(); cartitems.CartID = cartId; cartitems.Quantity = qty; cartitems.Product = await _context.GetProductByID(productId); await _cart.AddItemToCart(cartitems); product = cartitems.Product; } else { cartItem.Quantity++; await _cart.UpdateCartItemAsync(cartItem); } return(RedirectToAction("Cart", "Store")); } else { return(RedirectToPage("Register")); } }
public async Task <ActionResult> AddItemToCart(int mealId, int price, int quantity, int createCartId) { await _cart.AddItemToCart(mealId, price, quantity, createCartId); return(RedirectToPage("/cart")); }