Skip to content

Commit

Permalink
Update BorrowingRepository.java
Browse files Browse the repository at this point in the history
  • Loading branch information
sanchitc05 authored Nov 5, 2024
1 parent 72ac746 commit f1d7044
Showing 1 changed file with 14 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,19 @@
@Repository
public interface BorrowingRepository extends JpaRepository<Borrowings, Integer> {

public Page<BorrowingsDto> getAllBorrowingsOfMember(int memberId, Pageable pageable) {
try {
Page<Borrowings> 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)
Expand All @@ -21,4 +34,4 @@ public interface BorrowingRepository extends JpaRepository<Borrowings, Integer>
"WHERE b.borrowDate BETWEEN :startDate AND :endDate " +
"GROUP BY FUNCTION('DATE', b.borrowDate)")
Map<String, Long> getBorrowingTrendsBetweenDates(LocalDate startDate, LocalDate endDate);
}
}

0 comments on commit f1d7044

Please sign in to comment.