Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Search Book Functionality #106

Merged
merged 2 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/main/java/com/libraryman_api/book/BookController.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.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -100,4 +102,21 @@ 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.
* 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")
public ResponseEntity<Page<Book>> searchBook(@RequestParam String keyword, @PageableDefault(page = 0, size = 5, sort = "title") Pageable pageable){
Page<Book> books=bookService.searchBook(keyword,pageable);
if(!books.isEmpty())
return ResponseEntity.ok(books);
return ResponseEntity.noContent().build();
}
}
23 changes: 23 additions & 0 deletions src/main/java/com/libraryman_api/book/BookRepository.java
Original file line number Diff line number Diff line change
@@ -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<Book, Integer> {

/**
* 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 "
+ "(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<Book> searchBook(@Param("keyword") String keyword,Pageable pageable);
}


Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/libraryman_api/book/BookService.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,4 +181,16 @@ public Book DtoToEntity(BookDto bookDto) {
book.setIsbn(bookDto.getIsbn());
return book;
}

/**
* <p>This method takes String keyword and search book based on
* title, author, genre, publishedYear, etc. from Book Entity. </p>
*
* @param keyword
* @param pageable
* @return
*/
public Page<Book> searchBook(String keyword,Pageable pageable ){
return bookRepository.searchBook(keyword,pageable);
}
}
Loading