Skip to content

Commit

Permalink
refactor: nullSafeBooleanBuilder로 NPE를 방지한다.
Browse files Browse the repository at this point in the history
  • Loading branch information
rlarltj committed Jul 15, 2024
1 parent c989a3a commit b872347
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.moneymong.domain.agency.entity.Agency;
import com.moneymong.domain.agency.entity.enums.AgencyType;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
Expand All @@ -12,6 +13,7 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.function.Supplier;

import static com.moneymong.domain.agency.entity.QAgency.agency;

Expand All @@ -38,7 +40,7 @@ public Page<Agency> findByUniversityNameByPaging(String universityName, Pageable
public Page<Agency> findByUniversityNameAndAgencyTypeByPaging(String universityName, AgencyType type,
Pageable pageable) {
JPAQuery<Agency> query = queryFactory.selectFrom(agency)
.where(eqUniversityName(universityName).or(agency.agencyType.eq(type)))
.where(agency.agencyType.eq(type).or(eqUniversityName(universityName)))
.limit(pageable.getPageSize())
.offset(pageable.getOffset());

Expand All @@ -58,7 +60,16 @@ private JPAQuery<Agency> getCountQuery(String universityName, AgencyType type) {
return queryFactory.selectFrom(agency)
.where(eqUniversityName(universityName).or(agency.agencyType.eq(type)));
}
private BooleanExpression eqUniversityName(String universityName) {
return universityName != null ? agency.universityName.eq(universityName) : null;

private BooleanBuilder eqUniversityName(String universityName) {
return nullSafeBooleanBuilder(() -> agency.universityName.eq(universityName));
}

private BooleanBuilder nullSafeBooleanBuilder(Supplier<BooleanExpression> supplier) {
try {
return new BooleanBuilder(supplier.get());
} catch (IllegalArgumentException e) {
return new BooleanBuilder();
}
}
}

0 comments on commit b872347

Please sign in to comment.