public void DisplayOrderDetails(int orderId) { if (repository.GetAll().Any(o => o.OrderId == orderId)) { var context = new storeapplicationContext(GeneralRepository <Orders> .Options); var order = context.Orders .Include(o => o.OrderLines) .ThenInclude(ol => ol.Product) .First(o => o.OrderId == orderId); Console.WriteLine($"Order ID: {order.OrderId} Total Cost: {order.Total}\n" + $"Placed by customer with ID: {order.CustomerId} On: {order.TimeOfOrder} At Store with ID: {order.LocationId}\n"); foreach (var ol in order.OrderLines) { Console.WriteLine($"Product: {ol.Product.Name}\nPrice: {ol.Product.Price}\nQty: {ol.Amount}\n"); } context.Dispose(); } else { Console.WriteLine($"No orders with ID: {orderId}"); } }
public GeneralRepository(storeapplicationContext _context) { this._context = _context; table = _context.Set <T>(); }
private static void PlaceOrder(Customers existingCustomer, StoreController storeControl, ProductController productControl, OrderController orderControl) { Console.WriteLine("Please choose the store that you would like to place an order with "); storeControl.DisplayStores(); Console.WriteLine("Enter the store ID: "); string userInput = Console.ReadLine(); int storeId; while (!int.TryParse(userInput, out storeId)) { Console.WriteLine("Please select a valid Store ID."); userInput = Console.ReadLine(); } if (storeControl.repository.GetAll().Any(s => s.StoreLocationId == storeId)) { var currentStore = storeControl.repository.GetById(storeId); using var context = new storeapplicationContext(GeneralRepository <StoreLocations> .Options); var inventory = context.Inventory .Include(i => i.Product) .Where(i => i.LocationId == storeId) .ToList(); Dictionary <Products, int> cartProducts = new Dictionary <Products, int>(); while (true) { Console.WriteLine("Please select an item to add to your order:"); foreach (var item in inventory) { Console.WriteLine($" {item.Product.ProductId} {item.Product.Name} ${item.Product.Price.ToString("0.##")} Amount in stock: {item.Quantity}\n"); } Console.WriteLine("Please enter a product ID to add item to your order or please type 0 to exit and complete your order"); userInput = Console.ReadLine(); int productId; while (!int.TryParse(userInput, out productId)) { Console.WriteLine("Please respond with a valid ID."); userInput = Console.ReadLine(); } if (cartProducts != null) { while (cartProducts.Keys.Any(p => p.ProductId == productId)) { Console.WriteLine("Item was already added to order."); foreach (var item in inventory) { Console.WriteLine($"ID: {item.Product.ProductId} Product Name: {item.Product.Name} Price: ${item.Product.Price} Amount in stock: {item.Quantity}\n"); } Console.WriteLine("Enter a product ID to add item to your order( or please type 0 to exit the order):"); userInput = Console.ReadLine(); while (!int.TryParse(userInput, out productId)) { Console.WriteLine("Please enter a valid product ID."); Console.WriteLine("Enter a product ID to add item to your order( or please type 0 to exit the order):"); userInput = Console.ReadLine(); } } } if (productId == 0) { break; } if (inventory.Any(i => i.Product.ProductId == productId)) { var p = productControl.repository.GetById(productId); Console.WriteLine($"How many {p.Name}s do you want to add to the order:"); userInput = Console.ReadLine(); int qty; while (!int.TryParse(userInput, out qty)) { Console.WriteLine("Please put in a valid amount."); Console.WriteLine($"How many {p.Name}s do you want to add to the order:"); userInput = Console.ReadLine(); } if (qty > 0) { Inventory inventoryLine = inventory.First(i => i.Product.ProductId == productId); if (inventoryLine.Quantity == 0) { Console.WriteLine($"{p.Name} no longer in stock."); } else if (inventoryLine.Quantity < qty) { Console.WriteLine("You can't order more products than are available."); } else { cartProducts.Add(p, qty); inventoryLine.Quantity -= qty; context.Update(inventoryLine); context.SaveChanges(); Console.WriteLine("Product added to order!"); } } else { Console.WriteLine("Quantity must be positive."); } } else { Console.WriteLine($"Unfortunately product number {productId} is not available in this store."); } } if (cartProducts.Count == 0) { Console.WriteLine("No products were added to order."); } else { decimal totalCostOfOrder = 0; foreach (var item in cartProducts.Keys) { totalCostOfOrder += (item.Price * cartProducts[item]); } Console.WriteLine("Your total comes out to $" + totalCostOfOrder.ToString("0.##")); Orders newOrder = new Orders { CustomerId = existingCustomer.CustomerId, LocationId = currentStore.StoreLocationId, Total = totalCostOfOrder, TimeOfOrder = DateTime.Now }; orderControl.repository.Add(newOrder); orderControl.repository.Save(); newOrder = orderControl.repository.GetAll().First(o => o.CustomerId.Equals(existingCustomer.CustomerId)); foreach (var item in cartProducts.Keys) { var product = context.Products .Include(p => p.OrderLines) .First(p => p.ProductId == item.ProductId); product.OrderLines.Add(new OrderLines { OrderId = newOrder.OrderId, Order = newOrder, Amount = cartProducts[item] }); } context.SaveChanges(); } } else { Console.WriteLine($"A store does not exist with that ID. Please use a valid store ID."); } }
public GeneralRepository() { this._context = new storeapplicationContext(); table = _context.Set <T>(); }