/// <summary> /// Handle the input when selecting a location to query for orders. /// </summary> /// <returns>A <c>HandlerMsg</c> indicating what action should be taken.</returns> private HandlerMsg HandleSelectLocationInput() { var inputOptions = CliInput.GetLineOptions.TrimSpaces | CliInput.GetLineOptions.AcceptEmpty; do { var line = CliInput.GetLine(inputOptions, v => true, "Select location number:"); if (line == "" || line == null) { return(HandlerMsg.Exit); } try { var locationNumber = Int32.Parse(line); if (locationNumber > 0 && locationNumber <= this.LocationIds.Count) { this.SelectedLocation = this.LocationIds[locationNumber - 1]; this.CurrentOperatingMode = OperatingMode.ViewOrders; return(HandlerMsg.Continue); } } catch { CliPrinter.Error("Please enter a valid location number."); } } while (true); }
/// <summary> /// Handle input. /// </summary> public void InputLoop() { var cliOptions = CliInput.GetLineOptions.TrimSpaces | CliInput.GetLineOptions.AcceptEmpty; do { var login = CliInput.GetLine(cliOptions, CliInput.EmailValidator, "Email:"); if (login == "" || login == null) { this.MenuExit(); return; } var password = CliInput.GetPassword("Password:"******"Invalid login."); } } while (true); this.ApplicationState.UserData.RefreshDefaultLocation(); }
/// <summary> /// Handles user input. /// </summary> public void InputLoop() { do { Console.Write("Location name: "); var name = Console.ReadLine(); if (name.Trim() == "") { this.AbortThenExit("No name provided."); return; } var location = new StoreDb.Location(name); var added = this.ApplicationState.DbOptions.AddLocation(location); if (!added) { CliPrinter.Error("A location with that name already exists."); continue; } this.MenuExit(); CliInput.PressAnyKey("Location added."); break; } while (true); }
/// <summary> /// Handle the input when viewing a list of all orders for a location. /// </summary> /// <returns>A <c>HandlerMsg</c> indicating what action should be taken.</returns> private HandlerMsg HandleViewOrderInput() { var inputOptions = CliInput.GetLineOptions.TrimSpaces | CliInput.GetLineOptions.AcceptEmpty; do { var line = CliInput.GetLine(inputOptions, v => true, "\nSort by [D]ate / [P]rice\nor enter an order number to view details:"); switch (line) { case "D": case "d": this.SortKey = this.SortKey != OrderSortKey.DateDesc ? OrderSortKey.DateDesc : OrderSortKey.DateAsc; break; case "P": case "p": this.SortKey = this.SortKey != OrderSortKey.PriceDesc ? OrderSortKey.PriceDesc : OrderSortKey.PriceAsc; break; default: if (line == "" || line == null) { this.CurrentOperatingMode = OperatingMode.SelectLocation; return(HandlerMsg.Continue); } try { var orderNum = Int32.Parse(line); if (orderNum > 0 && orderNum <= this.OrderIds.Count) { using (var db = new StoreContext(this.ApplicationState.DbOptions)) { var order = db.GetOrderById(this.OrderIds[orderNum - 1]); var amountCharged = db.GetAmountCharged(order); this.DisplayDetail(db, order, amountCharged); break; } } else { CliPrinter.Error("Invalid order number"); continue; } } catch (Exception) { // We will just ignore parse errors and reprint the menu. break; } } this.PrintMenu(); } while (true); }
/// <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."); } }
/// <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 user input. /// </summary> public void InputLoop() { string EmailLoop(DbContextOptions <StoreContext> dbOptions) { var cliOptions = CliInput.GetLineOptions.TrimSpaces | CliInput.GetLineOptions.AcceptEmpty; string login = ""; do { login = CliInput.GetLine(cliOptions, CliInput.EmailValidator, "Email Address:"); if (login == "" || login == null) { return(null); } if (dbOptions.LoginExists(login)) { CliPrinter.Error("That email address is already in use."); } else { break; } } while (true); return(login); } do { var cliOptions = CliInput.GetLineOptions.TrimSpaces | CliInput.GetLineOptions.AcceptEmpty; var firstName = CliInput.GetLine(cliOptions, Customer.ValidateName, "First Name:"); if (firstName == "" || firstName == null) { this.AbortThenExit("Empty entry - exiting."); return; } var lastName = CliInput.GetLine(cliOptions, Customer.ValidateName, "Last Name:"); if (lastName == "" || lastName == null) { this.AbortThenExit("Empty entry - exiting."); return; } var login = EmailLoop(this.ApplicationState.DbOptions); if (login == "" || login == null) { this.AbortThenExit("Empty entry - exiting."); return; } var password = CliInput.GetPassword("Password:"******"") { this.AbortThenExit("Empty entry - exiting."); return; } var customer = new Customer(); // Data pre-validated above. customer.FirstName = firstName; customer.LastName = lastName; customer.Login = login; customer.Password = password; var createResult = this.ApplicationState.DbOptions.CreateUserAccount(customer); if (createResult == CreateUserAccountResult.Ok) { this.ApplicationState.UserData.CustomerId = customer.CustomerId; this.MenuExit(); this.MenuAdd(new StoreCliMenuUser.Main(this.ApplicationState)); CliInput.PressAnyKey("\nAccount created."); break; } else { CliPrinter.Error("An error occurred while creating your account. Please try again."); } } while (true); }
/// <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; } }