diff --git a/src/main/java/com/libraryman_api/book/BookController.java b/src/main/java/com/libraryman_api/book/BookController.java index c3de162..766b3b7 100644 --- a/src/main/java/com/libraryman_api/book/BookController.java +++ b/src/main/java/com/libraryman_api/book/BookController.java @@ -105,13 +105,18 @@ public void deleteBook(@PathVariable int id) { /** * Searches book based on title, author, genre, etc. - * + * It uses a keyword parameter to filter the books, and pagination is applied to the search results. + * If no book is found it will return 204(No content found) http response. + * If keyword is null then it will return all books. * @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); + @GetMapping("/search") + public ResponseEntity> searchBook(@RequestParam String keyword, @PageableDefault(page = 0, size = 5, sort = "title") Pageable pageable){ + Page books=bookService.searchBook(keyword,pageable); + if(!books.isEmpty()) + return ResponseEntity.ok(books); + return ResponseEntity.noContent().build(); } } \ 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 8674be0..96942f3 100644 --- a/src/main/java/com/libraryman_api/book/BookRepository.java +++ b/src/main/java/com/libraryman_api/book/BookRepository.java @@ -21,12 +21,12 @@ public interface BookRepository extends JpaRepository { */ @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%") + + "(LOWER(b.title) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " + + "LOWER(b.author) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " + + "LOWER(b.publisher) LIKE LOWER(CONCAT('%', :keyword, '%')) OR " + + "LOWER(b.genre) LIKE LOWER(CONCAT('%', :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); }