/// <summary> /// Send a console app to the database /// </summary> /// <param name="order"></param> public void SendGenOrderToDB(IOrder order) { // Create context using var context = new danielGProj0DBContext(_contextOptions); // Create a database GenOrder using info from the order passed into this method GenOrder dbGeneralOrder = new GenOrder { CustomerId = order.Customer.Id, StoreId = order.Location.Id, Cost = order.Cost, Date = order.Date }; // Add GenOrder to database and save change context.GenOrders.Add(dbGeneralOrder); context.SaveChanges(); // From here, see the last order to get the id and update the other tables int lastOrder = GetAmountOfGenOrders(); // Send the AggOrder of the products to the DB SendAggOrder(order, lastOrder); // Update the inventory of the store that had a purchase UpdateInventory(order); }
/// <summary> /// Method runs when creating a new /// </summary> /// <param name="customer"></param> public void CreateCustomerInDb(CustomerClass customer) { // Create Context using var context = new danielGProj0DBContext(_contextOptions); // Create a database customer using info from ConsoleApp Customer Customer dbCustomer = new Customer { Name = customer.Name }; // Add Customer to database and save change context.Customers.Add(dbCustomer); context.SaveChanges(); }
/// <summary> /// Update the inventory in the database after an order is placed /// </summary> /// <param name="order"></param> public void UpdateInventory(IOrder order) { // Create Context using var context = new danielGProj0DBContext(_contextOptions); // Get agg inventory items from the database- this is a stores inventory var itemsInInventory = context.AggInventories.Where(i => i.StoreId == order.Location.Id).ToList(); // Iterate over the items in the order foreach (var item in order.Customer.ShoppingCart) { // Find the item in the list of items that are in stock at that store // then decrement based on the amount ordered var currentItem = itemsInInventory.Find(x => x.Product == item.Key); currentItem.InStock -= item.Value; } // Save changes made and send to DB context.SaveChanges(); }
/// <summary> /// Send aggregate Order to database when an order is placed /// </summary> /// <param name="order"></param> /// <param name="orderID"></param> public void SendAggOrder(IOrder order, int orderID) { // Create Context using var context = new danielGProj0DBContext(_contextOptions); // Create the aggOrders list that need to get sent List <AggOrder> aggOrders = new List <AggOrder>(); // For each value in order.customer.shoppingCart, create a single aggOrder // then add that aggOrder to the database foreach (var product in order.Customer.ShoppingCart) { AggOrder orderDetails = new AggOrder { OrderId = orderID, Product = product.Key, Amount = product.Value }; context.AggOrders.Add(orderDetails); } context.SaveChanges(); }