//get history (req) /// <summary> /// Given a model location, return all orders placed from that location. /// Also updates the model with any missing orders. /// </summary> /// <param name="l">The model location.</param> /// <returns>List of orders.</returns> public IEnumerable <IOrder> GetOrderHistory(Store.Location l) { if (l is null) { _logger.LogError("Null argument to GetOrderHistory - store"); throw new ArgumentNullException(); } Location location = _context.Locations .Where(loc => loc.LocationName == l.LocationName) .Include(loc => loc.Orders) .ThenInclude(order => order.Customer) .Include(Loc => Loc.Orders) .ThenInclude(ord => ord.OrderItems) .ThenInclude(ordi => ordi.Item) .FirstOrDefault(); var orders = location.Orders.ToList(); foreach (Order LocationOrder_DB in orders) { bool foundEquiv = HasEquivilentOrder(LocationOrder_DB); if (!foundEquiv) { Console.WriteLine("no equiv found, creating order."); checkForModelMissingOrderData(LocationOrder_DB); Db_StoreMapper.MapAndAddOrderToModel(LocationOrder_DB); } } return(Store.Orders.Instance.GetOrdersByLocation(l)); }
//get history (req) /// <summary> /// Gets all unique order histories involving a customer, and loads them into the model /// if they're not already there. /// </summary> /// <param name="c">The model's version of the customer.</param> /// <returns> A list of all IOrders related to the customer.</returns> public IEnumerable <IOrder> GetOrderHistory(Store.Customer c) { Customer customer = GetDBCustomerByName(c.CustomerName); customer = _context.Customers .Where(cust => cust.Id == customer.Id) .Include(cust => cust.Orders) .ThenInclude(ord => ord.OrderItems) .ThenInclude(ordi => ordi.Item) .Include(cust => cust.StoreLocationNavigation) .FirstOrDefault(); foreach (Order CustomerOrder_DB in customer.Orders) { bool foundEquiv = HasEquivilentOrder(CustomerOrder_DB); if (!foundEquiv) { checkForModelMissingOrderData(CustomerOrder_DB); Db_StoreMapper.MapAndAddOrderToModel(CustomerOrder_DB); } } return(Store.Orders.Instance.GetOrdersByCustomer(c)); }
/// <summary> /// Takes a datamodel customer and turns it into a store customer. /// </summary> /// <param name="DbCustomer">A data model customer to be added to the model</param> /// <returns>A Model customer</returns> public static Store.Customer MapCustomerToStore(DataModel.Customer DbCustomer) { if (DbCustomer != null) { Name name = Db_StoreMapper.getCustomerName(DbCustomer); if (Customers.Instance.HasCustomer(name)) { //something weird happened probably. Expecting customers to be gotten from //the model first before checking DB. Console.Error.WriteLine($"Warning: Customer {name} already existed in the model"); return(Customers.Instance.GetCustomer(name)); } else { if (DbCustomer.StoreLocation != null) { return(Customers.Instance.RegisterCustomer(name, Locations.Instance.GetOrRegisterLocation(DbCustomer.StoreLocation))); } return(Customers.Instance.RegisterCustomer(name, DbCustomer.StoreLocation)); } } else { return(null); } }
/// <summary> /// Get a customer from the db by name, and register it with the data model assuming it doesn't already /// exist in the model. /// </summary> /// <param name="name">The name of the customer.</param> /// <returns>The customer created (or returned) by the Customers object in the datamodel, or null if that customer isn't /// in the Database.</returns> public Store.Customer GetCustomerByName(Name name) { Customer DBCustomer = GetDBCustomerByName(name); if (DBCustomer != null) { return(Db_StoreMapper.MapCustomerToStore(DBCustomer)); } else { _logger.LogError($"Customer, {name}, is not in the DB."); return(null); } }
/// <summary> /// Takes an order, and checks that the customer and the store are in the model. /// If they are not, then they are loaded into the model. /// </summary> /// <param name="o">The DB Order to be checked.</param> private void checkForModelMissingOrderData(DataModel.Order o) { if (!Locations.Instance.HasLocation(o.StoreLocation)) { //load up the location GetLocation(o.StoreLocation); } Name custname = Db_StoreMapper.getCustomerName(o.Customer); if (!Customers.Instance.HasCustomer(custname)) { GetCustomerByName(custname); } }
/// <summary> /// Get a list of all locations from the DB, and updates the model if any are missing. /// </summary> /// <returns>List of all stores</returns> IEnumerable <Store.Location> IDbRepository.GetLocations() { //get all customers from DB HashSet <Store.Location> locations = new HashSet <Store.Location>(); //convert and check if in model foreach (Location l in _context.Locations.Include(store => store.Invintories).ThenInclude(inv => inv.ItemNameNavigation)) { Store.Location NewLocation = Db_StoreMapper.MapLocationToStore(l); locations.Add(NewLocation); } //return list return(locations); }
/// <summary> /// Loads all the DB Items into memory /// </summary> /// <remarks> /// Will have uncaught exceptions if used more than once. This is probably a compramise and /// impracticle for large databases but makes logic easier. /// </remarks> void IDbRepository.LoadDBDataToModel() { //get all locations -> model foreach (Location l in _context.Locations) { Store.Locations.Instance.RegisterLocation(l.LocationName); } //get customers -> model foreach (Customer c in _context.Customers) { Store.Customer newcust = Store.Customers.Instance.RegisterCustomer(Db_StoreMapper.getCustomerName(c), c.StoreLocation); } //get all items -> model foreach (Item i in _context.Items) { Store.StoreCatalogue.Instance.RegisterItem(i.ItemName, i.ItemPrice); } //get all orders -> model // as historic orders foreach (Order o in _context.Orders.Include(oi => oi.OrderItems).ThenInclude(oi => oi.Item).Include(oi => oi.Customer)) { //get all items in the order ICollection <ItemCount> orderItems = new List <ItemCount>(); foreach (OrderItem orderItem in o.OrderItems) { orderItems.Add(new ItemCount(orderItem.Quantity, orderItem.Item.ItemName)); } Store.Orders.Instance.CreateAndAddPastOrder( o.StoreLocation, Db_StoreMapper.getCustomerName(o.Customer), o.OrderTime, orderItems, o.OrderTotal, o.Id ); } //get all store invintories foreach (Invintory i in _context.Invintories) { Locations.Instance.GetLocation(i.StoreLocation).AddInventory(i.ItemName, i.Quantity); } }
/// <summary> /// Get a specific store from the db. /// </summary> /// <param name="storeName"></param> /// <returns></returns> public Store.Location GetLocation(string storeName) { Location store = _context.Locations .Where(str => str.LocationName == storeName) .Include(store => store.Invintories) .ThenInclude(invintory => invintory.ItemNameNavigation) .FirstOrDefault(); if (store != null) { return(Db_StoreMapper.MapLocationToStore(store)); } else { _logger.LogError($"No location by the name of {storeName}"); throw new ArgumentException($"No location by the name of {storeName}"); } }
/// <summary> /// Get a list of all customers in the model. /// </summary> /// <returns>An enumerable list of all customers. (Concretely a hashset) </returns> public IEnumerable <Store.Customer> GetCustomers() { HashSet <Store.Customer> customers = new HashSet <Store.Customer>(); //convert and check if in model foreach (DataModel.Customer customer in _context.Customers.Include(cust => cust.StoreLocationNavigation)) { //customers.Add(); Store.Customer nextcustomer = Db_StoreMapper.MapCustomerToStore(customer); if (nextcustomer != null) { customers.Add(nextcustomer); } } //return list return(customers); }
/// <summary> /// Get a list of all the Orders. /// </summary> /// <returns>A list of all the orders</returns> public IEnumerable <IOrder> GetAllOrders() { IEnumerable <DataModel.Order> orders = _context.Orders .Include(order => order.Customer) .Include(order => order.OrderItems) .ThenInclude(oi => oi.Item) //manafest the query here so that more queries can be made ass needed. .ToList(); foreach (Order Order_DB in orders) { bool foundEquiv = HasEquivilentOrder(Order_DB); if (!foundEquiv) { this.checkForModelMissingOrderData(Order_DB); Db_StoreMapper.MapAndAddOrderToModel(Order_DB); } } return(Store.Orders.Instance.GetAllOrders()); }