/// <summary> /// The purpose of this class is to insert new a new order into the database. /// </summary> /// <param name="Order">Type Domain.Models.Order. It will contain all data about customer, location, and a list of orderlines.</param> public int PlaceAnOrderForACustomer(Domain.Models.Order m_order) { // Create the Entity item to be put into the database Entities.OrderEntity order; order = MapperOrder.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 <Entities.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 (Entities.InventoryEntity i in dbStocks) { // We also need to iterate through all the Domain.Models.Stock list for the location foreach (var orderLine in m_order.Purchase) { // An extra measure is taken here just to be sure that only books that exists in the database are being changed. if (orderLine.BookISBN == i.BookIsbn) { i.Quantity -= orderLine.Quantity; } } } // Add the new order and orderlines to the database _context.Add(order); // Save those changes Save(); return(order.Id); }
/// <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 Domain.Models.Order 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 Domain with the relevant books FillBookLibrary(); // Create the order objects to be filled Entities.OrderEntity dbOrder; Domain.Models.Order m_order; // Try to see if the order even exists if it does then assign it dbOrder = _context.Orders .Include(ol => ol.Orderlines) .Include(c => c.Customer) .Include(l => l.Location) .First(o => o.Id == ordernumber); m_order = MapperOrder.MapOrderWithLocationCustomerAndOrderLines(dbOrder); return(m_order); }