ex-ex-02-books/BooksApp/Books/Program.cs
github-classroom[bot] 49d2d70965
Initial commit
2025-06-03 15:21:16 +00:00

70 lines
No EOL
2.6 KiB
C#

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<string>()
.Title("Please choose the source of books")
.AddChoices(CsvChoice, WebChoice));
string filter = AnsiConsole.Prompt(new SelectionPrompt<string>()
.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<int>("Please enter the year (4 digits, 0-2100):");
} while (filterYear is < 0 or > 2100);
}
else
{
filterValue = AnsiConsole.Ask<string>($"Please enter the {filter}:");
order = AnsiConsole.Prompt(new SelectionPrompt<string>()
.Title("Please choose the order")
.AddChoices(AscChoice, DescChoice));
}
var bookStore = new BookStore(source == CsvChoice ? LoaderType.Csv : LoaderType.Web, true);
bool descending = order == DescChoice;
IReadOnlyCollection<Book> 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();