/// <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() { //get all customers from DB using MyStoreDbContext context = this.ConnectToDB(); HashSet <Store.Customer> customers = new HashSet <Store.Customer>(); //convert and check if in model foreach (Customer customer in context.Customers) { Name cname = this.getCustomerName(customer); // if not, add to model if (!Customers.Instance.HasCustomer(cname)) { Store.Customer NewCustomer = Store.Customers.Instance.RegisterCustomer(cname, customer.StoreLocation); } else { customers.Add(Store.Customers.Instance.GetCustomer(cname)); } } //return list return(customers); }
//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)); }
private static void Setup() { StoreCatalogue.Instance.RegisterItem("Item1", 1); StoreCatalogue.Instance.RegisterItem("Item2", 2); StoreCatalogue.Instance.RegisterItem("Item3", 3); StoreCatalogue.Instance.RegisterItem("Item4", 4); StoreCatalogue.Instance.RegisterItem("Item5", 5); Locations.Instance.RegisterLocation("Store1"); Locations.Instance.GetLocation("Store1").AddInventory("Item1", 50); Locations.Instance.GetLocation("Store1").AddInventory("Item2", 1); Locations.Instance.GetLocation("Store1").AddInventory("Item3", 10); Locations.Instance.RegisterLocation("Store2"); Locations.Instance.GetLocation("Store2").AddInventory("Item4", 50); Locations.Instance.GetLocation("Store2").AddInventory("Item5", 60); Locations.Instance.GetLocation("Store2").AddInventory("Item2", 25); Store.Customer c = Customers.Instance.RegisterCustomer("Daniel", "last", 'm'); Customers.Instance.RegisterCustomer("Randel", "last", 'n'); Customers.Instance.RegisterCustomer("Daniel", "last"); Console.WriteLine("Creating first order"); Store.Order o = Locations.Instance.GetLocation("Store1").CreateNewOrder("Item1", 10, c); o.FinallizeOrder(); o = Locations.Instance.GetLocation("Store2").CreateNewOrder("Item5", 1, c); o.EditOrderAmounts("Item2", 5); o.FinallizeOrder(); }
public EditOrderMenu(IDbRepository repo, Store.Customer currentCustomer, Store.Location selectedStore, Store.Order currentOrder) { Repo = repo; this.currentCustomer = currentCustomer; this.selectedStore = selectedStore; this.currentOrder = currentOrder; }
/// <summary> /// Takes a model customer, creates a DB customer, and sends it to the database. Will register /// the customer with the data model if the customer isn't already. /// </summary> /// <remarks> /// May throw exceptions if the store name is over 100 characters, or doesn't exist in the DB. /// </remarks> /// <param name="customer">The model customer.</param> public void CreateCustomer(Store.Customer customer) { using MyStoreDbContext DBContext = new MyStoreDbContext(dbContextOptions); if (!Customers.Instance.HasCustomer(customer.CustomerName)) { Customers.Instance.RegisterCustomer(customer); } DataModel.Customer newcustomer = new Customer(); newcustomer.Id = (DBContext.Customers.OrderByDescending(cust => cust.Id).Take(1).First().Id) + 1; newcustomer.LastName = customer.CustomerName.Last; newcustomer.FirstName = customer.CustomerName.First; if (customer.CustomerName.MiddleInitial != null) { newcustomer.MiddleInitial = customer.CustomerName.MiddleInitial.ToString(); } if (customer.DefaultStore != null) { newcustomer.StoreLocation = customer.DefaultStore.LocationName; } DBContext.Customers.Add(newcustomer); DBContext.SaveChanges(); }
/// <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() { using MyStoreDbContext context = ConnectToDB(); //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(getCustomerName(c), c.StoreLocation); /* * if(c.StoreLocation != null) * { * newcust.SetDefaultStore(Store.Locations.Instance.GetLocation(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, 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); } }
public static CustomerViewModel MapCustomerToView(Store.Customer StoreCust) { CustomerViewModel customerViewModel = new CustomerViewModel(); customerViewModel.FirstName = StoreCust.CustomerName.First; customerViewModel.MiddleInitial = StoreCust.CustomerName.MiddleInitial; customerViewModel.LastName = StoreCust.CustomerName.Last; customerViewModel.Name = StoreCust.CustomerName.ToString(); customerViewModel.NumOrders = StoreCust.CustomerOrderHistory.Count(); customerViewModel.HomeStore = StoreCust.DefaultStore?.LocationName ?? "None"; customerViewModel.orders = new List <OrderViewModel>(); foreach (var order in StoreCust.CustomerOrderHistory) { customerViewModel.orders.Add(MapOrderToViewModel(order)); } return(customerViewModel); }
/// <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); }
//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) { MyStoreDbContext dBContext = this.ConnectToDB(); Customer customer = GetDBCustomerByName(dBContext, c.CustomerName); customer = dBContext.Customers .Where(cust => cust.Id == customer.Id) .Include(cust => cust.Orders) .ThenInclude(ord => ord.OrderItems) .ThenInclude(ordi => ordi.Item) .FirstOrDefault(); IEnumerable <IOrder> orders = Store.Orders.Instance.GetOrdersByCustomer(c); foreach (Order CustomerOrder_DB in customer.Orders) { bool foundEquiv = false; foreach (Store.IOrder CustomerOrder_MD in orders) { if (EquivilentOrder(CustomerOrder_MD, CustomerOrder_DB)) { foundEquiv = true; break; } } if (!foundEquiv) { ICollection <ItemCount> orderitems = new List <ItemCount>(); foreach (OrderItem oi in CustomerOrder_DB.OrderItems) { orderitems.Add(new ItemCount(oi.Quantity, oi.Item.ItemName)); } Store.Orders.Instance.CreateAndAddPastOrder(CustomerOrder_DB.StoreLocation, this.getCustomerName(CustomerOrder_DB.Customer), CustomerOrder_DB.OrderTime, orderitems, CustomerOrder_DB.OrderTotal, CustomerOrder_DB.Id); } } orders = Store.Orders.Instance.GetOrdersByCustomer(c); return(orders); }
/// <summary> /// Takes a model customer, creates a DB customer, and sends it to the database. Will register /// the customer with the data model if the customer isn't already. /// </summary> /// <remarks> /// May throw exceptions if the store name is over 100 characters, or doesn't exist in the DB. /// </remarks> /// <param name="customer">The model customer.</param> public void CreateCustomer(Store.Customer customer) { DataModel.Customer newcustomer = new Customer(); newcustomer.Id = (_context.Customers.OrderByDescending(cust => cust.Id).Take(1).First().Id) + 1; newcustomer.LastName = customer.CustomerName.Last; newcustomer.FirstName = customer.CustomerName.First; if (customer.CustomerName.MiddleInitial != null) { newcustomer.MiddleInitial = customer.CustomerName.MiddleInitial.ToString(); } if (customer.DefaultStore != null) { newcustomer.StoreLocation = customer.DefaultStore.LocationName; } _context.Customers.Add(newcustomer); _context.SaveChanges(); }
// GET: Customer/Details/5 // view order history and other details public ActionResult Details([FromServices] IDbRepository repo, string customerName) { if (string.IsNullOrWhiteSpace(customerName)) { //todo: set error thing to display on the view, could not find customer ModelState.TryAddModelError("customerName", "Invalid name given, it's null, or just space"); _logger.Log(LogLevel.Error, "Invalid name given, it's null, or just space"); return(RedirectToAction(nameof(Choose))); } else { Store.Customer c = repo.GetCustomerByName(new Name(customerName)); /* get the order history from the repo into the model * incase a new one has been placed since the model was loaded. * Utilized in the mapper to fill in the object. */ repo.GetOrderHistory(c); CustomerViewModel customer = StoreToViewMapper.MapCustomerToView(c); return(View(customer)); } }
public ViewStoreMenu(DataModel.IDbRepository repo, Customer currentCustomer, Location selectedStore) { Repo = repo; Customer = currentCustomer; this.selectedStore = selectedStore; }
public ActionResult Create([FromServices] IDbRepository repo, CustomerViewModel customer, object nothing = null) { if (!ModelState.IsValid) { _logger.LogWarning("Invalid model state for Create"); //ViewData["Stores"] = GetStoreNames(repo); //return View(nameof(Create), customer); } Store.Location homestore = null; if (customer.HomeStore != "None") { Console.WriteLine(customer.HomeStore); try { homestore = repo.GetLocation(customer.HomeStore); } catch (LocationNotFoundException e) { Console.Error.WriteLine(e.Message); ModelState.AddModelError("HomeStore", "Location does not exist."); _logger.LogError($"Location, {customer.HomeStore} does not exist in model."); return(View(nameof(Create), customer)); } } Name custname = new Name(customer.FirstName, customer.LastName, customer.MiddleInitial); Console.WriteLine(custname.ToString()); if (Customers.Instance.HasCustomer(custname)) { // Invalid name, go back ModelState.AddModelError("FirstName", "Name Already Exists."); ModelState.AddModelError("LastName", "Name Already Exists."); ModelState.AddModelError("MiddleInitial", "Name Already Exists."); _logger.LogError($"Customer, {custname}, Already Exists."); ViewData["Stores"] = GetStoreNames(repo); return(View(nameof(Create), customer)); } else { try { Store.Customer newcustomer = Customers.Instance.RegisterCustomer(custname, homestore); repo.CreateCustomer(newcustomer); } catch (Exception e) { Console.Error.WriteLine(e.Message); ModelState.AddModelError("FirstName", "Error creating customer."); _logger.LogError($"Error creating customer w/ DB, {custname}."); ViewData["Stores"] = GetStoreNames(repo); return(View(nameof(Create), customer)); } return(RedirectToAction("Choose")); } }