-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: 동적 쿼리 생성 툴을 도입하여 중복 쿼리 개선 (#684)
* chore: QueryDsl 도입을 위한 초기 세팅 * refactor: QueryDsl 도입 - 최근 공모 조회 * test: 날짜 변동으로 테스트 깨짐 해결 * refactor: QueryDsl 도입 - 마감임박 공모 조회 * refactor: QueryDsl 도입 - 높은할인율순 공모 조회 * refactor: QueryDsl 도입 - 참여가능 공모 조회 * refactor: 비슷한 로직의 메서드간 위치 조정 및 IN절 메서드 추출 * style: EOF 추가 * refactor: QueryDslConfig 패키지 위치 변경 * refactor: QueryDsl 적용 레포지토리명 변경
- Loading branch information
Showing
13 changed files
with
364 additions
and
178 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,3 +47,6 @@ openapi3.yaml | |
|
||
### FCM ### | ||
/src/main/resources/fcm | ||
|
||
### QueryDSL ### | ||
/src/main/generated |
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
19 changes: 19 additions & 0 deletions
19
backend/src/main/java/com/zzang/chongdae/global/config/QueryDslConfig.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,19 @@ | ||
package com.zzang.chongdae.global.config; | ||
|
||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class QueryDslConfig { | ||
|
||
@PersistenceContext | ||
EntityManager entityManager; | ||
|
||
@Bean | ||
public JPAQueryFactory jpaQueryFactory() { | ||
return new JPAQueryFactory(entityManager); | ||
} | ||
} |
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
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
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
25 changes: 25 additions & 0 deletions
25
...nd/src/main/java/com/zzang/chongdae/offering/repository/CustomizedOfferingRepository.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 com.zzang.chongdae.offering.repository; | ||
|
||
import com.zzang.chongdae.offering.repository.entity.OfferingEntity; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
public interface CustomizedOfferingRepository { | ||
|
||
List<OfferingEntity> findRecentOfferings(Long lastId, String keyword, Pageable pageable); | ||
|
||
List<OfferingEntity> findJoinableOfferings(Long lastId, String keyword, Pageable pageable); | ||
|
||
List<OfferingEntity> findImminentOfferingsWithTitleKeyword( | ||
LocalDateTime lastMeetingDate, Long lastId, String keyword, Pageable pageable); | ||
|
||
List<OfferingEntity> findImminentOfferingsWithMeetingAddressKeyword( | ||
LocalDateTime lastMeetingDate, Long lastId, String keyword, Pageable pageable); | ||
|
||
List<OfferingEntity> findHighDiscountOfferingsWithTitleKeyword( | ||
double lastDiscountRate, Long lastId, String keyword, Pageable pageable); | ||
|
||
List<OfferingEntity> findHighDiscountOfferingsWithMeetingAddressKeyword( | ||
double lastDiscountRate, Long lastId, String keyword, Pageable pageable); | ||
} |
119 changes: 119 additions & 0 deletions
119
...rc/main/java/com/zzang/chongdae/offering/repository/CustomizedOfferingRepositoryImpl.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,119 @@ | ||
package com.zzang.chongdae.offering.repository; | ||
|
||
import static com.zzang.chongdae.offering.domain.OfferingStatus.AVAILABLE; | ||
import static com.zzang.chongdae.offering.domain.OfferingStatus.FULL; | ||
import static com.zzang.chongdae.offering.domain.OfferingStatus.IMMINENT; | ||
import static com.zzang.chongdae.offering.repository.entity.QOfferingEntity.offeringEntity; | ||
|
||
import com.querydsl.core.types.dsl.BooleanExpression; | ||
import com.querydsl.jpa.impl.JPAQueryFactory; | ||
import com.zzang.chongdae.offering.domain.OfferingStatus; | ||
import com.zzang.chongdae.offering.repository.entity.OfferingEntity; | ||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.Pageable; | ||
|
||
@RequiredArgsConstructor | ||
public class CustomizedOfferingRepositoryImpl implements CustomizedOfferingRepository { | ||
|
||
private final JPAQueryFactory queryFactory; | ||
|
||
@Override | ||
public List<OfferingEntity> findRecentOfferings(Long lastId, String keyword, Pageable pageable) { | ||
return queryFactory.selectFrom(offeringEntity) | ||
.where(offeringEntity.id.lt(lastId), | ||
likeTitleOrMeetingAddress(keyword)) | ||
.orderBy(offeringEntity.id.desc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
} | ||
|
||
@Override | ||
public List<OfferingEntity> findJoinableOfferings(Long lastId, String keyword, Pageable pageable) { | ||
return queryFactory.selectFrom(offeringEntity) | ||
.where(offeringEntity.id.lt(lastId), | ||
inOfferingStatus(AVAILABLE, IMMINENT), | ||
likeTitleOrMeetingAddress(keyword)) | ||
.orderBy(offeringEntity.id.desc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
} | ||
|
||
@Override | ||
public List<OfferingEntity> findImminentOfferingsWithTitleKeyword( | ||
LocalDateTime lastMeetingDate, Long lastId, String keyword, Pageable pageable) { | ||
return findImminentOfferings(lastMeetingDate, lastId, pageable, likeTitle(keyword)); | ||
} | ||
|
||
@Override | ||
public List<OfferingEntity> findImminentOfferingsWithMeetingAddressKeyword( | ||
LocalDateTime lastMeetingDate, Long lastId, String keyword, Pageable pageable) { | ||
return findImminentOfferings(lastMeetingDate, lastId, pageable, likeMeetingAddress(keyword)); | ||
} | ||
|
||
private List<OfferingEntity> findImminentOfferings( | ||
LocalDateTime lastMeetingDate, Long lastId, Pageable pageable, BooleanExpression keywordCondition) { | ||
return queryFactory.selectFrom(offeringEntity) | ||
.where(keywordCondition, | ||
inOfferingStatus(IMMINENT), | ||
offeringEntity.meetingDate.gt(lastMeetingDate) | ||
.or(offeringEntity.meetingDate.eq(lastMeetingDate).and(offeringEntity.id.lt(lastId)))) | ||
.orderBy(offeringEntity.meetingDate.asc(), offeringEntity.id.desc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
} | ||
|
||
@Override | ||
public List<OfferingEntity> findHighDiscountOfferingsWithTitleKeyword( | ||
double lastDiscountRate, Long lastId, String keyword, Pageable pageable) { | ||
return findHighDiscountOfferings(lastDiscountRate, lastId, pageable, likeTitle(keyword)); | ||
} | ||
|
||
@Override | ||
public List<OfferingEntity> findHighDiscountOfferingsWithMeetingAddressKeyword( | ||
double lastDiscountRate, Long lastId, String keyword, Pageable pageable) { | ||
return findHighDiscountOfferings(lastDiscountRate, lastId, pageable, likeMeetingAddress(keyword)); | ||
} | ||
|
||
private List<OfferingEntity> findHighDiscountOfferings( | ||
double lastDiscountRate, Long lastId, Pageable pageable, BooleanExpression keywordCondition) { | ||
return queryFactory.selectFrom(offeringEntity) | ||
.where(keywordCondition, | ||
inOfferingStatus(AVAILABLE, FULL, IMMINENT), | ||
offeringEntity.discountRate.lt(lastDiscountRate) | ||
.or(offeringEntity.discountRate.eq(lastDiscountRate).and(offeringEntity.id.lt(lastId)))) | ||
.orderBy(offeringEntity.discountRate.desc(), offeringEntity.id.desc()) | ||
.offset(pageable.getOffset()) | ||
.limit(pageable.getPageSize()) | ||
.fetch(); | ||
} | ||
|
||
private BooleanExpression likeTitleOrMeetingAddress(String keyword) { | ||
if (keyword == null) { | ||
return null; | ||
} | ||
return likeTitle(keyword).or(likeMeetingAddress(keyword)); | ||
} | ||
|
||
private BooleanExpression likeTitle(String keyword) { | ||
if (keyword == null) { | ||
return null; | ||
} | ||
return offeringEntity.title.like(keyword + '%'); | ||
} | ||
|
||
private BooleanExpression likeMeetingAddress(String keyword) { | ||
if (keyword == null) { | ||
return null; | ||
} | ||
return offeringEntity.meetingAddress.like(keyword + '%'); | ||
} | ||
|
||
private BooleanExpression inOfferingStatus(OfferingStatus... offeringStatus) { | ||
return offeringEntity.offeringStatus.in(offeringStatus); | ||
} | ||
} |
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.