Skip to content

Commit

Permalink
Enhancement: Use DTOs for entity exposure in controller layer
Browse files Browse the repository at this point in the history
  • Loading branch information
anishmu20 committed Oct 6, 2024
1 parent e6ec2bc commit 3eafa4b
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 99 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/libraryman_api/book/Book.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public int getCopiesAvailable() {
return copiesAvailable;
}

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

public void setTitle(String title) {
this.title = title;
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/libraryman_api/book/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ public class BookController {
* @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.
* @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.
*/
@GetMapping
public Page<Book> getAllBooks(@PageableDefault(page=0, size=5, sort="title") Pageable pageable,
public Page<BookDto> getAllBooks(@PageableDefault(page=0, size=5, sort="title") Pageable pageable,
@RequestParam(required = false) String sortBy,
@RequestParam(required = false) String sortDir) {

Expand All @@ -58,7 +58,7 @@ public Page<Book> getAllBooks(@PageableDefault(page=0, size=5, sort="title") Pag
* @throws ResourceNotFoundException if the book with the specified ID is not found.
*/
@GetMapping("/{id}")
public ResponseEntity<Book> getBookById(@PathVariable int id) {
public ResponseEntity<BookDto> getBookById(@PathVariable int id) {
return bookService.getBookById(id)
.map(ResponseEntity::ok)
.orElseThrow(() -> new ResourceNotFoundException("Book not found"));
Expand All @@ -67,24 +67,24 @@ public ResponseEntity<Book> getBookById(@PathVariable int id) {
/**
* Adds a new book to the library.
*
* @param book the {@link Book} object representing the new book to add.
* @param bookDto the {@link Book} object representing the new book to add.
* @return the added {@link Book} object.
*/
@PostMapping
public Book addBook(@RequestBody Book book) {
return bookService.addBook(book);
public BookDto addBook(@RequestBody BookDto bookDto) {
return bookService.addBook(bookDto);
}

/**
* Updates an existing book in the library.
*
* @param id the ID of the book to update.
* @param bookDetails the {@link Book} object containing the updated book details.
* @param bookDtoDetails the {@link Book} object containing the updated book details.
* @return the updated {@link Book} object.
*/
@PutMapping("/{id}")
public Book updateBook(@PathVariable int id, @RequestBody Book bookDetails) {
return bookService.updateBook(id, bookDetails);
public BookDto updateBook(@PathVariable int id, @RequestBody BookDto bookDtoDetails) {
return bookService.updateBook(id, bookDtoDetails);
}

/**
Expand Down
65 changes: 48 additions & 17 deletions src/main/java/com/libraryman_api/book/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ public BookService(BookRepository bookRepository) {
* @return a {@link Page} of {@link Book} representing all books
* @throws InvalidSortFieldException if an invalid sortBy field is specified
*/
public Page<Book> getAllBooks(Pageable pageable) {
public Page<BookDto> getAllBooks(Pageable pageable) {
try {
return bookRepository.findAll(pageable);
Page<Book> pagedBooks = bookRepository.findAll(pageable);
return pagedBooks.map(this::EntityToDto);
} catch (PropertyReferenceException ex) {
throw new InvalidSortFieldException("The specified 'sortBy' value is invalid.");
}
Expand All @@ -60,39 +61,44 @@ public Page<Book> getAllBooks(Pageable pageable) {
* @param bookId the ID of the book to retrieve
* @return an {@code Optional} containing the found book, or {@code Optional.empty()} if no book was found
*/
public Optional<Book> getBookById(int bookId) {
return bookRepository.findById(bookId);
public Optional<BookDto> getBookById(int bookId) {

Optional<Book> bookById = bookRepository.findById(bookId);
return bookById.map(this::EntityToDto);
}

/**
* Adds a new book to the database.
*
* @param book the book to be added
* @param bookDto the book to be added
* @return the saved book
*/
public Book addBook(Book book) {
return bookRepository.save(book);
public BookDto addBook(BookDto bookDto) {
Book book = DtoToEntity(bookDto);
Book savedBook = bookRepository.save(book);
return EntityToDto(savedBook);
}

/**
* Updates an existing book with the given details.
*
* @param bookId the ID of the book to update
* @param bookDetails the new details for the book
* @param bookDtoDetails the new details for the book
* @return the updated book
* @throws ResourceNotFoundException if the book with the specified ID is not found
*/
public Book updateBook(int bookId, Book bookDetails) {
public BookDto updateBook(int bookId, BookDto bookDtoDetails) {
Book book = bookRepository.findById(bookId)
.orElseThrow(() -> new ResourceNotFoundException("Book not found"));
book.setTitle(bookDetails.getTitle());
book.setAuthor(bookDetails.getAuthor());
book.setIsbn(bookDetails.getIsbn());
book.setPublisher(bookDetails.getPublisher());
book.setPublishedYear(bookDetails.getPublishedYear());
book.setGenre(bookDetails.getGenre());
book.setCopiesAvailable(bookDetails.getCopiesAvailable());
return bookRepository.save(book);
book.setTitle(bookDtoDetails.getTitle());
book.setAuthor(bookDtoDetails.getAuthor());
book.setIsbn(bookDtoDetails.getIsbn());
book.setPublisher(bookDtoDetails.getPublisher());
book.setPublishedYear(bookDtoDetails.getPublishedYear());
book.setGenre(bookDtoDetails.getGenre());
book.setCopiesAvailable(bookDtoDetails.getCopiesAvailable());
Book updatedBook = bookRepository.save(book);
return EntityToDto(updatedBook);
}

/**
Expand All @@ -107,4 +113,29 @@ public void deleteBook(int bookId) {
bookRepository.delete(book);
}

public BookDto EntityToDto(Book book){
BookDto bookDto= new BookDto();
bookDto.setBookId(book.getBookId());
bookDto.setPublisher(book.getPublisher());
bookDto.setPublishedYear(book.getPublishedYear());
bookDto.setTitle(book.getTitle());
bookDto.setAuthor(book.getAuthor());
bookDto.setGenre(book.getGenre());
bookDto.setIsbn(book.getIsbn());
bookDto.setCopiesAvailable(book.getCopiesAvailable());
return bookDto;
}

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public BorrowingController(BorrowingService borrowingService) {
* The results are sorted by borrow date by default and limited to 5 members per page.
*/
@GetMapping
public Page<Borrowings> getAllBorrowings(@PageableDefault(page=0, size=5, sort="borrowDate") Pageable pageable,
public Page<BorrowingsDto> getAllBorrowings(@PageableDefault(page=0, size=5, sort="borrowDate") Pageable pageable,
@RequestParam(required = false) String sortBy,
@RequestParam(required = false) String sortDir) {

Expand All @@ -60,12 +60,12 @@ public Page<Borrowings> getAllBorrowings(@PageableDefault(page=0, size=5, sort="
/**
* Records a new book borrowing.
*
* @param borrowing the {@link Borrowings} object containing borrowing details.
* @param borrowingsDto the {@link Borrowings} object containing borrowing details.
* @return the saved {@link Borrowings} object representing the borrowing record.
*/
@PostMapping
public Borrowings borrowBook(@RequestBody Borrowings borrowing) {
return borrowingService.borrowBook(borrowing);
public BorrowingsDto borrowBook(@RequestBody BorrowingsDto borrowingsDto) {
return borrowingService.borrowBook(borrowingsDto);
}

/**
Expand All @@ -74,8 +74,8 @@ public Borrowings borrowBook(@RequestBody Borrowings borrowing) {
* @param id the ID of the borrowing record to update.
*/
@PutMapping("/{id}/return")
public void returnBook(@PathVariable int id) {
borrowingService.returnBook(id);
public BorrowingsDto returnBook(@PathVariable int id) {
return borrowingService.returnBook(id);
}

/**
Expand All @@ -101,7 +101,7 @@ public String payFine(@PathVariable int id) {
* The results are sorted by borrow date by default and limited to 5 members per page.
*/
@GetMapping("member/{memberId}")
public Page<Borrowings> getAllBorrowingsOfAMember(@PathVariable int 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) {
Expand All @@ -128,7 +128,7 @@ public Page<Borrowings> getAllBorrowingsOfAMember(@PathVariable int memberId,
* @throws ResourceNotFoundException if the borrowing record with the specified ID is not found.
*/
@GetMapping("{borrowingId}")
public Borrowings getBorrowingById(@PathVariable int borrowingId) {
public BorrowingsDto getBorrowingById(@PathVariable int borrowingId) {
return borrowingService.getBorrowingById(borrowingId)
.orElseThrow(() -> new ResourceNotFoundException("Borrowing not found"));
}
Expand Down
Loading

0 comments on commit 3eafa4b

Please sign in to comment.