diff --git a/src/main/java/com/libraryman_api/borrowing/BorrowingRepository.java b/src/main/java/com/libraryman_api/borrowing/BorrowingRepository.java index 3b0a6c2..cbabd93 100644 --- a/src/main/java/com/libraryman_api/borrowing/BorrowingRepository.java +++ b/src/main/java/com/libraryman_api/borrowing/BorrowingRepository.java @@ -11,6 +11,19 @@ @Repository public interface BorrowingRepository extends JpaRepository { +public Page getAllBorrowingsOfMember(int memberId, Pageable pageable) { +try { +Page borrowings = borrowingRepository.findByMember_memberId(memberId, pageable); + +if (borrowings.isEmpty()) { + throw new ResourceNotFoundException("Member didn't borrow any book"); +} +return borrowings.map(this::EntityToDto); +} catch (PropertyReferenceException ex) { + throw new InvalidSortFieldException("The specified 'sortBy' value is invalid."); +} +} + @Query(value = "SELECT b.title AS bookTitle, COUNT(*) AS borrowCount " + "FROM borrowings br JOIN books b ON br.book_id = b.book_id " + "GROUP BY b.book_id ORDER BY borrowCount DESC LIMIT :limit", nativeQuery = true) @@ -21,4 +34,4 @@ public interface BorrowingRepository extends JpaRepository "WHERE b.borrowDate BETWEEN :startDate AND :endDate " + "GROUP BY FUNCTION('DATE', b.borrowDate)") Map getBorrowingTrendsBetweenDates(LocalDate startDate, LocalDate endDate); -} \ No newline at end of file +}