/*
         * public IEnumerable<ItemCount> GetStoreStocks(Store.Location l)
         * {
         *  IEnumerable<ItemCount> ModdelStock = l.GetAllStock();
         *  //may have uncommitted changes in an asynchronus enviorment
         *  //no way to reconsile stock changes, and there shouldn't be any discrepency ...
         *  //so just returning
         *  return ModdelStock;
         * }
         */


        /// <summary>
        /// Get a specific store from the db.
        /// </summary>
        /// <param name="storeName"></param>
        /// <returns></returns>
        Store.Location IDbRepository.GetLocation(string storeName)
        {
            using MyStoreDbContext context = ConnectToDB();
            Location store = context.Locations
                             .Where(str => str.LocationName == storeName)
                             .Include(str => str.Invintories)
                             .FirstOrDefault();

            if (store != null)
            {
                if (Locations.Instance.HasLocation(storeName))
                {
                    return(Locations.Instance.GetLocation(storeName));
                }
                else
                {
                    //create location
                    Store.Location newLocation = Locations.Instance.RegisterLocation(storeName);
                    //add invintory
                    foreach (Invintory inv in store.Invintories)
                    {
                        newLocation.AddInventory(inv.ItemName, inv.Quantity);
                    }
                    return(newLocation);
                }
            }
            else
            {
                return(null);
            }
        }
예제 #2
0
        public IMenu DisplayMenu()
        {
            Customer Current      = null;
            bool     gotValidName = false;
            Name     customerName;


            do
            {
                customerName = FindCustomer.GetName();
                try
                {
                    Store.Location defloc = null;
                    Current = Customers.Instance.RegisterCustomer(customerName, defloc);
                    Repo.CreateCustomer(Current);
                    gotValidName = true;
                }
                catch (CustomerAlreadyExistsException e)
                {
                    gotValidName = false;
                    Current      = null;
                    Console.WriteLine(e.Message);
                    Console.WriteLine("Names must be unique, that one isn't. Try again please.");
                }
            } while (!gotValidName);

            return(new LoggedInMenu(Repo, Current));
        }
예제 #3
0
        //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));
        }
 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>
        /// Get a list of all locations from the DB, and updates the model if any are missing.
        /// </summary>
        /// <returns></returns>
        IEnumerable <Store.Location> IDbRepository.GetLocations()
        {
            //get all customers from DB
            using MyStoreDbContext context = this.ConnectToDB();

            HashSet <Store.Location> locations = new HashSet <Store.Location>();

            //convert and check if in model
            foreach (Location l in context.Locations)
            {
                string lname = l.LocationName;
                // if not, add to model
                if (!Locations.Instance.HasLocation(lname))
                {
                    Store.Location NewLocation = Store.Locations.Instance.RegisterLocation(lname);
                    //get invintory
                    AddStockToModel(context, l);
                    locations.Add(NewLocation);
                }
                else
                {
                    locations.Add(Store.Locations.Instance.GetLocation(lname));
                }
            }

            //return list
            return(locations);
        }
예제 #6
0
 /// <summary>
 /// Use the location to get the invintory added to the model.
 /// </summary>
 /// <remarks>
 /// ONLY for use when initializing a store
 /// </remarks>
 /// <param name="location">The location to fetch and add all stocks to</param>
 private void AddStockToModel(MyStoreDbContext context, Location location)
 {
     Store.Location modelLoc = Store.Locations.Instance.GetLocation(location.LocationName);
     foreach (Invintory invintory in context.Invintories
              .Where(inv => inv.StoreLocation == location.LocationName))
     {
         modelLoc.AddInventory(invintory.ItemName, invintory.Quantity);
     }
 }
        //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)
        {
            MyStoreDbContext dBContext = this.ConnectToDB();

            Location location = dBContext.Locations
                                .Where(loc => loc.LocationName == l.LocationName)
                                .Include(cust => cust.Orders)
                                .ThenInclude(cust => cust.Customer)
                                .Include(cust => cust.Orders)
                                .ThenInclude(ord => ord.OrderItems)
                                .ThenInclude(ordi => ordi.Item)
                                .FirstOrDefault();


            IEnumerable <IOrder> orders = Store.Orders.Instance.GetOrdersByLocation(l);

            foreach (Order LocationOrder_DB in location.Orders)
            {
                bool foundEquiv = false;
                foreach (Store.IOrder LocationOrder_MD in orders)
                {
                    foundEquiv = foundEquiv || EquivilentOrder(LocationOrder_MD, LocationOrder_DB);
                    if (foundEquiv)
                    {
                        break;
                    }
                }

                if (!foundEquiv)
                {
                    Console.WriteLine("no equiv found, creating order.");
                    ICollection <ItemCount> orderitems = new List <ItemCount>();
                    foreach (OrderItem oi in LocationOrder_DB.OrderItems)
                    {
                        orderitems.Add(new ItemCount(oi.Quantity, oi.Item.ItemName));
                    }

                    Name customername = getCustomerName(dBContext.Customers
                                                        .Where(dbcust => dbcust.Id == LocationOrder_DB.CustomerId).FirstOrDefault()
                                                        );

                    Store.Orders.Instance.CreateAndAddPastOrder(l.LocationName,
                                                                customername,
                                                                LocationOrder_DB.OrderTime,
                                                                orderitems,
                                                                LocationOrder_DB.OrderTotal,
                                                                LocationOrder_DB.Id);
                }
            }

            orders = Store.Orders.Instance.GetOrdersByLocation(l);
            return(orders);
        }
예제 #8
0
        /// <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);
        }
예제 #9
0
 /// <summary>
 /// Intended to be used to set a store's stock equal to current stocks in DB
 /// </summary>
 /// <param name="selectedStore">The store who's orders you want.</param>
 void IDbRepository.UpdateAndOverwriteStoreStocks(Store.Location selectedStore)
 {
     foreach (Invintory inv in _context.Invintories.Where(x => x.StoreLocation == selectedStore.LocationName))
     {
         if (inv.Quantity != selectedStore.CheckStock(inv.ItemName))
         {
             try
             {
                 selectedStore.SetItemStock(inv.ItemName, inv.Quantity);
             }
             catch (ItemNotFoundException e)
             {
                 _logger.LogWarning($"Item - {inv.ItemName} not found in order, adding and retrying");
                 addMissingItems(_context);
                 selectedStore.SetItemStock(inv.ItemName, inv.Quantity);
             }
         }
     }
 }
        public static StoreViewModel MapLocationToStore(Store.Location l)
        {
            StoreViewModel storeViewModel = new StoreViewModel();

            storeViewModel.Name            = l.LocationName;
            storeViewModel.NumItemsInStock = l.GetAllStock().ToList()
                                             .Where(item =>
                                                    item.Count > 0)
                                             .Count();

            storeViewModel.Orders = new List <OrderViewModel>();
            foreach (var order in l.LocationOrderHistory)
            {
                storeViewModel.Orders.Add(MapOrderToViewModel(order));
            }

            storeViewModel.NumOrders = storeViewModel.Orders.Count();

            return(storeViewModel);
        }
        /// <summary>
        /// Intended to be used to set a store's stock equal to current stocks in DB
        /// </summary>
        /// <param name="selectedStore"></param>
        void IDbRepository.UpdateAndOverwriteStoreStocks(Store.Location selectedStore)
        {
            using MyStoreDbContext context = this.ConnectToDB();

            foreach (Invintory inv in context.Invintories.Where(x => x.StoreLocation == selectedStore.LocationName))
            {
                if (inv.Quantity != selectedStore.CheckStock(inv.ItemName))
                {
                    try
                    {
                        selectedStore.SetItemStock(inv.ItemName, inv.Quantity);
                    }
                    catch (ItemNotFoundException e)
                    {
                        addMissingItems(context);
                        selectedStore.SetItemStock(inv.ItemName, inv.Quantity);
                    }
                }
            }
        }
예제 #12
0
        public ActionResult Stock([FromServices] IDbRepository repo, string store)
        {
            if (!string.IsNullOrWhiteSpace(store))
            {
                Store.Location            modelLocation = repo.GetLocation(store);
                List <StockItemViewModel> stocks        = new List <StockItemViewModel>();

                foreach (var item in modelLocation.GetAllStock())
                {
                    stocks.Add(StoreToViewMapper.MapStockToStockItem(item));
                }

                return(View(stocks));
            }
            else
            {
                _logger.LogError("Error: bad store name given.");
                ModelState.TryAddModelError("BadStore", "Error: bad store name given.");
                return(View(nameof(Stores), LoadandRetrieveStoreData(repo)));
            }
        }
        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"));
            }
        }