Skip to content

Commit

Permalink
Merge pull request #65 from anishmu20/feature-add-dto
Browse files Browse the repository at this point in the history
Feature: added dto layer  issue  #52
  • Loading branch information
ajaynegi45 authored Oct 7, 2024
2 parents 6989506 + 4b8c601 commit 09e2941
Show file tree
Hide file tree
Showing 14 changed files with 582 additions and 97 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
112 changes: 112 additions & 0 deletions src/main/java/com/libraryman_api/book/BookDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.libraryman_api.book;

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) {
this.bookId = bookId;
this.title = title;
this.author = author;
this.isbn = isbn;
this.publisher = publisher;
this.publishedYear = publishedYear;
this.genre = genre;
this.copiesAvailable = copiesAvailable;
}

public BookDto() {
}

public int getBookId() {
return bookId;
}

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

public String getTitle() {
return title;
}

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;
}

@Override
public String toString() {
return "BookDto{" +
"bookId=" + bookId +
", title='" + title + '\'' +
", author='" + author + '\'' +
", isbn='" + isbn + '\'' +
", publisher='" + publisher + '\'' +
", publishedYear=" + publishedYear +
", genre='" + genre + '\'' +
", copiesAvailable=" + copiesAvailable +
'}';
}
}
88 changes: 71 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 @@ -106,5 +112,53 @@ public void deleteBook(int bookId) {
.orElseThrow(() -> new ResourceNotFoundException("Book not found"));
bookRepository.delete(book);
}
/**
* Converts a Book entity to a BookDto object.
*
* <p>This method takes a Book entity and transforms it into a BookDto object for
* data transfer between application layers. It maps all relevant book details,
* including book ID, publisher, published year, title, author, genre, ISBN,
* and copies available, from the entity to the DTO.</p>
*
* @param book the entity object containing book information
* @return a BookDto object with data populated from the entity
*/

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;
}
/**
* Converts a BookDto object to a Book entity.
*
* <p>This method takes a BookDto object and converts it into a Book entity for
* use in database operations. It maps all relevant book details, including
* book ID, author, genre, publisher, published year, title, ISBN, and copies
* available, from the DTO to the entity.</p>
*
* @param bookDto the DTO object containing book information
* @return a Book entity with data populated from the DTO
*/


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 09e2941

Please sign in to comment.