// Edit a Borrower // Ability to borrow a book if available public void BorrowBook(BorrowerBooksAccount BorrowerBooksAccount, bool isBorrowing) { try { // Get the list of books var books = _redisCacheProvider.GetAll <Book>(); var borrowers = _redisCacheProvider.GetAll <Borrower>(); ValidateBorrowBook(BorrowerBooksAccount, books, borrowers, isBorrowing); // create a borrowerBookAccount for new Account to be created var bbaToCreate = BorrowerBooksAccount.Map(); if (isBorrowing) { bbaToCreate.Id = _redisCacheProvider.GetNextSequenceForBorrowerBookAccount(); } _redisCacheProvider.SaveBorrowerBooksAccount(bbaToCreate); // Update the Book var book = books.FirstOrDefault(x => x.Id == bbaToCreate.BookId); if (book == null) { throw new Exception("Cannot find the book"); } book.IsAvailable = false; _redisCacheProvider.SaveBook(book); } catch (Exception ex) { throw new Exception(ex.Message); } }
private void ValidateBorrowBook(BorrowerBooksAccount borrowerBooksAccount, IEnumerable <Book> books, IEnumerable <Borrower> borrowers, bool isBorrowing) { // Check whether the book exist in the books list, if not throw error Message var book = books.FirstOrDefault(x => x.Id == borrowerBooksAccount.BookId); if (book == null) { throw new Exception("Cannot find the Book"); } // Check whether the book is available to borrow if (!book.IsAvailable) { throw new Exception("Book is not available for borrowing"); } // Check whether the borrower is there in the list, if not throw error Message var borrower = borrowers.FirstOrDefault(x => x.Id == borrowerBooksAccount.BorrowerId); if (borrower == null) { throw new Exception("Cannot find the Borrower"); } if (isBorrowing) { if (borrowerBooksAccount.DateBorrowed < DateTime.Today) { throw new Exception("Cannot set date borrowed in the past"); } } }
public BorrowerBooksAccount SaveBorrowerBooksAccount(BorrowerBooksAccount borrowerBooksAccount) { BorrowerBooksAccount result; using (RedisClient client = new RedisClient()) { var wrapper = client.As <BorrowerBooksAccount>(); result = wrapper.Store(borrowerBooksAccount); } return(result); }
public static BorrowerBooksAccount Map(this BorrowerBooksAccount borrowerBooksAccount) { if (borrowerBooksAccount == null) { return(null); } return(new BorrowerBooksAccount { BookId = borrowerBooksAccount.BookId, BorrowerId = borrowerBooksAccount.BorrowerId, DateBorrowed = borrowerBooksAccount.DateBorrowed, DateReturned = borrowerBooksAccount.DateReturned }); }
public void PostBorrowBook([FromBody] BorrowerBooksAccount bba) { try { var bookBorrower = new BorrowerBooksAccount { Id = 0, BookId = bba.BookId, BorrowerId = bba.BorrowerId, DateBorrowed = DateTime.Now, DateReturned = null }; _libraryService.BorrowBook(bookBorrower, true); } catch (Exception ex) { throw new Exception(ex.Message); } }