/// <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>
        /// Print this menu.
        /// </summary>
        public void PrintMenu()
        {
            Console.Clear();
            CliPrinter.Title("Order History");
            switch (this.CurrentOperatingMode)
            {
            case OperatingMode.SelectLocation:
                using (var db = new StoreContext(this.ApplicationState.DbOptions))
                {
                    var locations = db.GetLocations();
                    this.LocationIds.Clear();
                    var i = 1;
                    foreach (var location in locations)
                    {
                        this.LocationIds.Add(location.LocationId);
                        Console.WriteLine($"{i}.  {location.Name}");
                        i += 1;
                    }
                    Console.WriteLine("\n");
                }
                break;

            case OperatingMode.ViewOrders:
                using (var db = new StoreContext(this.ApplicationState.DbOptions))
                {
                    var location = db.GetLocationById(this.SelectedLocation);
                    if (location == null)
                    {
                        break;
                    }

                    var orders = db
                                 .GetOrderHistory(location)
                                 .Select(o => new {
                        OrderId       = o.OrderId,
                        Customer      = o.Customer,
                        Location      = o.Location,
                        TimeCreated   = o.TimeCreated,
                        TimeSubmitted = o.TimeSubmitted,
                        TimeFulfilled = o.TimeFulfilled,
                        AmountPaid    = o.AmountPaid,
                        OrderLineItem = o.OrderLineItems,
                        AmountCharged = db.GetAmountCharged(o),
                    }).ToList();

                    if (orders.Count() == 0)
                    {
                        CliPrinter.Error("There are no orders for this location.");
                        this.CurrentOperatingMode = OperatingMode.SelectLocation;
                        CliInput.PressAnyKey();
                        this.PrintMenu();
                        return;
                    }

                    var upArrow   = '↑';
                    var downArrow = '↓';

                    var priceSortSymbol = '-';
                    var dateSortSymbol  = '-';

                    switch (this.SortKey)
                    {
                    case OrderSortKey.DateDesc:
                        orders         = orders.OrderByDescending(o => o.TimeSubmitted).ToList();
                        dateSortSymbol = downArrow;
                        break;

                    case OrderSortKey.DateAsc:
                        orders         = orders.OrderBy(o => o.TimeSubmitted).ToList();
                        dateSortSymbol = upArrow;
                        break;

                    case OrderSortKey.PriceDesc:
                        orders          = orders.OrderByDescending(o => o.AmountCharged).ToList();
                        priceSortSymbol = downArrow;
                        break;

                    case OrderSortKey.PriceAsc:
                        orders          = orders.OrderBy(o => o.AmountCharged).ToList();
                        priceSortSymbol = upArrow;
                        break;
                    }
                    var historyDisplayAlignment = "{0,-6}{1,-9}{2,-25}";
                    var priceSortLine           = $"{priceSortSymbol}----";
                    var dateSortLine            = $"{dateSortSymbol}---";

                    Console.WriteLine(historyDisplayAlignment, "Num", "Price", "Date");
                    Console.WriteLine(historyDisplayAlignment, "---", priceSortLine, dateSortLine);
                    var i = 1;
                    this.OrderIds.Clear();
                    foreach (var order in orders)
                    {
                        this.OrderIds.Add(order.OrderId);
                        Console.WriteLine(historyDisplayAlignment,
                                          i + ".", "$" + order.AmountCharged, order.TimeSubmitted, order.Location.Name);
                        i += 1;
                    }
                }
                break;

            default:
                break;
            }
        }