Skip to content

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaynegi45 committed Oct 24, 2024
1 parent 7b8d882 commit f987c54
Show file tree
Hide file tree
Showing 40 changed files with 635 additions and 659 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
@EnableCaching
public class LibrarymanApiApplication {

public static void main(String[] args) {
SpringApplication.run(LibrarymanApiApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(LibrarymanApiApplication.class, args);
}

}
54 changes: 28 additions & 26 deletions src/main/java/com/libraryman_api/book/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,60 +50,62 @@ public int getBookId() {
return bookId;
}

public String getTitle() {
return title;
}

public String getAuthor() {
return author;
}

public String getIsbn() {
return isbn;
}

public String getPublisher() {
return publisher;
public void setBookId(int bookId) {
this.bookId = bookId;
}

public int getPublishedYear() {
return publishedYear;
}

public String getGenre() {
return genre;
}

public int getCopiesAvailable() {
return copiesAvailable;
public String getTitle() {
return title;
}

public void setBookId(int bookId) {this.bookId = bookId;}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getIsbn() {
return isbn;
}

public void setIsbn(String isbn) {
this.isbn = isbn;
}

public String getPublisher() {
return publisher;
}

public void setPublisher(String publisher) {
this.publisher = publisher;
}

public int getPublishedYear() {
return publishedYear;
}

public void setPublishedYear(int publishedYear) {
this.publishedYear = publishedYear;
}

public String getGenre() {
return genre;
}

public void setGenre(String genre) {
this.genre = genre;
}

public int getCopiesAvailable() {
return copiesAvailable;
}

public void setCopiesAvailable(int copiesAvailable) {
this.copiesAvailable = copiesAvailable;
}
Expand Down
34 changes: 17 additions & 17 deletions src/main/java/com/libraryman_api/book/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,26 +28,26 @@ public class BookController {
* Retrieves a paginated and sorted list of all 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.
* @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 BookDto} objects representing the books in the library.
* The results are sorted by title by default and limited to 5 books per page.
* The results are sorted by title by default and limited to 5 books per page.
*/
@GetMapping
public Page<BookDto> getAllBooks(@PageableDefault(page=0, size=5, sort="title") Pageable pageable,
@RequestParam(required = false) String sortBy,
@RequestParam(required = false) String sortDir) {
public Page<BookDto> 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)) ;
}
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 Expand Up @@ -80,7 +80,7 @@ public BookDto addBook(@RequestBody BookDto bookDto) {
/**
* Updates an existing book in the library.
*
* @param id the ID of the book to update.
* @param id the ID of the book to update.
* @param bookDtoDetails the {@link Book} object containing the updated book details.
* @return the updated {@link Book} object.
*/
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/libraryman_api/book/BookDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@ public class BookDto {

private int bookId;
private String title;

private String author;

private String isbn;

private String publisher;


private int publishedYear;
private String genre;

private int copiesAvailable;

public BookDto(int bookId, String title, String author, String isbn, String publisher, int publishedYear, String genre, int copiesAvailable) {
Expand Down
41 changes: 21 additions & 20 deletions src/main/java/com/libraryman_api/book/BookService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.libraryman_api.book;

import java.util.Optional;

import com.libraryman_api.exception.InvalidSortFieldException;
import com.libraryman_api.exception.ResourceNotFoundException;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
Expand All @@ -10,8 +10,7 @@
import org.springframework.data.mapping.PropertyReferenceException;
import org.springframework.stereotype.Service;

import com.libraryman_api.exception.InvalidSortFieldException;
import com.libraryman_api.exception.ResourceNotFoundException;
import java.util.Optional;

/**
* Service class for managing books in the LibraryMan system.
Expand Down Expand Up @@ -52,7 +51,7 @@ public BookService(BookRepository bookRepository) {

@Cacheable(value = "books")
public Page<BookDto> getAllBooks(Pageable pageable) {
try {
try {
Page<Book> pagedBooks = bookRepository.findAll(pageable);
return pagedBooks.map(this::EntityToDto);
} catch (PropertyReferenceException ex) {
Expand All @@ -67,7 +66,7 @@ public Page<BookDto> getAllBooks(Pageable pageable) {
* @return an {@code Optional} containing the found book, or {@code Optional.empty()} if no book was found
*/

@Cacheable(value = "books", key ="#bookId")
@Cacheable(value = "books", key = "#bookId")
public Optional<BookDto> getBookById(int bookId) {

Optional<Book> bookById = bookRepository.findById(bookId);
Expand All @@ -91,7 +90,7 @@ public BookDto addBook(BookDto bookDto) {
/**
* Updates an existing book with the given details.
*
* @param bookId the ID of the book to update
* @param bookId the ID of the book to update
* @param bookDtoDetails the new details for the book
* @return the updated book
* @throws ResourceNotFoundException if the book with the specified ID is not found
Expand Down Expand Up @@ -131,6 +130,7 @@ public void deleteBook(int bookId) {
.orElseThrow(() -> new ResourceNotFoundException("Book not found"));
bookRepository.delete(book);
}

/**
* Converts a Book entity to a BookDto object.
*
Expand All @@ -143,8 +143,8 @@ public void deleteBook(int bookId) {
* @return a BookDto object with data populated from the entity
*/

public BookDto EntityToDto(Book book){
BookDto bookDto= new BookDto();
public BookDto EntityToDto(Book book) {
BookDto bookDto = new BookDto();
bookDto.setBookId(book.getBookId());
bookDto.setPublisher(book.getPublisher());
bookDto.setPublishedYear(book.getPublishedYear());
Expand All @@ -155,6 +155,7 @@ public BookDto EntityToDto(Book book){
bookDto.setCopiesAvailable(book.getCopiesAvailable());
return bookDto;
}

/**
* Converts a BookDto object to a Book entity.
*
Expand All @@ -168,16 +169,16 @@ public BookDto EntityToDto(Book book){
*/


public Book DtoToEntity(BookDto bookDto){
Book book= new Book();
book.setBookId(bookDto.getBookId());
book.setAuthor(bookDto.getAuthor());
book.setGenre(bookDto.getGenre());
book.setPublisher(bookDto.getPublisher());
book.setPublishedYear(bookDto.getPublishedYear());
book.setTitle(bookDto.getTitle());
book.setCopiesAvailable(bookDto.getCopiesAvailable());
book.setIsbn(bookDto.getIsbn());
return book;
public Book DtoToEntity(BookDto bookDto) {
Book book = new Book();
book.setBookId(bookDto.getBookId());
book.setAuthor(bookDto.getAuthor());
book.setGenre(bookDto.getGenre());
book.setPublisher(bookDto.getPublisher());
book.setPublishedYear(bookDto.getPublishedYear());
book.setTitle(bookDto.getTitle());
book.setCopiesAvailable(bookDto.getCopiesAvailable());
book.setIsbn(bookDto.getIsbn());
return book;
}
}
68 changes: 33 additions & 35 deletions src/main/java/com/libraryman_api/borrowing/BorrowingController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.libraryman_api.borrowing;

import com.libraryman_api.exception.ResourceNotFoundException;

import org.springframework.security.access.prepost.PreAuthorize;

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.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
Expand All @@ -35,27 +33,27 @@ public BorrowingController(BorrowingService borrowingService) {
* Retrieves a paginated and sorted list of all borrowing records 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.
* @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.
* The results are sorted by borrow date by default and limited to 5 members per page.
*/
@GetMapping
@PreAuthorize("hasRole('LIBRARIAN') or hasRole('ADMIN')")
public Page<BorrowingsDto> 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
public Page<BorrowingsDto> getAllBorrowings(@PageableDefault(page = 0, size = 5, sort = "borrowDate") Pageable pageable,
@RequestParam(required = false) String sortBy,
@RequestParam(required = false) String sortDir) {

if(sortDir!=null && sortDir.equalsIgnoreCase("desc")) {
direction = Sort.Direction.DESC;
}
// Adjust the pageable based on dynamic sorting parameters
if (sortBy != null && !sortBy.isEmpty()) {
Sort.Direction direction = Sort.Direction.ASC; // Default direction

pageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(), Sort.by(direction, sortBy)) ;
}
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 @@ -99,28 +97,28 @@ public String payFine(@PathVariable int id) {
*
* @param memberId the ID of the member whose borrowing records are to be retrieved.
* @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.
* @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.
* The results are sorted by borrow date by default and limited to 5 members per page.
*/
@GetMapping("member/{memberId}")
@PreAuthorize("hasRole('LIBRARIAN') or hasRole('ADMIN') or (hasRole('USER') and #memberId == authentication.principal.memberId)")
public Page<BorrowingsDto> 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)) ;
}
@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,17 +1,18 @@
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;

@Repository
public interface BorrowingRepository extends JpaRepository<Borrowings, Integer> {

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

List<Borrowings> findByBook_bookId(int bookId);
}

Loading

0 comments on commit f987c54

Please sign in to comment.