-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from TarasYashchuk/dev
implemented book search
- Loading branch information
Showing
14 changed files
with
214 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package mate.academy.dto; | ||
|
||
public record BookSearchParametersDto( | ||
String title, | ||
String author, | ||
String isbn, | ||
String price) { | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/mate/academy/repository/SpecificationBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package mate.academy.repository; | ||
|
||
import mate.academy.dto.BookSearchParametersDto; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
public interface SpecificationBuilder<T> { | ||
Specification<T> build(BookSearchParametersDto searchParameters); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/mate/academy/repository/SpecificationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package mate.academy.repository; | ||
|
||
import mate.academy.model.Book; | ||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
public interface SpecificationProvider<T> { | ||
Specification<Book> getSpecification(String param); | ||
|
||
String getKey(); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/mate/academy/repository/SpecificationProviderManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package mate.academy.repository; | ||
|
||
public interface SpecificationProviderManager<T> { | ||
SpecificationProvider<T> getSpecificationProvider(String key); | ||
} |
5 changes: 3 additions & 2 deletions
5
...te/academy/repository/BookRepository.java → ...ademy/repository/book/BookRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
package mate.academy.repository; | ||
package mate.academy.repository.book; | ||
|
||
import mate.academy.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> { | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/mate/academy/repository/book/BookSpecificationBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package mate.academy.repository.book; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.dto.BookSearchParametersDto; | ||
import mate.academy.model.Book; | ||
import mate.academy.repository.SpecificationBuilder; | ||
import mate.academy.repository.SpecificationProviderManager; | ||
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> specification = Specification.where(null); | ||
|
||
specification = addSpecification(specification, | ||
BookSpecificationKeys.TITLE, searchParameters.title()); | ||
specification = addSpecification(specification, | ||
BookSpecificationKeys.PRICE, searchParameters.price()); | ||
specification = addSpecification(specification, | ||
BookSpecificationKeys.ISBN, searchParameters.isbn()); | ||
specification = addSpecification(specification, | ||
BookSpecificationKeys.AUTHOR, searchParameters.author()); | ||
|
||
return specification; | ||
} | ||
|
||
private Specification<Book> addSpecification(Specification<Book> specification, | ||
String key, String value) { | ||
if (value != null && !value.isEmpty()) { | ||
specification = specification | ||
.and(bookSpecificationProviderManager.getSpecificationProvider(key) | ||
.getSpecification(value)); | ||
} | ||
return specification; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/mate/academy/repository/book/BookSpecificationKeys.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package mate.academy.repository.book; | ||
|
||
public final class BookSpecificationKeys { | ||
public static final String TITLE = "title"; | ||
public static final String PRICE = "price"; | ||
public static final String ISBN = "isbn"; | ||
public static final String AUTHOR = "author"; | ||
|
||
private BookSpecificationKeys() { | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/mate/academy/repository/book/BookSpecificationProviderManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package mate.academy.repository.book; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.model.Book; | ||
import mate.academy.repository.SpecificationProvider; | ||
import mate.academy.repository.SpecificationProviderManager; | ||
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 get specification with key: " + key)); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/mate/academy/repository/book/spec/AuthorSpecificationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package mate.academy.repository.book.spec; | ||
|
||
import mate.academy.model.Book; | ||
import mate.academy.repository.SpecificationProvider; | ||
import mate.academy.repository.book.BookSpecificationKeys; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class AuthorSpecificationProvider implements SpecificationProvider<Book> { | ||
|
||
public Specification<Book> getSpecification(String param) { | ||
return (root, query, criteriaBuilder) -> criteriaBuilder | ||
.equal(root.get(BookSpecificationKeys.AUTHOR), param); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return BookSpecificationKeys.AUTHOR; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/main/java/mate/academy/repository/book/spec/IsbnSpecificationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package mate.academy.repository.book.spec; | ||
|
||
import mate.academy.model.Book; | ||
import mate.academy.repository.SpecificationProvider; | ||
import mate.academy.repository.book.BookSpecificationKeys; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class IsbnSpecificationProvider implements SpecificationProvider<Book> { | ||
|
||
public Specification<Book> getSpecification(String param) { | ||
return (root, query, criteriaBuilder) -> criteriaBuilder | ||
.equal(root.get(BookSpecificationKeys.ISBN), param); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return BookSpecificationKeys.ISBN; | ||
} | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
src/main/java/mate/academy/repository/book/spec/PriceSpecificationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package mate.academy.repository.book.spec; | ||
|
||
import mate.academy.model.Book; | ||
import mate.academy.repository.SpecificationProvider; | ||
import mate.academy.repository.book.BookSpecificationKeys; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class PriceSpecificationProvider implements SpecificationProvider<Book> { | ||
public Specification<Book> getSpecification(String param) { | ||
return (root, query, criteriaBuilder) -> criteriaBuilder | ||
.equal(root.get(BookSpecificationKeys.PRICE), param); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return BookSpecificationKeys.PRICE; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/mate/academy/repository/book/spec/TitleSpecificationProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package mate.academy.repository.book.spec; | ||
|
||
import mate.academy.model.Book; | ||
import mate.academy.repository.SpecificationProvider; | ||
import mate.academy.repository.book.BookSpecificationKeys; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class TitleSpecificationProvider implements SpecificationProvider<Book> { | ||
public Specification<Book> getSpecification(String param) { | ||
return (root, query, criteriaBuilder) -> criteriaBuilder | ||
.equal(root.get(BookSpecificationKeys.TITLE), param); | ||
} | ||
|
||
@Override | ||
public String getKey() { | ||
return BookSpecificationKeys.TITLE; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters