Esempio n. 1
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();
        }