Skip to content

Commit

Permalink
fix: change repository typo method name to findByBookId
Browse files Browse the repository at this point in the history
  • Loading branch information
thisdudkin committed Sep 24, 2024
1 parent da2811b commit 9efcd45
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@
* @author Alexander Dudkin
*/
public interface LoanRepository extends JpaRepository<Loan, Integer> {
Optional<Loan> findByBookIdAndUserIdAndReturnedAtIsNull(Integer bookId, Integer userId);
Optional<Loan> findByBookIdAndReturnedAtIsNull(Integer bookId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 9efcd45

Please sign in to comment.