public BookDetailDTO getBookById(int id) { var book = _bookRepo.getBookById(id); if (book == null) { throw new BookNotFoundException(); } var loansByBook = _bookRepo.getLoanHistoryByBook(book); var loanHistory = new List <BookUserLoanDTO>(); foreach (BookLoan loan in loansByBook) { var user = _userRepo.getUserById(loan.UserId); var l = new BookUserLoanDTO { Username = user.FirstName + " " + user.LastName, DateOfLoan = loan.DateOfLoan, DateOfReturn = loan.DateOfReturn }; loanHistory.Add(l); } var result = new BookDetailDTO { Title = book.Title, BookId = book.BookId, AuthorFirst = book.AuthorFirst, AuthorLast = book.AuthorLast, DateOfIssue = book.DateOfIssue, ISBNNumber = book.ISBNNumber, Rating = (double)book.RatingSum / (double)book.TotalRatings, LoanHistory = loanHistory }; 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 UserReviewDTO AddReview(int userId, int bookId, BookReviewViewModel newReview) { var user = _userRepo.getUserById(userId); var book = _bookRepo.getBookById(bookId); if (user == null) { throw new UserNotFoundException(); } if (book == null) { throw new BookNotFoundException(); } try { GetUserReview(userId, bookId); throw new AlreadyReviewedByUserException(); } catch (ReviewNotFoundException e) { // Good thing, there is no review :p! } var review = new BookReview { UserId = userId, BookId = bookId, DateOfReview = DateTime.Now, Rating = newReview.Rating, Review = newReview.Review }; _bookRepo.SetBookRating(review.Rating, bookId); var result = _repo.AddUserReview(review); return(new UserReviewDTO { BookReviewId = result.BookReviewId, BookTitle = book.Title, DateOfReview = result.DateOfReview, Rating = result.Rating, Review = result.Review }); }