-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,4 @@ | ||
package bookstore.dto; | ||
|
||
public record BookSearchParametersDto(String[] authors, String[] titles) { | ||
} |
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 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
9
src/main/java/bookstore/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,9 @@ | ||
package bookstore.repository; | ||
|
||
import org.springframework.data.jpa.domain.Specification; | ||
|
||
public interface SpecificationProvider<T> { | ||
String getKey(); | ||
|
||
Specification<T> getSpecification(String[] params); | ||
} |
5 changes: 5 additions & 0 deletions
5
src/main/java/bookstore/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 bookstore.repository; | ||
|
||
public interface SpecificationProviderManager<T> { | ||
SpecificationProvider<T> getSpecificationProvider(String key); | ||
} |
5 changes: 3 additions & 2 deletions
5
.../bookstore/repository/BookRepository.java → ...store/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 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> { | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/bookstore/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,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; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/bookstore/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,25 @@ | ||
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)); | ||
|
||
} | ||
} | ||
|
23 changes: 23 additions & 0 deletions
23
src/main/java/bookstore/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,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()); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/bookstore/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,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()); | ||
} | ||
|
||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.