/// <summary> /// User interface that guides the user to add Products to a single store /// </summary> /// <param name="pc">The ProductController</param> /// <param name="sc">The StoreController</param> private static void AddProductToStore(ProductController pc, StoreController sc) { //Select the store to add products to and make sure it exists Console.WriteLine("Which store do you want to add products to: \n"); sc.DisplayStores(); Console.WriteLine("Enter Store ID: "); string userIn = Console.ReadLine(); int sid; while (!int.TryParse(userIn, out sid)) { Console.WriteLine("Invalid input. Not a number."); userIn = Console.ReadLine(); } if (sc.repository.GetAll().Any(s => s.StoreId == sid)) { //Display the full set of products that exist in the database Console.WriteLine($"Which products do you want to add to the store: {sc.repository.GetById(sid).StoreName}"); pc.DisplayProducts(); //Select the product id, making sure it exists, to link a product to the store Console.WriteLine("Enter Product ID to add to store: "); userIn = Console.ReadLine(); int pid; while (!int.TryParse(userIn, out pid)) { Console.WriteLine("Invalid input. Not a number."); userIn = Console.ReadLine(); } if (pc.repository.GetAll().Any(p => p.ProductId == pid)) { //Get that product to be later added to the store inventory Products product = pc.repository.GetById(pid); //Enter the amount of products to be stored in the stores inventory Console.WriteLine("Enter the quantity of the product to be added:"); userIn = Console.ReadLine(); int qty; while (!int.TryParse(userIn, out qty)) { Console.WriteLine("Invalid input. Not a number."); userIn = Console.ReadLine(); } //Try adding the products to the store //If the product already existed in the store the product won't be added and an error is thrown try { using var context = new _2006StoreApplicationContext(GenericRepository <Products> .Options); var store = context.Stores .Include(s => s.Inventory) .First(s => s.StoreId == sid); store.Inventory.Add(new Inventory { Product = product, Amount = qty }); context.SaveChanges(); Console.WriteLine("Product was added to the store!"); } catch (Exception) { Console.WriteLine("Error occurred when trying to add product to store."); } } else { Console.WriteLine($"No products with ID: {pid}"); } } else { Console.WriteLine($"No stores with ID: {sid}"); } }
public void Save() { _context.SaveChanges(); }
/// <summary> /// User interface that guides the user to place an order for the current customer. /// </summary> /// <param name="currentCustomer">The current customer placing the order</param> /// <param name="sc">The StoreController</param> /// <param name="pc">The ProductController</param> /// <param name="oc">The OrderController</param> private static void PlaceOrder(Customers currentCustomer, StoreController sc, ProductController pc, OrderController oc) { //Select the store you want to place an order to Console.WriteLine("Which store would you like to place an order to: "); sc.DisplayStores(); Console.WriteLine("Enter the store ID: "); string userIn = Console.ReadLine(); int sid; while (!int.TryParse(userIn, out sid)) { Console.WriteLine("Invalid input. Not a number."); Console.WriteLine("Enter the store ID: "); userIn = Console.ReadLine(); } //If the store id that was inputted exists list the products in that store if (sc.repository.GetAll().Any(s => s.StoreId == sid)) { var currentStore = sc.repository.GetById(sid); using var context = new _2006StoreApplicationContext(GenericRepository <Stores> .Options); var inventory = context.Inventory .Include(i => i.Product) .Where(i => i.StoreId == sid) .ToList(); //Keep track of the number of products in the order to calculate the final total of the order later Dictionary <Products, int> productsInOrder = new Dictionary <Products, int>(); //Keep asking the user to add more products to the order until they type 0 while (true) { Console.WriteLine("Select a product to add to order:"); foreach (var item in inventory) { Console.WriteLine($"Product: {item.Product.ProductName} Price: ${item.Product.Price} ID: {item.Product.ProductId} In Stock: {item.Amount}\n"); } Console.WriteLine("Enter product ID to add to order(or type 0 to quit):"); userIn = Console.ReadLine(); int pid; while (!int.TryParse(userIn, out pid)) { Console.WriteLine("Invalid input. Not a number."); Console.WriteLine("Enter product ID to add to order(or type 0 to quit):"); userIn = Console.ReadLine(); } //prevent the user from adding the same item to their order if (productsInOrder != null) { while (productsInOrder.Keys.Any(p => p.ProductId == pid)) { Console.WriteLine("Item was already added to order."); foreach (var item in inventory) { Console.WriteLine($"Product: {item.Product.ProductName} Price: ${item.Product.Price} ID: {item.Product.ProductId} In Stock: {item.Amount}\n"); } Console.WriteLine("Enter product ID to add to order(or type 0 to quit):"); userIn = Console.ReadLine(); while (!int.TryParse(userIn, out pid)) { Console.WriteLine("Invalid input. Not a number."); Console.WriteLine("Enter product ID to add to order(or type 0 to quit):"); userIn = Console.ReadLine(); } } } if (pid == 0) { break; } //Check to see if the product id the user enter matches any of the products available in the store if (inventory.Any(i => i.Product.ProductId == pid)) { //Get the product info from the id the user entered, then get the amount from the inventory to //check that it is >= 0 var p = pc.repository.GetById(pid); Console.WriteLine($"How many {p.ProductName}s do you want to add to the order:"); userIn = Console.ReadLine(); int qty; while (!int.TryParse(userIn, out qty)) { Console.WriteLine("Invalid input. Not a number."); Console.WriteLine($"How many {p.ProductName}s do you want to add to the order:"); userIn = Console.ReadLine(); } if (qty > 0) { Inventory inventoryLine = inventory.First(i => i.Product.ProductId == pid); if (inventoryLine.Amount == 0) { Console.WriteLine($"{p.ProductName} no longer in stock."); } else if (inventoryLine.Amount < qty) { Console.WriteLine("You can't order more products than are available."); } //If the product is available and in stock, add keep track of the product, decrement the inventory, //and update the inventory when selecting more products else { productsInOrder.Add(p, qty); inventoryLine.Amount -= qty; context.Update(inventoryLine); context.SaveChanges(); Console.WriteLine("Product added to order!"); } } else { Console.WriteLine("Invalid qty. Input a positive integer."); } } else { Console.WriteLine($"Product with ID: {pid} is not available in the current store."); } } //Check that the user actually selected products before actually creating the order in the database if (productsInOrder.Count == 0) { Console.WriteLine("No products were added to order."); } else { //calculate the total cost of the order and display it to the user decimal totalCostOfOrder = 0; foreach (var item in productsInOrder.Keys) { totalCostOfOrder += (item.Price * productsInOrder[item]); } Console.WriteLine("Total cost of your order: $" + totalCostOfOrder); //ask for a description to uniquely identify the order in order to find it later Console.WriteLine("Please provide a unique description for your order: "); string desc = Console.ReadLine(); if (desc == null || oc.repository.GetAll().Any(o => o.OrderDescription.Equals(desc))) { Console.WriteLine("Description already exists or no description was entered."); } else { //Save the order, then retrieve it again to create the orderline for it Orders newOrder = new Orders { CustomerId = currentCustomer.CustomerId, StoreId = currentStore.StoreId, OrderDescription = desc, TotalCost = totalCostOfOrder }; oc.repository.Add(newOrder); oc.repository.Save(); newOrder = oc.repository.GetAll().First(o => o.OrderDescription.Equals(desc)); //Link products that were in the recently created order to the orderId //This creates a new OrderLine that keeps track of what products belong to what order foreach (var item in productsInOrder.Keys) { var product = context.Products .Include(p => p.OrderLines) .First(p => p.ProductId == item.ProductId); product.OrderLines.Add(new OrderLines { Order = newOrder, Amount = productsInOrder[item] }); } context.SaveChanges(); } } } else { Console.WriteLine($"Store with ID: {sid} does not exist."); } }