public ActionResult Add(int id = 0) { //Add to Cart _cartManager.AddCartItem(id); //Redirect back to Index return(RedirectToAction("index")); }
public ActionResult Add(int id = 0) { _cartManager.AddCartItem(id); return(RedirectToAction("Index")); }
static void Main(string[] args) { using (var context = new DataContext()) { var inventoryRepo = new InventoryRepository(context); var cartRepo = new InMemoryCartRepository(); //define business manager and repository var inventoryManager = new InventoryManager(inventoryRepo); var cartManager = new CartManager(cartRepo, inventoryRepo); ConsoleKeyInfo keyPressed = default(ConsoleKeyInfo); do { WriteLine("1 - List Products"); WriteLine("2 - Display Cart Items"); WriteLine("3 - Add Item to Cart"); WriteLine("4 - Place Order"); WriteLine("Press Command.. Q to exit"); keyPressed = ReadKey(); WriteLine(""); switch (keyPressed.Key) { case ConsoleKey.D1: //List Products var products = inventoryManager.GetProducts(); WriteLine("================ INVENTORY =================="); foreach (var prod in products) { WriteLine($"{prod.ProductId} - {prod.Name} {prod.Price}"); } break; case ConsoleKey.D2: //Display Cart Items var items = cartManager.GetCartItems(); if (items.Any()) { WriteLine("================ CART =================="); foreach (var item in items) { WriteLine($"{item.Quantity} - {item.Product.Name} {item.Product.Price}"); } } else { WriteLine($"No cart item for user Please add item"); } break; case ConsoleKey.D3: //Add Item to Cart WriteLine("Specify the Product id"); //get userid string id = ReadLine(); var productId = int.Parse(id); bool result = cartManager.AddCartItem(productId); if (result) { WriteLine("item added to cart"); } else { WriteLine("invalid data or error occurred"); } break; } WriteLine(Environment.NewLine); } while (keyPressed.Key != ConsoleKey.Q); } }
static void Main(string[] args) { //define business manager and repository var inventoryManager = new InventoryManager(); var cartManager = new CartManager(); ConsoleKeyInfo keyPressed = default(ConsoleKeyInfo); do { WriteLine("1 - List Products"); WriteLine("2 - Display Cart Items"); WriteLine("3 - Add Item to Cart"); WriteLine("4 - Place Order"); WriteLine("Press Command.. Q to exit"); keyPressed = ReadKey(); WriteLine(""); switch (keyPressed.Key) { case ConsoleKey.D1: var products = inventoryManager.GetProducts(); foreach (var product in products) { WriteLine($"{product.Name} {product.Price}"); } break; case ConsoleKey.D2: WriteLine("Type your userid:"); string input = ReadLine(); int userid = int.Parse(input); //call the business manager var cart = cartManager.GetCart(userid); if (cart != null) { WriteLine($"cart item for user {userid}"); foreach (var item in cart.Items) { WriteLine($"{item.Product.Name} {item.Product.Price}"); } } else { WriteLine($"No cart item for user {userid}. Please add item"); } break; case ConsoleKey.D3: WriteLine("Specify your user id"); //get userid string id = ReadLine(); userid = int.Parse(id); WriteLine("Specify your product id"); string productinput = ReadLine(); int productid = int.Parse(productinput); //create cartitem object CartModel cartmodel = new CartModel(); cartmodel.UserId = userid; cartmodel.Items.Add(new ItemModel { ProductId = productid }); bool result = cartManager.AddCartItem(cartmodel); if (result) { WriteLine("item added to cart"); } else { WriteLine("invalid data or error occurred"); } break; } WriteLine(Environment.NewLine); } while (keyPressed.Key != ConsoleKey.Q); }