Skip to content

Commit

Permalink
Merge pull request #53 from Guhapriya01/feature/pagination-sorting-api
Browse files Browse the repository at this point in the history
Add pagination and sorting functionality to API endpoints
  • Loading branch information
ajaynegi45 authored Oct 2, 2024
2 parents 4e022ed + c362ae5 commit e8905ac
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ target/
.settings
.springBeans
.sts4-cache
/bin/

### IntelliJ IDEA ###
.idea
Expand Down
32 changes: 26 additions & 6 deletions src/main/java/com/libraryman_api/book/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<Book> getAllBooks() {
return bookService.getAllBooks();
public Page<Book> 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);
}

/**
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/com/libraryman_api/book/BookService.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<Book> getAllBooks() {
return bookRepository.findAll();
public Page<Book> getAllBooks(Pageable pageable) {
return bookRepository.findAll(pageable);
}

/**
Expand Down
61 changes: 51 additions & 10 deletions src/main/java/com/libraryman_api/borrowing/BorrowingController.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<Borrowings> getAllBorrowings() {
return borrowingService.getAllBorrowings();
public Page<Borrowings> 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);
}

/**
Expand Down Expand Up @@ -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<Borrowings> getAllBorrowingsOfAMember(@PathVariable int memberId) {
return borrowingService.getAllBorrowingsOfMember(memberId);
public Page<Borrowings> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Borrowings, Integer> {

// Underscore (_) used for property traversal, navigating from Borrowings to Members entity via 'member' property
Optional<List<Borrowings>> findByMember_memberId(int memberId);
Page<Borrowings> findByMember_memberId(int memberId, Pageable pageable);
List<Borrowings> findByBook_bookId(int bookId);
}

27 changes: 18 additions & 9 deletions src/main/java/com/libraryman_api/borrowing/BorrowingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<Borrowings> getAllBorrowings() {
return borrowingRepository.findAll();
public Page<Borrowings> getAllBorrowings(Pageable pageable) {
return borrowingRepository.findAll(pageable);
}

/**
Expand Down Expand Up @@ -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<Borrowings> getAllBorrowingsOfMember(int memberId) {
return borrowingRepository.findByMember_memberId(memberId).orElseThrow(() -> new ResourceNotFoundException("Member didn't borrow any book"));
public Page<Borrowings> getAllBorrowingsOfMember(int memberId, Pageable pageable) {
Page<Borrowings> borrowings = borrowingRepository.findByMember_memberId(memberId, pageable);

if (borrowings.isEmpty()) {
throw new ResourceNotFoundException("Member didn't borrow any book");
}
return borrowings;
}
}
34 changes: 28 additions & 6 deletions src/main/java/com/libraryman_api/member/MemberController.java
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<Members> getAllMembers() {
return memberService.getAllMembers();
public Page<Members> 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);
}

/**
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/com/libraryman_api/member/MemberService.java
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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<Members> getAllMembers() {
return memberRepository.findAll();
public Page<Members> getAllMembers(Pageable pageable) {
return memberRepository.findAll(pageable);
}

/**
Expand Down

0 comments on commit e8905ac

Please sign in to comment.