From 9efcd45a2cfef947de7e7f50652331c23e45ec58 Mon Sep 17 00:00:00 2001 From: Alexander Dudkin Date: Tue, 24 Sep 2024 20:46:58 +0300 Subject: [PATCH] fix: change repository typo method name to findByBookId --- .../library/service/LibraryServiceImpl.java | 6 ++-- .../loans/repository/LoanRepository.java | 2 +- .../loans/service/LoanServiceImpl.java | 31 ++++++++++++------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/library-api-library-service/src/main/java/dev/earlspilner/library/service/LibraryServiceImpl.java b/library-api-library-service/src/main/java/dev/earlspilner/library/service/LibraryServiceImpl.java index 38d06ed..5790732 100644 --- a/library-api-library-service/src/main/java/dev/earlspilner/library/service/LibraryServiceImpl.java +++ b/library-api-library-service/src/main/java/dev/earlspilner/library/service/LibraryServiceImpl.java @@ -39,11 +39,11 @@ public BookRecordDto getBookRecord(Integer bookId) { @Override public BookRecordDto updateBookRecord(Integer bookId, BookRecordDto dto) { if (dto.status() == null) { - throw new IllegalArgumentException("Book record status is not configured for this operation"); + throw new IllegalArgumentException("The book status is not set up for this operation."); } - BookRecord bookRecord = bookRecordRepository.findById(bookId) - .orElseThrow(() -> new BookRecordNotFoundException("Book not found with ID: " + bookId)); + BookRecord bookRecord = bookRecordRepository.findByBookId(bookId) + .orElseThrow(() -> new BookRecordNotFoundException("Book record not found with ID: " + bookId)); bookRecord.setStatus(dto.status()); return bookRecordMapper.toDto(bookRecordRepository.save(bookRecord)); diff --git a/library-api-loan-service/src/main/java/dev/earlspilner/loans/repository/LoanRepository.java b/library-api-loan-service/src/main/java/dev/earlspilner/loans/repository/LoanRepository.java index df4216c..ba767a4 100644 --- a/library-api-loan-service/src/main/java/dev/earlspilner/loans/repository/LoanRepository.java +++ b/library-api-loan-service/src/main/java/dev/earlspilner/loans/repository/LoanRepository.java @@ -9,5 +9,5 @@ * @author Alexander Dudkin */ public interface LoanRepository extends JpaRepository { - Optional findByBookIdAndUserIdAndReturnedAtIsNull(Integer bookId, Integer userId); + Optional findByBookIdAndReturnedAtIsNull(Integer bookId); } diff --git a/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanServiceImpl.java b/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanServiceImpl.java index a367e3c..e26c676 100644 --- a/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanServiceImpl.java +++ b/library-api-loan-service/src/main/java/dev/earlspilner/loans/service/LoanServiceImpl.java @@ -46,34 +46,43 @@ public LoanServiceImpl(JwtTokenProvider jwtTokenProvider, UserClient userClient, public LoanDto addLoan(LoanDto dto, HttpServletRequest request) { BookRecordDto bookRecord = libraryClient.getBookRecord(dto.bookId()); if (bookRecord == null || bookRecord.status() == ON_LOAN) { - throw new UnsupportedOperationException("You can't take this book right now"); + throw new UnsupportedOperationException("The book is not available."); } UserDto userDto = userClient.getUser(jwtTokenProvider.getUsername(jwtTokenProvider.resolveToken(request))); + libraryClient.setBookStatus(dto.bookId(), new BookRecordDto(null, ON_LOAN)); + Loan loan = new Loan(userDto.id(), dto.bookId()); return loanMapper.toLoanDto(loanRepository.save(loan)); } - @Override - public LoanDto getLoan(Integer loanId) { - return loanRepository.findById(loanId) - .map(loanMapper::toLoanDto) - .orElseThrow(() -> new LoanNotFoundException("Loan not found with ID: " + loanId)); - } - @Override public LoanDto returnBook(Integer bookId, HttpServletRequest request) { BookRecordDto bookRecord = libraryClient.getBookRecord(bookId); if (bookRecord == null || bookRecord.status() == IN_LIBRARY) { - throw new IllegalArgumentException("You can't return this book right now"); + throw new IllegalArgumentException("The book is already in the library."); } UserDto userDto = userClient.getUser(jwtTokenProvider.getUsername(jwtTokenProvider.resolveToken(request))); - Loan loan = loanRepository.findByBookIdAndUserIdAndReturnedAtIsNull(bookId, userDto.id()) - .orElseThrow(() -> new LoanNotFoundException("Loan not found with bookId '" + bookId + "' and userId '" + userDto.id() + "'")); + + Loan loan = loanRepository.findByBookIdAndReturnedAtIsNull(bookId) + .orElseThrow(() -> new LoanNotFoundException("Loan not found for book with ID '" + bookId + "' and user with ID '" + userDto.id() + "'")); + + if (!loan.getUserId().equals(userDto.id())) { + throw new UnsupportedOperationException("You did not borrow this book, it was borrowed by another user."); + } + loan.setReturnedAt(Instant.now()); libraryClient.setBookStatus(bookId, new BookRecordDto(null, IN_LIBRARY)); + return loanMapper.toLoanDto(loanRepository.save(loan)); } + + @Override + public LoanDto getLoan(Integer loanId) { + return loanRepository.findById(loanId) + .map(loanMapper::toLoanDto) + .orElseThrow(() -> new LoanNotFoundException("Loan not found with ID: " + loanId)); + } }