public IHttpActionResult Purchase(int productId) { try { Product product = ProductRepository.GetProduct(productId); if (product == null) { return(Json(new { success = false, error = $"Product with id: {productId} not found.", })); } if (product.IsOutOfInventory()) { return(Json(new { success = false, error = $"Product with id: {productId} is out of inventory, hence cannot be purchased", })); } //Check if product has enough inventory to be able to add to the cart if (!CartRepository.CanAddProductToCart(product)) { return(Json(new { success = false, error = $"Product with id: {productId} does not have enough inventory to be added to cart", })); } CartRepository.AddProduct(product); return(Json(new { success = false, error = $"Product with id: {productId} successfully purchased", })); } catch (Exception e) { return(Json(new { success = false, error = e.Message, })); } }
public bool AddProduct(int cartId, int productId) { if (ProductRepository.Get(productId) == null) { return(false); } if (CartRepository.Get(cartId) == null) { return(false); } CartRepository.AddProduct(cartId, productId); return(true); }
public RedirectToRouteResult AddToCart(int ProductNo, string username, string returnUrl) { Product product = repository.Products.SingleOrDefault(x => x.ProductNo == ProductNo); int cartidx = 0; if (product != null) { cartidx = _cartrepo.AddProduct(ProductNo, username, 1); } TempData["returnUrl"] = returnUrl; return(RedirectToRoute(new { controller = "Cart", action = "Index", cartid = cartidx })); }