public Loan LoanABook(long memberId, long bookId) { using (ILoanUnitOfWork uow = _uowFactory.GetLoanUnitOfWork()) { Book bookToLoan = uow.FindBook(bookId); if (bookToLoan == null) { throw new InvalidOperationException("The book does not exist."); } if (bookToLoan.Status == BookStatus.OnLoan) { throw new InvalidOperationException("The book is already on loan."); } if (HasMemberBorrowedMaxNumberOfBooks(uow, memberId)) { throw new InvalidOperationException("You have borrowed the maximum number of books allowed."); } Loan newLoan = CreateNewLoanRecord(uow, memberId, bookToLoan.BookId); bookToLoan.Status = BookStatus.OnLoan; uow.Save(); return(newLoan); } }
public IEnumerable <Book> GetBooks(string title) { using (ILoanUnitOfWork uow = _uowFactory.GetLoanUnitOfWork()) { return(uow.GetBooks(b => b.Title == title)); } }
private Loan CreateNewLoanRecord(ILoanUnitOfWork uow, long memberId, long bookId) { var newLoan = new Loan { MemberId = memberId, BookId = bookId, Status = LoanStatus.Open, LoanDate = DateTime.UtcNow.Date, ExpectedReturnDate = DateTime.UtcNow.Date.AddDays(LoanDuration) }; uow.InsertLoanRecord(newLoan); return(newLoan); }
private bool HasMemberBorrowedMaxNumberOfBooks(ILoanUnitOfWork uow, long memberId) { int numberOfBooksOnLoan = uow.GetLoans(loan => loan.MemberId == memberId && loan.Status == LoanStatus.Open).Count(); return(numberOfBooksOnLoan == MaxNumberOfBook); }