public IActionResult AddToCart(ShoppingListViewModel cart) { string sessionId = HttpContext.Session.GetString("customerId"); Guid customerId = new Guid(sessionId); CartInfoViewModel updatedCart = _logic.AddToCart(cart.ProductName, cart.QuantityAdded, cart.StoreId, customerId); if (updatedCart == null) { ModelState.AddModelError("Failure", "Customer, Product, Store or Inventory does not exist"); ShoppingListViewModel storeInventory = _logic.GetStoreInventory(cart.StoreId); return(View("Store", storeInventory)); } // if you are trying too add too much quantity if (updatedCart.error == 1) { TempData["ModelState"] = $"{updatedCart.errorMessage}"; return(RedirectToAction("Store", "Shop", new { id = cart.StoreId })); } return(View("ViewCart", updatedCart)); }
/// <summary> /// Checks to see if the user has selected enough or too many products /// from the list of available stock /// </summary> /// <param name="amountToAddView"></param> /// <returns></returns> public IActionResult CanBeAdded(AmountToAddViewModel amountToAddView) { if (amountToAddView.amount > amountToAddView.stock) { ModelState.AddModelError("Failure", "The amount you chose exceeded stock"); return(View("AddToCart", amountToAddView)); } if (amountToAddView.amount != 0) { ModelState.AddModelError("Success", "Item(s) added to cart"); _businessLogicClass.AddToCart(amountToAddView); _businessLogicClass.SubtractFromInventory(amountToAddView.inventory, amountToAddView.amount); } List <InventoryViewModel> inventoryViewModels = _businessLogicClass.SearchInventoryList(amountToAddView.location, amountToAddView.CustomerId); return(View("InventoryList", inventoryViewModels)); }
/// <summary> /// Takes in chosen product from location and returns cart page view with chosen product /// </summary> /// <param name="productViewModel"></param> /// <returns></returns> public ActionResult Cart(ProductViewModel productViewModel) { ProductViewModel product = new ProductViewModel(); product.ProductId = productViewModel.ProductId; listOfProducts = _businessLogicClass.AddToCart(product); if (listOfProducts != null) { try { return(View(listOfProducts[0])); } catch (ArgumentOutOfRangeException aore) { _logger.LogInformation($"Trying to view empty cart threw error, {aore}"); return(View(productViewModel)); } } else { return(View(productViewModel)); } }