diff --git a/.gitignore b/.gitignore index 49bad9c..1f9ac79 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ target/ .settings .springBeans .sts4-cache +/bin/ ### IntelliJ IDEA ### .idea diff --git a/src/main/java/com/libraryman_api/book/BookController.java b/src/main/java/com/libraryman_api/book/BookController.java index 778f7c4..fbacbec 100644 --- a/src/main/java/com/libraryman_api/book/BookController.java +++ b/src/main/java/com/libraryman_api/book/BookController.java @@ -2,11 +2,14 @@ import com.libraryman_api.exception.ResourceNotFoundException; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.List; - /** * REST controller for managing books in the LibraryMan application. * This controller provides endpoints for performing CRUD operations on books, @@ -21,13 +24,30 @@ public class BookController { private BookService bookService; /** - * Retrieves a list of all books in the library. + * Retrieves a paginated and sorted list of all books in the library. * - * @return a list of {@link Book} objects representing all the books in the library. + * @param pageable contains pagination information (page number, size, and sorting). + * @param sortBy (optional) the field by which to sort the results. + * @param sortDir (optional) the direction of sorting (asc or desc). Defaults to ascending. + * @return a {@link Page} of {@link Book} objects representing the books in the library. + * The results are sorted by title by default and limited to 5 books per page. */ @GetMapping - public List getAllBooks() { - return bookService.getAllBooks(); + public Page getAllBooks(@PageableDefault(page=0, size=5, sort="title") Pageable pageable, + @RequestParam(required = false) String sortBy, + @RequestParam(required = false) String sortDir) { + + // Adjust the pageable based on dynamic sorting parameters + if(sortBy!=null && !sortBy.isEmpty()) { + Sort.Direction direction= Sort.Direction.ASC; // Default direction + + if(sortDir!=null && sortDir.equalsIgnoreCase("desc")) { + direction = Sort.Direction.DESC; + } + + pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(direction, sortBy)) ; + } + return bookService.getAllBooks(pageable); } /** diff --git a/src/main/java/com/libraryman_api/book/BookService.java b/src/main/java/com/libraryman_api/book/BookService.java index 652f01a..665e83c 100644 --- a/src/main/java/com/libraryman_api/book/BookService.java +++ b/src/main/java/com/libraryman_api/book/BookService.java @@ -1,10 +1,12 @@ package com.libraryman_api.book; -import com.libraryman_api.exception.ResourceNotFoundException; +import java.util.Optional; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; -import java.util.List; -import java.util.Optional; +import com.libraryman_api.exception.ResourceNotFoundException; /** * Service class for managing books in the LibraryMan system. @@ -36,12 +38,13 @@ public BookService(BookRepository bookRepository) { } /** - * Retrieves all books from the database. + * Retrieves a paginated list of all books from the database. * - * @return a list of all books + * @param pageable the pagination information, including the page number and size + * @return a {@link Page} of {@link Book} representing all books */ - public List getAllBooks() { - return bookRepository.findAll(); + public Page getAllBooks(Pageable pageable) { + return bookRepository.findAll(pageable); } /** diff --git a/src/main/java/com/libraryman_api/borrowing/BorrowingController.java b/src/main/java/com/libraryman_api/borrowing/BorrowingController.java index e76ae53..1dddc89 100644 --- a/src/main/java/com/libraryman_api/borrowing/BorrowingController.java +++ b/src/main/java/com/libraryman_api/borrowing/BorrowingController.java @@ -1,9 +1,13 @@ package com.libraryman_api.borrowing; import com.libraryman_api.exception.ResourceNotFoundException; -import org.springframework.web.bind.annotation.*; -import java.util.List; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; +import org.springframework.web.bind.annotation.*; /** * REST controller for managing borrowings in the LibraryMan application. @@ -26,13 +30,31 @@ public BorrowingController(BorrowingService borrowingService) { } /** - * Retrieves a list of all borrowing records in the library. + * Retrieves a paginated and sorted list of all borrowing records in the library. * - * @return a list of {@link Borrowings} objects representing all borrowings. + * @param pageable contains pagination information (page number, size, and sorting). + * @param sortBy (optional) the field by which to sort the results. + * @param sortDir (optional) the direction of sorting (asc or desc). Defaults to ascending. + * @return a {@link Page} of {@link Borrowings} representing all borrowings. + * The results are sorted by borrow date by default and limited to 5 members per page. */ @GetMapping - public List getAllBorrowings() { - return borrowingService.getAllBorrowings(); + public Page getAllBorrowings(@PageableDefault(page=0, size=5, sort="borrowDate") Pageable pageable, + @RequestParam(required = false) String sortBy, + @RequestParam(required = false) String sortDir) { + + // Adjust the pageable based on dynamic sorting parameters + if(sortBy!=null && !sortBy.isEmpty()) { + Sort.Direction direction= Sort.Direction.ASC; // Default direction + + if(sortDir!=null && sortDir.equalsIgnoreCase("desc")) { + direction = Sort.Direction.DESC; + } + + pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(direction, sortBy)) ; + } + + return borrowingService.getAllBorrowings(pageable); } /** @@ -69,14 +91,33 @@ public String payFine(@PathVariable int id) { } /** - * Retrieves all borrowing records for a specific member. + * Retrieves a paginated and sorted list of all borrowing records for a specific member. * * @param memberId the ID of the member whose borrowing records are to be retrieved. - * @return a list of {@link Borrowings} objects representing the member's borrowings. + * @param pageable contains pagination information (page number, size, and sorting). + * @param sortBy (optional) the field by which to sort the results. + * @param sortDir (optional) the direction of sorting (asc or desc). Defaults to ascending. + * @return a {@link Page} of {@link Borrowings} representing all borrowings for a specific member. + * The results are sorted by borrow date by default and limited to 5 members per page. */ @GetMapping("member/{memberId}") - public List getAllBorrowingsOfAMember(@PathVariable int memberId) { - return borrowingService.getAllBorrowingsOfMember(memberId); + public Page getAllBorrowingsOfAMember(@PathVariable int memberId, + @PageableDefault(page=0, size=5, sort="borrowDate") Pageable pageable, + @RequestParam(required = false) String sortBy, + @RequestParam(required = false) String sortDir) { + + // Adjust the pageable based on dynamic sorting parameters + if(sortBy!=null && !sortBy.isEmpty()) { + Sort.Direction direction= Sort.Direction.ASC; // Default direction + + if(sortDir!=null && sortDir.equalsIgnoreCase("desc")) { + direction = Sort.Direction.DESC; + } + + pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(direction, sortBy)) ; + } + + return borrowingService.getAllBorrowingsOfMember(memberId, pageable); } /** diff --git a/src/main/java/com/libraryman_api/borrowing/BorrowingRepository.java b/src/main/java/com/libraryman_api/borrowing/BorrowingRepository.java index 5b077d3..4c1fe27 100644 --- a/src/main/java/com/libraryman_api/borrowing/BorrowingRepository.java +++ b/src/main/java/com/libraryman_api/borrowing/BorrowingRepository.java @@ -1,16 +1,17 @@ package com.libraryman_api.borrowing; +import java.util.List; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; -import java.util.Optional; - @Repository public interface BorrowingRepository extends JpaRepository { // Underscore (_) used for property traversal, navigating from Borrowings to Members entity via 'member' property - Optional> findByMember_memberId(int memberId); + Page findByMember_memberId(int memberId, Pageable pageable); List findByBook_bookId(int bookId); } diff --git a/src/main/java/com/libraryman_api/borrowing/BorrowingService.java b/src/main/java/com/libraryman_api/borrowing/BorrowingService.java index c5e427c..150510e 100644 --- a/src/main/java/com/libraryman_api/borrowing/BorrowingService.java +++ b/src/main/java/com/libraryman_api/borrowing/BorrowingService.java @@ -8,13 +8,15 @@ import com.libraryman_api.member.MemberService; import com.libraryman_api.member.Members; import com.libraryman_api.notification.NotificationService; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.time.temporal.ChronoUnit; import java.util.Calendar; import java.util.Date; -import java.util.List; import java.util.Optional; /** @@ -60,12 +62,13 @@ public BorrowingService(BorrowingRepository borrowingRepository, FineRepository } /** - * Retrieves all borrowings from the database. + * Retrieves a paginated list of all borrowing records from the database. * - * @return a list of all borrowings + * @param pageable the pagination information, including the page number and size + * @return a {@link Page} of {@link Borrowings} representing all borrowings */ - public List getAllBorrowings() { - return borrowingRepository.findAll(); + public Page getAllBorrowings(Pageable pageable) { + return borrowingRepository.findAll(pageable); } /** @@ -256,13 +259,19 @@ private BigDecimal calculateFineAmount(Borrowings borrowing) { } /** - * Retrieves all borrowings associated with a specific member. + * Retrieves a paginated list of all borrowing associated with a specific member. * * @param memberId the ID of the member whose borrowings are to be retrieved - * @return a list of borrowings associated with the specified member + * @param pageable the pagination information, including the page number and size * @throws ResourceNotFoundException if the member has not borrowed any books + * @return a {@link Page} of {@link Borrowings} representing all borrowing associated with a specific member */ - public List getAllBorrowingsOfMember(int memberId) { - return borrowingRepository.findByMember_memberId(memberId).orElseThrow(() -> new ResourceNotFoundException("Member didn't borrow any book")); + public Page getAllBorrowingsOfMember(int memberId, Pageable pageable) { + Page borrowings = borrowingRepository.findByMember_memberId(memberId, pageable); + + if (borrowings.isEmpty()) { + throw new ResourceNotFoundException("Member didn't borrow any book"); + } + return borrowings; } } diff --git a/src/main/java/com/libraryman_api/member/MemberController.java b/src/main/java/com/libraryman_api/member/MemberController.java index 246589c..f6d016f 100644 --- a/src/main/java/com/libraryman_api/member/MemberController.java +++ b/src/main/java/com/libraryman_api/member/MemberController.java @@ -1,11 +1,15 @@ package com.libraryman_api.member; import com.libraryman_api.exception.ResourceNotFoundException; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.data.domain.Sort; +import org.springframework.data.web.PageableDefault; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import java.util.List; - /** * REST controller for managing library members. * This controller provides endpoints for performing CRUD operations on members. @@ -26,13 +30,31 @@ public MemberController(MemberService memberService) { } /** - * Retrieves a list of all library members. + * Retrieves a paginated and sorted list of all library members. * - * @return a list of {@link Members} representing all members in the library + * @param pageable contains pagination information (page number, size, and sorting). + * @param sortBy (optional) the field by which to sort the results. + * @param sortDir (optional) the direction of sorting (asc or desc). Defaults to ascending. + * @return a {@link Page} of {@link Members} representing all members in the library. + * The results are sorted by name by default and limited to 5 members per page. */ @GetMapping - public List getAllMembers() { - return memberService.getAllMembers(); + public Page getAllMembers(@PageableDefault(page=0, size=5, sort="name") Pageable pageable, + @RequestParam(required = false) String sortBy, + @RequestParam(required = false) String sortDir) { + + // Adjust the pageable based on dynamic sorting parameters + if(sortBy!=null && !sortBy.isEmpty()) { + Sort.Direction direction= Sort.Direction.ASC; // Default direction + + if(sortDir!=null && sortDir.equalsIgnoreCase("desc")) { + direction = Sort.Direction.DESC; + } + + pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(direction, sortBy)) ; + } + + return memberService.getAllMembers(pageable); } /** diff --git a/src/main/java/com/libraryman_api/member/MemberService.java b/src/main/java/com/libraryman_api/member/MemberService.java index b73e384..93c8843 100644 --- a/src/main/java/com/libraryman_api/member/MemberService.java +++ b/src/main/java/com/libraryman_api/member/MemberService.java @@ -1,10 +1,13 @@ package com.libraryman_api.member; +import java.util.Optional; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + import com.libraryman_api.exception.ResourceNotFoundException; import com.libraryman_api.notification.NotificationService; -import org.springframework.stereotype.Service; -import java.util.List; -import java.util.Optional; /** * Service class responsible for managing member-related operations in the LibraryMan system. @@ -39,12 +42,13 @@ public MemberService(MemberRepository memberRepository, NotificationService noti } /** - * Retrieves all members from the database. + * Retrieves a paginated list of all members from the database. * - * @return a list of all members + * @param pageable the pagination information, including the page number and size + * @return a {@link Page} of {@link Members} representing all members */ - public List getAllMembers() { - return memberRepository.findAll(); + public Page getAllMembers(Pageable pageable) { + return memberRepository.findAll(pageable); } /**