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

search were implemented #4

Merged
merged 2 commits into from
May 14, 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
11 changes: 11 additions & 0 deletions src/main/java/bookstore/controller/BookController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import bookstore.dto.BookDto;
import bookstore.dto.BookRequestDto;
import bookstore.dto.BookSearchParametersDto;
import bookstore.mapper.BookMapper;
import bookstore.model.Book;
import bookstore.service.BookService;
Expand Down Expand Up @@ -54,4 +55,14 @@ public BookDto updateBook(@PathVariable Long id, @RequestBody BookRequestDto req
public void deleteBook(@PathVariable Long id) {
bookService.deleteById(id);
}

@GetMapping("/search")
@ResponseStatus(HttpStatus.OK)
public List<BookDto> searchBooks(BookSearchParametersDto searchParameters) {
return bookService.search(searchParameters)
.stream()
.map(bookMapper::toDto)
.toList();
}

}
4 changes: 4 additions & 0 deletions src/main/java/bookstore/dto/BookSearchParametersDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package bookstore.dto;

public record BookSearchParametersDto(String[] authors, String[] titles) {
}
8 changes: 8 additions & 0 deletions src/main/java/bookstore/repository/SpecificationBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bookstore.repository;

import bookstore.dto.BookSearchParametersDto;
import org.springframework.data.jpa.domain.Specification;

public interface SpecificationBuilder<T> {
Specification<T> build(BookSearchParametersDto searchParameters);
}
9 changes: 9 additions & 0 deletions src/main/java/bookstore/repository/SpecificationProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package bookstore.repository;

import org.springframework.data.jpa.domain.Specification;

public interface SpecificationProvider<T> {
String getKey();

Specification<T> getSpecification(String[] params);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bookstore.repository;

public interface SpecificationProviderManager<T> {
SpecificationProvider<T> getSpecificationProvider(String key);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package bookstore.repository;
package bookstore.repository.book;

import bookstore.model.Book;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;

@Repository
public interface BookRepository extends JpaRepository<Book, Long> {
public interface BookRepository extends JpaRepository<Book, Long>, JpaSpecificationExecutor<Book> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package bookstore.repository.book;

import bookstore.dto.BookSearchParametersDto;
import bookstore.model.Book;
import bookstore.repository.SpecificationBuilder;
import bookstore.repository.SpecificationProviderManager;
import lombok.RequiredArgsConstructor;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class BookSpecificationBuilder implements SpecificationBuilder<Book> {
private final SpecificationProviderManager<Book> bookSpecificationProviderManager;

@Override
public Specification<Book> build(BookSearchParametersDto searchParameters) {
Specification<Book> spec = Specification.where(null);
if (searchParameters.titles() != null && searchParameters.titles().length > 0) {
spec = spec.and(bookSpecificationProviderManager.getSpecificationProvider("title")
.getSpecification(searchParameters.titles()));
}
if (searchParameters.authors() != null && searchParameters.authors().length > 0) {
spec = spec.and(bookSpecificationProviderManager.getSpecificationProvider("author")
.getSpecification(searchParameters.authors()));
}
return spec;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package bookstore.repository.book;

import bookstore.model.Book;
import bookstore.repository.SpecificationProvider;
import bookstore.repository.SpecificationProviderManager;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class BookSpecificationProviderManager implements SpecificationProviderManager<Book> {
private final List<SpecificationProvider<Book>> bookSpecificationProviders;

@Override
public SpecificationProvider<Book> getSpecificationProvider(String key) {
return bookSpecificationProviders.stream()
.filter(p -> p.getKey().equals(key))
.findFirst()
.orElseThrow(() -> new RuntimeException("Cant find correct specification "
+ "provider for key" + key));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package bookstore.repository.book.spec;

import bookstore.model.Book;
import bookstore.repository.SpecificationProvider;
import java.util.Arrays;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

@Component
public class AuthorSpecificationProvider implements SpecificationProvider<Book> {
private static final String KEY_AUTHOR = "author";

@Override
public String getKey() {
return KEY_AUTHOR;
}

@Override
public Specification<Book> getSpecification(String[] params) {
return (root, query, criteriaBuilder)
-> root.get(KEY_AUTHOR).in(Arrays.stream(params).toArray());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package bookstore.repository.book.spec;

import bookstore.model.Book;
import bookstore.repository.SpecificationProvider;
import java.util.Arrays;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

@Component
public class TitleSpecificationProvider implements SpecificationProvider<Book> {
private static final String KEY_TITLE = "title";

@Override
public String getKey() {
return KEY_TITLE;
}

public Specification<Book> getSpecification(String[] params) {
return (root, query, criteriaBuilder)
-> root.get(KEY_TITLE).in(Arrays.stream(params).toArray());
}

}
3 changes: 3 additions & 0 deletions src/main/java/bookstore/service/BookService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package bookstore.service;

import bookstore.dto.BookSearchParametersDto;
import bookstore.model.Book;
import java.util.List;

Expand All @@ -13,4 +14,6 @@ public interface BookService {
Book updateById(Long id, Book book);

void deleteById(Long id);

List<Book> search(BookSearchParametersDto params);
}
12 changes: 11 additions & 1 deletion src/main/java/bookstore/service/impl/BookServiceImpl.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package bookstore.service.impl;

import bookstore.dto.BookSearchParametersDto;
import bookstore.exception.EntityNotFoundException;
import bookstore.model.Book;
import bookstore.repository.BookRepository;
import bookstore.repository.book.BookRepository;
import bookstore.repository.book.BookSpecificationBuilder;
import bookstore.service.BookService;
import jakarta.transaction.Transactional;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

Expand All @@ -15,6 +18,7 @@
public class BookServiceImpl implements BookService {

private final BookRepository bookRepository;
private final BookSpecificationBuilder bookSpecificationBuilder;

@Override
public Book save(Book book) {
Expand Down Expand Up @@ -45,4 +49,10 @@ public Book updateById(Long id, @RequestBody Book book) {
public void deleteById(Long id) {
bookRepository.deleteById(id);
}

@Override
public List<Book> search(BookSearchParametersDto params) {
Specification<Book> bookSpecification = bookSpecificationBuilder.build(params);
return bookRepository.findAll(bookSpecification);
}
}
Loading