/// <summary> /// Create this menu. /// </summary> /// <param name="appState">Global application state.</param> /// <returns>This menu.</returns> public Main(ApplicationData.State appState) : base(appState) { this.AddListMenuOption("User", ConsoleKey.D1, () => new StoreCliMenuUser.Landing(this.ApplicationState)); this.AddListMenuOption("Administrator", ConsoleKey.D2, () => new StoreCliMenuAdmin.Main(this.ApplicationState)); // This establishes a new database connection and will raise an exception if // there are any problems. We do this so we can detect any connection problems // immediately, and also so there is an active connection for later queries // (the user will not have to wait for a connection except on initial load). using (var db = new StoreDb.StoreContext(appState.DbOptions)){ db.Locations.FirstOrDefault(); } }
/// <summary> /// Print this menu. /// </summary> public void PrintMenu() { Console.Clear(); CliPrinter.Title("Find Customer"); this.DisplaySearchTerm(this.SearchQuery); Console.SetCursorPosition(0, SearchResultTerminalRow); DisplaySearchTerm(this.SearchQuery); ClearResults(); Console.SetCursorPosition(0, SearchResultTerminalRow); Console.WriteLine("============================="); if (this.SearchQuery.Length == 0) { return; } var maxResults = Console.WindowHeight - SearchResultTerminalRow - 4; using (var db = new StoreDb.StoreContext(this.ApplicationState.DbOptions)) { var customers = db.FindCustomerByName(this.SearchQuery); var displayAmount = customers.Count() > maxResults ? maxResults : customers.Count(); Console.Write($"{customers.Count()} customers found. Displaying {displayAmount}.\n\n"); foreach (var customer in customers.Take(displayAmount).OrderBy(c => c.FirstName)) { var phoneNumber = customer.PhoneNumber; if (phoneNumber == null || phoneNumber == "") { phoneNumber = " "; } Console.WriteLine($"{phoneNumber}: {customer.FirstName} {customer.LastName}"); } } }