using System.Text; using Books; using Spectre.Console; Console.OutputEncoding = Encoding.UTF8; const string CsvChoice = "csv"; const string WebChoice = "web"; const string AuthorChoice = "author"; const string PublisherChoice = "publisher"; const string YearChoice = "year"; const string AscChoice = "asc"; const string DescChoice = "desc"; AnsiConsole.Write(new FigletText("BOOKS") .Centered() .Color(Color.Red)); string source = AnsiConsole.Prompt(new SelectionPrompt() .Title("Please choose the source of books") .AddChoices(CsvChoice, WebChoice)); string filter = AnsiConsole.Prompt(new SelectionPrompt() .Title("Please choose the filter type") .AddChoices(AuthorChoice, PublisherChoice, YearChoice)); string? filterValue = null; int filterYear = -1; string order = AscChoice; if (filter == YearChoice) { do { filterYear = AnsiConsole.Ask("Please enter the year (4 digits, 0-2100):"); } while (filterYear is < 0 or > 2100); } else { filterValue = AnsiConsole.Ask($"Please enter the {filter}:"); order = AnsiConsole.Prompt(new SelectionPrompt() .Title("Please choose the order") .AddChoices(AscChoice, DescChoice)); } var bookStore = new BookStore(source == CsvChoice ? LoaderType.Csv : LoaderType.Web, true); bool descending = order == DescChoice; IReadOnlyCollection results = filter switch { AuthorChoice when filterValue is not null => await bookStore .GetBooksByAuthorAsync(filterValue, descending), PublisherChoice when filterValue is not null => await bookStore .GetBooksByPublisherAsync(filterValue, descending), YearChoice => await bookStore.GetBooksByYearAsync(filterYear), _ => throw new ArgumentOutOfRangeException(nameof(filter)) }; var table = new Table() .AddColumn("Title") .AddColumn("Author") .AddColumn("Publisher") .AddColumn("Year") .AddColumn("ISBN"); foreach (var book in results) { table.AddRow(book.Title, book.Author, book.Publisher, book.Year.ToString(), book.ISBN); } AnsiConsole.Write(table); Console.ReadKey();