/// <summary> /// The purpose of this class is to insert new a new order into the database. /// </summary> /// <param name="Order">Type Library.Models.Order. It will contain all data about customer, location, and a list of orderlines.</param> public void PlaceAnOrderForACustomer(Library.Models.Order m_order) { // Create the Entity item to be put into the database OrderEntity order; order = Mapper_Order.MapOrderWithOrderLines(m_order); // We need to grab the entity objects from the database for the inventory rows for the given location. // This is so we can update them accordingly. IEnumerable <InventoryEntity> dbStocks = _context.Inventories.Where(i => i.LocationId == m_order.LocationPlaced.ID); // Since we are returned all the rows of inventory we need to iterate through each one to update it // This is done no matter if there was 1 purchase or many changing the inventory just to be sure // everything is updated correctly. foreach (InventoryEntity i in dbStocks) { // We also need to iterate through all the Library.Models.Stock list for the location foreach (Library.Models.Stock stock in m_order.LocationPlaced.Inventory) { // An extra measure is taken here just to be sure that only books that exists in the database are being changed. if (stock.Book.ISBN == i.BookIsbn) { // Set the new quantity i.Quantity = stock.Quantity; } } } // Add the new order and orderlines to the database _context.Add(order); // Save those changes Save(); }
/// <summary> /// The purpose of this method is to return the string version of an order, given an order number. /// It not only returns the information on the customer but also each order line. /// </summary> /// <param name="ordernumber"></param> /// <returns>A string version of an order summary.</returns> public string GetDetailsForOrder(int ordernumber) { // This method is called because we need the information on the whole catalog // Since the catalog is small I went with this implementation. // If it was much large I would only fill the library with the relevant books FillBookLibrary(); // Create the order objects to be filled OrderEntity dbOrder; Library.Models.Order m_order; // Try to see if the order even exists if it does then assign it try { dbOrder = _context.Orders .Include(ol => ol.Orderlines) .Include(c => c.Customer) .Include(l => l.Location) .First(o => o.Id == ordernumber); m_order = Mapper_Order.MapOrderWithLocationCustomerAndOrderLines(dbOrder); } catch (InvalidOperationException ex) { // if it doesn't then return the error along with a descriptive text return($"{ex.Message}\nOrder does not exist!"); } return($"{m_order}\n{m_order.CustomerPlaced}\t{m_order.LocationPlaced}"); }