Skip to content

Commit

Permalink
1. Add Data transfer object(DTOs) 2. use builder pattern #52
Browse files Browse the repository at this point in the history
  • Loading branch information
anishmu20 committed Oct 4, 2024
1 parent eb50835 commit 49061a3
Show file tree
Hide file tree
Showing 20 changed files with 407 additions and 409 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.0.0</version>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
94 changes: 8 additions & 86 deletions src/main/java/com/libraryman_api/book/Book.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package com.libraryman_api.book;

import jakarta.persistence.*;
import lombok.*;



@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@ToString
public class Book {
@Id

Expand Down Expand Up @@ -33,91 +41,5 @@ public class Book {
@Column(name = "copies_available", nullable = false)
private int copiesAvailable;

public Book() {
}

public Book(String title, String author, String isbn, String publisher, int publishedYear, String genre, int copiesAvailable) {
this.title = title;
this.author = author;
this.isbn = isbn;
this.publisher = publisher;
this.publishedYear = publishedYear;
this.genre = genre;
this.copiesAvailable = copiesAvailable;
}

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 int getPublishedYear() {
return publishedYear;
}

public String getGenre() {
return genre;
}

public int getCopiesAvailable() {
return copiesAvailable;
}


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

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

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

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

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

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

public void setCopiesAvailable(int copiesAvailable) {
this.copiesAvailable = copiesAvailable;
}

@Override
public String toString() {
return "Books{" +
"bookId=" + bookId +
", title='" + title + '\'' +
", author='" + author + '\'' +
", isbn='" + isbn + '\'' +
", publisher='" + publisher + '\'' +
", publishedYear=" + publishedYear +
", genre='" + genre + '\'' +
", copiesAvailable=" + copiesAvailable +
'}';
}
}
21 changes: 11 additions & 10 deletions src/main/java/com/libraryman_api/book/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@ public class BookController {
/**
* Retrieves a list of all books in the library.
*
* @return a list of {@link Book} objects representing all the books in the library.
* @return a list of {@link BookDto} objects representing all the books in the library.
*/
@GetMapping
public List<Book> getAllBooks() {
public List<BookDto> getAllBooks() {

return bookService.getAllBooks();
}

/**
* Retrieves a book by its ID.
*
* @param id the ID of the book to retrieve.
* @return a {@link ResponseEntity} containing the {@link Book} object, if found.
* @return a {@link ResponseEntity} containing the {@link BookDto} object, if found.
* @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 @@ -47,24 +48,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
34 changes: 34 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,34 @@
package com.libraryman_api.book;

import jakarta.persistence.Column;
import lombok.*;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Builder
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;



}
54 changes: 37 additions & 17 deletions src/main/java/com/libraryman_api/book/BookService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.libraryman_api.book;

import com.libraryman_api.exception.ResourceNotFoundException;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
Expand All @@ -25,13 +27,16 @@
public class BookService {

private final BookRepository bookRepository;
@Autowired
private ModelMapper mapper;

/**
* Constructs a new {@code BookService} with the specified {@code BookRepository}.
*
* @param bookRepository the repository to be used by this service to interact with the database
*/
public BookService(BookRepository bookRepository) {

this.bookRepository = bookRepository;
}

Expand All @@ -40,8 +45,9 @@ public BookService(BookRepository bookRepository) {
*
* @return a list of all books
*/
public List<Book> getAllBooks() {
return bookRepository.findAll();
public List<BookDto> getAllBooks() {
List<Book> allBooks = bookRepository.findAll();
return allBooks.stream().map(this::EntityToDto).toList();
}

/**
Expand All @@ -50,39 +56,44 @@ public List<Book> getAllBooks() {
* @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> getBook = bookRepository.findById(bookId);
return getBook.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 @@ -97,4 +108,13 @@ public void deleteBook(int bookId) {
bookRepository.delete(book);
}



public BookDto EntityToDto(Book book){
return mapper.map(book,BookDto.class);
}
public Book DtoToEntity(BookDto bookDto){
return mapper.map(bookDto,Book.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,20 @@ public BorrowingController(BorrowingService borrowingService) {
* @return a list of {@link Borrowings} objects representing all borrowings.
*/
@GetMapping
public List<Borrowings> getAllBorrowings() {
public List<BorrowingsDto> getAllBorrowings() {

return borrowingService.getAllBorrowings();
}

/**
* Records a new book borrowing.
*
* @param borrowing the {@link Borrowings} object containing borrowing details.
* @param borrowingDto 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 borrowingDto) {
return borrowingService.borrowBook(borrowingDto);
}

/**
Expand All @@ -52,8 +53,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 @@ -75,7 +76,7 @@ public String payFine(@PathVariable int id) {
* @return a list of {@link Borrowings} objects representing the member's borrowings.
*/
@GetMapping("member/{memberId}")
public List<Borrowings> getAllBorrowingsOfAMember(@PathVariable int memberId) {
public List<BorrowingsDto> getAllBorrowingsOfAMember(@PathVariable int memberId) {
return borrowingService.getAllBorrowingsOfMember(memberId);
}

Expand All @@ -87,7 +88,7 @@ public List<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 49061a3

Please sign in to comment.