public IEnumerable <UserBookLoanDTO> GetUserBookLoans(int userId) { var user = _repo.getUserById(userId); if (user == null) { throw new UserNotFoundException(); } var bookLoans = _repo.GetBookLoansByUserId(userId); var result = new List <UserBookLoanDTO>(); foreach (BookLoan bookLoan in bookLoans) { if (bookLoan.DateOfReturn == null) { var bl = new UserBookLoanDTO { BookLoanId = bookLoan.BookLoanId, BookId = _bookRepo.getBookById(bookLoan.BookId).BookId, BookTitle = _bookRepo.getBookById(bookLoan.BookId).Title, DateOfLoan = bookLoan.DateOfLoan, DateOfReturn = bookLoan.DateOfReturn }; result.Add(bl); } } return(result); }
public UserDetailDTO GetUserById(int id) { var user = _repo.getUserById(id); if (user == null) { throw new UserNotFoundException(); } var loansByUser = _repo.GetBookLoansByUserId(id); var loanHistory = new List <UserBookLoanDTO>(); foreach (BookLoan loan in loansByUser) { var book = _bookRepo.getBookById(loan.BookId); var l = new UserBookLoanDTO { BookId = book.BookId, BookTitle = book.AuthorFirst + " " + book.AuthorLast, DateOfLoan = loan.DateOfLoan, DateOfReturn = loan.DateOfReturn }; loanHistory.Add(l); } var result = new UserDetailDTO { UserId = user.UserId, FirstName = user.FirstName, LastName = user.LastName, Address = user.Address, Email = user.Email, LoanHistory = loanHistory }; return(result); }
public UserBookLoanDTO GetUserBookLoanByBookId(int userId, int bookId) { if (_repo.getUserById(userId) == null) { throw new UserNotFoundException(); } if (_bookRepo.getBookById(bookId) == null) { throw new BookNotFoundException(); } var bookLoan = new BookLoan(); try { _repo.GetBookLoan(userId, bookId); } catch (BookLoanNotFoundException e) { Console.WriteLine(e); throw e; } bookLoan = _repo.GetBookLoan(userId, bookId); if (bookLoan == null) { throw new BookLoanNotFoundException(); } var result = new UserBookLoanDTO { BookLoanId = bookLoan.BookLoanId, BookId = _bookRepo.getBookById(bookLoan.BookId).BookId, BookTitle = _bookRepo.getBookById(bookLoan.BookId).Title, DateOfLoan = bookLoan.DateOfLoan, DateOfReturn = bookLoan.DateOfReturn }; return(result); }