/// <summary>
        /// Handle menu input.
        /// </summary>
        public void InputLoop()
        {
            Console.WriteLine("Select store for orders:");

            var numLocations = 0;
            IEnumerable <Guid> locationIds = null;

            using (var db = new StoreContext(this.ApplicationState.DbOptions))
            {
                var locations     = db.GetLocations();
                var locationsIter = Enumerable.Range(1, locations.Count()).Zip(locations);
                foreach (var location in locationsIter)
                {
                    Console.WriteLine($"  {location.First}. {location.Second.Name}");
                }
                numLocations = locationsIter.Count();
                locationIds  = locationsIter.Select(i => i.Second.LocationId).ToList();
            }

            bool validator(int num)
            {
                if (num > 0 && num <= numLocations)
                {
                    return(true);
                }
                else
                {
                    CliPrinter.Error($"Please enter a store number between 1 and {numLocations}");
                    return(false);
                }
            }

            var locationSelected = CliInput.GetInt(0, validator, "Enter store number:");

            var customerId = this.ApplicationState.UserData.CustomerId;
            var locationId = locationIds.ElementAt(locationSelected - 1 ?? 0);

            if (this.ApplicationState.DbOptions.SetDefaultLocation(customerId, locationId))
            {
                CliInput.PressAnyKey("Store set.");
                this.ApplicationState.UserData.OperatingLocationId = locationId;
                this.ApplicationState.UserData.RefreshDefaultLocation();
                this.MenuExit();
                return;
            }
            else
            {
                CliInput.PressAnyKey("An error occurred while setting the store. Please try again.");
            }
        }
Пример #2
0
        /// <summary>
        /// Handle input.
        /// </summary>
        public void InputLoop()
        {
            do
            {
                PrintMenu();
                using (var db = new StoreContext(this.ApplicationState.DbOptions))
                {
                    var location = db.GetLocationById(this.ApplicationState.UserData.OperatingLocationId);
                    var order    = db.FindCurrentOrder(location, this.ApplicationState.UserData.CustomerId);
                    if (order == null)
                    {
                        CliPrinter.Error("There was an error retrieving your order. Please try again.");
                        break;
                    }

                    bool itemNumberValidator(int num)
                    {
                        if (num > 0 && num <= this.InventoryIds.Count)
                        {
                            return(true);
                        }
                        else
                        {
                            CliPrinter.Error($"Please enter an inventory number between 1 and {this.InventoryIds.Count}");
                            return(false);
                        }
                    }

                    var itemIndex = CliInput.GetInt(CliInput.GetIntOptions.AllowEmpty, itemNumberValidator, "Inventory number: ") ?? 0;
                    if (itemIndex == 0)
                    {
                        break;
                    }

                    var inventoryId = this.InventoryIds[itemIndex - 1];
                    var product     = db.GetProductFromInventoryId(inventoryId);

                    var projectedQuantity = db.ProjectStockBasedOnOrder(order, product);

                    if (projectedQuantity <= 0)
                    {
                        CliInput.PressAnyKey("That item is out of stock");
                        continue;
                    }

                    var orderQuantity = CliInput.GetInt(CliInput.GetIntOptions.AllowEmpty,
                                                        v => v > 0 && v <= projectedQuantity,
                                                        $"Quantity [1..{projectedQuantity}]: ") ?? 0;
                    if (orderQuantity == 0)
                    {
                        continue;
                    }

                    db.AddLineItem(order, product, orderQuantity);
                    db.SaveChanges();

                    CliInput.PressAnyKey("\nAdded to order.");
                }
            } while (true);
            this.MenuExit();
        }
        /// <summary>
        /// Handle input.
        /// </summary>
        public void InputLoop()
        {
            do
            {
                this.PrintMenu();
                if (this.ApplicationState.UserData.CurrentOrderId == null)
                {
                    CliInput.PressAnyKey("There is currently no open order.");
                    break;
                }
                using (var db = new StoreContext(this.ApplicationState.DbOptions))
                {
                    var order = db.GetOrderById(this.ApplicationState.UserData.CurrentOrderId);
                    if (order == null)
                    {
                        break;
                    }
                    if (order.OrderLineItems.Count() == 0)
                    {
                        CliInput.PressAnyKey("There are no items in your order.");
                        break;
                    }
                }

                var getLineOptions = CliInput.GetLineOptions.AcceptEmpty | CliInput.GetLineOptions.TrimSpaces;
                var option         = CliInput.GetLine(getLineOptions, v => true, "[D]elete / Change [Q]uantity: ");
                switch (option)
                {
                case null:
                case "":
                    this.MenuExit();
                    return;

                case "D":
                case "d":
                {
                    var itemNumber = CliInput.GetInt(CliInput.GetIntOptions.AllowEmpty, n => n > 0 && n <= this.LineItemIds.Count, "Select item number:");
                    if (itemNumber == null)
                    {
                        continue;
                    }

                    using (var db = new StoreContext(this.ApplicationState.DbOptions))
                    {
                        var deleted = db.DeleteLineItem(this.LineItemIds[(int)itemNumber - 1]);
                        if (!deleted)
                        {
                            CliInput.PressAnyKey("There was a problem deleting that line item. Please try again.");
                        }
                    }
                    break;
                }

                case "Q":
                case "q":
                {
                    var itemNumber = CliInput.GetInt(CliInput.GetIntOptions.AllowEmpty, n => n > 0 && n <= this.LineItemIds.Count, "Select item number:");
                    if (itemNumber == null)
                    {
                        continue;
                    }

                    using (var db = new StoreContext(this.ApplicationState.DbOptions))
                    {
                        var order = db.GetOrderById(this.ApplicationState.UserData.CurrentOrderId);

                        var lineItemId = this.LineItemIds[(int)itemNumber - 1];
                        var lineItem   = order.OrderLineItems.Where(li => li.OrderLineItemId == lineItemId);

                        var product = lineItem.First().Product;


                        var maxOrder = db.LocationInventories
                                       .Where(i => i.Location.LocationId == order.Location.LocationId)
                                       .Where(i => i.Product.ProductId == product.ProductId)
                                       .Select(i => i.Quantity).FirstOrDefault();

                        var newQuantity = CliInput.GetInt(CliInput.GetIntOptions.AllowEmpty,
                                                          q => q >= 0 && q <= maxOrder,
                                                          $"New quantity [{maxOrder} max]:");

                        if (newQuantity == null)
                        {
                            continue;
                        }

                        var adjusted = db.SetLineItemQuantity(this.LineItemIds[(int)itemNumber - 1], (int)newQuantity);
                        if (!adjusted)
                        {
                            CliInput.PressAnyKey("There was a problem adjusting the quantity for that line item. Please try again.");
                        }
                    }
                    break;
                }
                }
            } while (true);
            this.MenuExit();
        }