Skip to content

Commit

Permalink
fix:쿼리 옵션의 모든 조건이 검색 조건으로 활용되도록 수정 (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sangwook02 authored Jul 27, 2024
1 parent 74b8035 commit 6cd275d
Showing 1 changed file with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import static com.goldbalance.dive.domain.article.domain.QQuiz.*;

import com.goldbalance.dive.domain.article.domain.Article;
import com.goldbalance.dive.domain.article.domain.Category;
import com.goldbalance.dive.domain.article.domain.Duration;
import com.goldbalance.dive.domain.article.dto.request.ArticleQueryOption;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
Expand All @@ -19,11 +22,26 @@ public class ArticleCustomRepositoryImpl implements ArticleCustomRepository {
public List<Article> searchArticle(ArticleQueryOption queryOption) {
return queryFactory
.selectFrom(article)
.where(containsKeyword(queryOption))
.where(matchesQueryOption(queryOption))
.fetch();
}

private BooleanExpression containsKeyword(ArticleQueryOption queryOption) {
return queryOption.keyword() != null ? article.title.containsIgnoreCase(queryOption.keyword()) : null;
private BooleanExpression containsKeyword(String keyword) {
return keyword != null ? article.title.containsIgnoreCase(keyword) : null;
}

private BooleanExpression eqCategory(Category category) {
return category != null ? article.category.eq(category) : null;
}

private BooleanExpression eqDuration(Duration duration) {
return duration != null ? article.duration.eq(duration) : null;
}

private BooleanBuilder matchesQueryOption(ArticleQueryOption queryOption) {
return new BooleanBuilder()
.and(containsKeyword(queryOption.keyword()))
.and(eqCategory(queryOption.category()))
.and(eqDuration(queryOption.duration()));
}
}

0 comments on commit 6cd275d

Please sign in to comment.