-
Notifications
You must be signed in to change notification settings - Fork 0
EX-235: Implement QueryDSL for dynamic database queries #43
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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
82 changes: 82 additions & 0 deletions
82
...n/java/com/exence/finance/modules/transaction/repository/TransactionPredicateBuilder.java
This file contains hidden or 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,82 @@ | ||
| package com.exence.finance.modules.transaction.repository; | ||
|
|
||
| import com.exence.finance.modules.transaction.dto.TransactionType; | ||
| import com.exence.finance.modules.transaction.dto.request.TransactionFilter; | ||
| import com.exence.finance.modules.transaction.entity.QTransaction; | ||
| import com.querydsl.core.BooleanBuilder; | ||
| import com.querydsl.core.types.Predicate; | ||
| import lombok.AccessLevel; | ||
| import lombok.NoArgsConstructor; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.time.Instant; | ||
|
|
||
| @NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
| public final class TransactionPredicateBuilder { | ||
|
|
||
| private static final QTransaction qTransaction = QTransaction.transaction; | ||
|
|
||
hptrk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public static Predicate buildPredicate(TransactionFilter filter) { | ||
| if (filter == null) { | ||
| return null; | ||
| } | ||
|
|
||
| BooleanBuilder builder = new BooleanBuilder(); | ||
|
|
||
| addKeywordFilter(builder, filter.getKeyword()); | ||
| addCategoryFilter(builder, filter.getCategoryId()); | ||
| addTypeFilter(builder, filter.getType()); | ||
| addDateRangeFilter(builder, filter.getDateFrom(), filter.getDateTo()); | ||
| addAmountRangeFilter(builder, filter.getAmountFrom(), filter.getAmountTo()); | ||
| addRecurringFilter(builder, filter.getRecurring()); | ||
|
|
||
| return builder.hasValue() ? builder : null; | ||
| } | ||
|
|
||
| private static void addKeywordFilter(BooleanBuilder builder, String keyword) { | ||
| if (StringUtils.hasText(keyword)) { | ||
| String lowerKeyword = keyword.toLowerCase(); | ||
| builder.and( | ||
| qTransaction.title.lower().contains(lowerKeyword) | ||
| .or(qTransaction.note.lower().contains(lowerKeyword)) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private static void addCategoryFilter(BooleanBuilder builder, Long categoryId) { | ||
| if (categoryId != null) { | ||
| builder.and(qTransaction.category.id.eq(categoryId)); | ||
| } | ||
| } | ||
|
|
||
| private static void addTypeFilter(BooleanBuilder builder, TransactionType type) { | ||
| if (type != null) { | ||
| builder.and(qTransaction.type.eq(type)); | ||
| } | ||
| } | ||
|
|
||
| private static void addDateRangeFilter(BooleanBuilder builder, Instant dateFrom, Instant dateTo) { | ||
| if (dateFrom != null) { | ||
| builder.and(qTransaction.date.goe(dateFrom)); | ||
| } | ||
| if (dateTo != null) { | ||
| builder.and(qTransaction.date.loe(dateTo)); | ||
| } | ||
| } | ||
|
|
||
| private static void addAmountRangeFilter(BooleanBuilder builder, BigDecimal amountFrom, BigDecimal amountTo) { | ||
| if (amountFrom != null) { | ||
| builder.and(qTransaction.amount.goe(amountFrom)); | ||
| } | ||
| if (amountTo != null) { | ||
| builder.and(qTransaction.amount.loe(amountTo)); | ||
| } | ||
| } | ||
|
|
||
| private static void addRecurringFilter(BooleanBuilder builder, Boolean recurring) { | ||
| if (recurring != null) { | ||
| builder.and(qTransaction.recurring.eq(recurring)); | ||
| } | ||
| } | ||
| } | ||
21 changes: 2 additions & 19 deletions
21
...rc/main/java/com/exence/finance/modules/transaction/repository/TransactionRepository.java
This file contains hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.