Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public enum CommonActions
SearchPatrons = 4,
RenewPatronMembership = 8,
ReturnLoanedBook = 16,
ExtendLoanedBook = 32
ExtendLoanedBook = 32,
SearchBooks = 64,
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Library.ApplicationCore.Entities;
using Library.ApplicationCore.Enums;
using Library.Console;
using Library.Infrastructure.Data;
using Library.Infrastructure.Data;

public class ConsoleApp
{
Expand All @@ -17,12 +19,20 @@ public class ConsoleApp
ILoanService _loanService;
IPatronService _patronService;

public ConsoleApp(ILoanService loanService, IPatronService patronService, IPatronRepository patronRepository, ILoanRepository loanRepository)
JsonData _jsonData;

public ConsoleApp(
ILoanService loanService,
IPatronService patronService,
IPatronRepository patronRepository,
ILoanRepository loanRepository,
JsonData jsonData)
{
_patronRepository = patronRepository;
_loanRepository = loanRepository;
_loanService = loanService;
_patronService = patronService;
_jsonData = jsonData;
}

public async Task Run()
Expand Down Expand Up @@ -136,6 +146,7 @@ static CommonActions ReadInputOptions(CommonActions options, out int optionNumbe
{
"q" when options.HasFlag(CommonActions.Quit) => CommonActions.Quit,
"s" when options.HasFlag(CommonActions.SearchPatrons) => CommonActions.SearchPatrons,
"b" when options.HasFlag(CommonActions.SearchBooks) => CommonActions.SearchBooks,
"m" when options.HasFlag(CommonActions.RenewPatronMembership) => CommonActions.RenewPatronMembership,
"e" when options.HasFlag(CommonActions.ExtendLoanedBook) => CommonActions.ExtendLoanedBook,
"r" when options.HasFlag(CommonActions.ReturnLoanedBook) => CommonActions.ReturnLoanedBook,
Expand Down Expand Up @@ -170,6 +181,10 @@ static void WriteInputOptions(CommonActions options)
{
Console.WriteLine(" - \"s\" for new search");
}
if (options.HasFlag(CommonActions.SearchBooks))
{
Console.WriteLine(" - \"b\" to check for book availability");
}
if (options.HasFlag(CommonActions.Quit))
{
Console.WriteLine(" - \"q\" to quit");
Expand All @@ -193,7 +208,7 @@ async Task<ConsoleState> PatronDetails()
loanNumber++;
}

CommonActions options = CommonActions.SearchPatrons | CommonActions.Quit | CommonActions.Select | CommonActions.RenewPatronMembership;
CommonActions options = CommonActions.SearchPatrons | CommonActions.Quit | CommonActions.Select | CommonActions.RenewPatronMembership | CommonActions.SearchBooks;
CommonActions action = ReadInputOptions(options, out int selectedLoanNumber);
if (action == CommonActions.Select)
{
Expand Down Expand Up @@ -225,10 +240,61 @@ async Task<ConsoleState> PatronDetails()
selectedPatronDetails = (await _patronRepository.GetPatron(selectedPatronDetails.Id))!;
return ConsoleState.PatronDetails;
}
else if (action == CommonActions.SearchBooks)
{
return await SearchBooks();
}

throw new InvalidOperationException("An input option is not handled.");
}

async Task<ConsoleState> SearchBooks()
{
string? bookTitle = null;

while (String.IsNullOrWhiteSpace(bookTitle))
{
Console.Write("Enter a book title to search for: ");
bookTitle = Console.ReadLine();
}

await _jsonData.EnsureDataLoaded();

Book? book = _jsonData.Books!
.FirstOrDefault(book =>
string.Equals(
book.Title,
bookTitle,
StringComparison.OrdinalIgnoreCase));

if (book == null)
{
Console.WriteLine("No matching book found.");
return ConsoleState.PatronDetails;
}

BookItem? bookItem = _jsonData.BookItems!
.FirstOrDefault(bookItem => bookItem.BookId == book.Id);

Loan? loan = _jsonData.Loans!
.FirstOrDefault(loan =>
loan.BookItemId == bookItem?.Id &&
loan.ReturnDate == null);

if (loan == null)
{
Console.WriteLine($"{book.Title} is available for loan");
}
else
{
Console.WriteLine(
$"{book.Title} is on loan to another patron. " +
$"The return due date is {loan.DueDate}.");
}

return ConsoleState.PatronDetails;
}

async Task<ConsoleState> LoanDetails()
{
Console.WriteLine($"Book title: {selectedLoanDetails.BookItem!.Book!.Title}");
Expand Down