diff --git a/src/main/java/com/libraryman_api/book/BookController.java b/src/main/java/com/libraryman_api/book/BookController.java index 0e2660f..c3de162 100644 --- a/src/main/java/com/libraryman_api/book/BookController.java +++ b/src/main/java/com/libraryman_api/book/BookController.java @@ -1,6 +1,8 @@ package com.libraryman_api.book; 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; @@ -100,4 +102,16 @@ public BookDto updateBook(@PathVariable int id, @RequestBody BookDto bookDtoDeta public void deleteBook(@PathVariable int id) { bookService.deleteBook(id); } + + /** + * Searches book based on title, author, genre, etc. + * + * @param keyword the Keyword to search Book + * @param pageable + * @return + */ + @GetMapping("/search/{keyword}") + public Page searchBook(@PathVariable String keyword, @PageableDefault(page = 0, size = 5, sort = "title") Pageable pageable){ + return bookService.searchBook(keyword,pageable); + } } \ No newline at end of file diff --git a/src/main/java/com/libraryman_api/book/BookRepository.java b/src/main/java/com/libraryman_api/book/BookRepository.java index 013b353..8674be0 100644 --- a/src/main/java/com/libraryman_api/book/BookRepository.java +++ b/src/main/java/com/libraryman_api/book/BookRepository.java @@ -1,10 +1,33 @@ package com.libraryman_api.book; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; @Repository public interface BookRepository extends JpaRepository { + + /** + * This method use SQL Query for finding book based on + * title, author, genre, publishedYear, etc. By using LIKE operator + * it search from database based on keyword entered. + * + * @param keyword + * @param pageable + * @return + */ + + @Query("SELECT b FROM Book b WHERE " + + "b.title LIKE %:keyword% OR " + + "b.author LIKE %:keyword% OR " + + "b.publisher LIKE %:keyword% OR " + + "b.genre LIKE %:keyword% OR " + + "CAST(b.publishedYear As string) LIKE %:keyword% OR " + + "CAST(b.copiesAvailable As string) LIKE %:keyword%") + Page searchBook(@Param("keyword") String keyword,Pageable pageable); } diff --git a/src/main/java/com/libraryman_api/book/BookService.java b/src/main/java/com/libraryman_api/book/BookService.java index 292684c..a1b5c90 100644 --- a/src/main/java/com/libraryman_api/book/BookService.java +++ b/src/main/java/com/libraryman_api/book/BookService.java @@ -181,4 +181,16 @@ public Book DtoToEntity(BookDto bookDto) { book.setIsbn(bookDto.getIsbn()); return book; } + + /** + *

This method takes String keyword and search book based on + * title, author, genre, publishedYear, etc. from Book Entity.

+ * + * @param keyword + * @param pageable + * @return + */ + public Page searchBook(String keyword,Pageable pageable ){ + return bookRepository.searchBook(keyword,pageable); + } }