void AddItemToCart(AddToCartViewModel viewModel, string cartData, HttpCookie cookie) { var cart = cartSerializer.Deserialize(cartData); cart = cartRepository.AddItem(cart, viewModel.ProductId, viewModel.Quantity); cookie.Value = cartSerializer.Serialize(cart); }
/// <summary> /// Adds item to shopping cart and redirect to shopping cart page. /// </summary> protected void ButtonAddToCart_Click(object sender, EventArgs e) { Page.Validate(); if (!Page.IsValid) { return; } // Retrieve product via Product Facade. var repository = new ProductRepository(); ActionServiceReference.Product product = repository.GetProduct(ProductId); // Get product details and add information to cart. int productId = product.ProductId; string name = product.ProductName; double unitPrice = product.UnitPrice; int quantity; if (!int.TryParse(TextBoxQuantity.Text.Trim(), out quantity)) { quantity = 1; } var cartRepository = new CartRepository(); cartRepository.AddItem(productId, name, quantity, unitPrice); // Show shopping cart to user. Response.Redirect(UrlMaker.ToCart()); }
public int AddItem(int ItemId, string ItemName) { Cart cart = new Cart(); int count = GetTotal(); cart.ItemId = ItemId; cart.ItemName = ItemName + " in cart " + count++; cartRepository.AddItem(cart); return(cart.Id); }
internal bool AddCartItem(string userInput) { var itemInfo = _itemRepository.GetItemFromId(userInput); if (itemInfo == null) { return(false); } _cartRepository.AddItem(itemInfo.Id); return(true); }
public void AddItem_ShouldAddItems() { var product = new Product { Code = "E", RetailPrice = 10 }; var cartLine = new CartLine { Product = product, Quantity = 5 }; cartRepository.AddItem(cartLine); Assert.Equal(cartLine, container.CartLines.First(c => c.Product.Code == "E")); }
/// <summary> /// GET: Cart/Add /// Add's the Product with the id from the parameter into the cart and store the result in the Session variable "CartItemAmount" /// </summary> /// <param name="id"></param> /// <returns>View with Model of IQueryable of Type Product</returns> public ActionResult Add(int id) { var product = _productRepository.GetById(id); var cartItems = _cartRepository.GetAll(); //Check if the product is already in the cart if (cartItems.ToList().Exists(p => p.Name == product.Name)) { cartItems.Where(p => p.Name == product.Name).ToList().ForEach(a => a.Amount++); } else { _cartRepository.AddItem(product); } //update total items Session["cartItemAmount"] = (int)Session["cartItemAmount"] + 1; var updatedCartItems = _cartRepository.GetAll(); ViewBag.Total = GetTotalPrice(updatedCartItems); return(View("Cart", updatedCartItems)); }