Skip to content
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

refactor: 공모글 목록 조회 필터링 수정 및 추가 #356

Merged
merged 10 commits into from
Aug 16, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public enum OfferingFilter {

JOINABLE("참여가능만", VISIBLE), // 처음: 참여 가능한 것 중 id가 가장 높은 값 10개 / 이후: 참여 가능한 것 중 마지막 id보다 낮은 것 x개 찾기
IMMINENT("마감임박순", VISIBLE), // 처음: 현재 시간보다 큰 것 중 가장 작은 값 10개 / 이후: lastMeetingDate 보다 큰 것 x개 찾기
IMMINENT("마감임박만", VISIBLE), // 처음: 현재 시간보다 큰 것 중 가장 작은 값 10개 / 이후: lastMeetingDate 보다 큰 것 x개 찾기
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

텍스트 수정 좋아요!!

HIGH_DISCOUNT("높은할인율순", VISIBLE), // 처음: 할인율(n빵가격/낱개가격) 가장 높은 값 10개 / 이후: 마지막 할인율(n빵가격/낱개가격)보다 낮은 것 x개 찾기
RECENT("최신순", INVISIBLE); // 처음: id가 가장 높은 값 10개 / 이후: 마지막 id보다 낮은 것 x개 찾기

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ protected List<OfferingEntity> fetchOfferingsWithoutLastId(String searchKeyword,
@Override
protected List<OfferingEntity> fetchOfferingsWithLastOffering(
OfferingEntity lastOffering, String searchKeyword, Pageable pageable) {
double discountRate = lastOffering.toOfferingPrice().calculateDiscountRate();
return offeringRepository.findHighDiscountOfferingsWithKeyword(discountRate, lastOffering.getId(),
searchKeyword, pageable);
double lastDiscountRate = lastOffering.toOfferingPrice().calculateDiscountRate();
return offeringRepository.findHighDiscountOfferingsWithKeyword(
lastDiscountRate, lastOffering.getId(), searchKeyword, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,18 @@ public ImminentOfferingStrategy(OfferingRepository offeringRepository) {

@Override
protected List<OfferingEntity> fetchOfferingsWithoutLastId(String searchKeyword, Pageable pageable) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime threshold = LocalDateTime.now().plusHours(6);
LocalDateTime outOfRangeMeetingDate = LocalDateTime.now();
Long outOfRangeId = findOutOfRangeId();
return offeringRepository.findImminentOfferingsWithKeyword(
now, threshold, outOfRangeMeetingDate, outOfRangeId, searchKeyword, pageable);
outOfRangeMeetingDate, outOfRangeId, searchKeyword, pageable);
}

@Override
protected List<OfferingEntity> fetchOfferingsWithLastOffering(
OfferingEntity lastOffering, String searchKeyword, Pageable pageable) {
LocalDateTime now = LocalDateTime.now();
LocalDateTime threshold = LocalDateTime.now().plusHours(6);
LocalDateTime lastMeetingDate = lastOffering.getMeetingDate();
Long lastId = lastOffering.getId();
return offeringRepository.findImminentOfferingsWithKeyword(
now, threshold, lastMeetingDate, lastId, searchKeyword, pageable);
lastMeetingDate, lastId, searchKeyword, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
public interface OfferingRepository extends JpaRepository<OfferingEntity, Long> {

@Query("""
SELECT o
FROM OfferingEntity as o JOIN OfferingMemberEntity as om
ON o.id = om.offering.id
WHERE om.member = :member
SELECT o
FROM OfferingEntity as o JOIN OfferingMemberEntity as om
ON o.id = om.offering.id
WHERE om.member = :member
""")
List<OfferingEntity> findCommentRoomsByMember(MemberEntity member);

Expand All @@ -30,30 +30,35 @@ public interface OfferingRepository extends JpaRepository<OfferingEntity, Long>
@Query("""
SELECT o
FROM OfferingEntity o
WHERE ((o.meetingDate > :lastMeetingDate AND o.meetingDate < :threshold)
OR (o.meetingDate = :lastMeetingDate AND o.id < :lastId AND o.meetingDate < :threshold)
OR (o.totalCount <= 3 AND (o.totalCount - o.currentCount) < 2 AND (o.totalCount - o.currentCount) > 0)
OR (o.totalCount > 3 AND (o.totalCount - o.currentCount) < 3 AND (o.totalCount - o.currentCount) > 0))
AND (:keyword IS NULL OR o.title LIKE %:keyword% OR o.meetingAddress LIKE %:keyword%)
AND (o.isManualConfirmed IS FALSE)
AND (o.meetingDate >= :now)
AND ((o.meetingDate > :lastMeetingDate) OR (o.meetingDate = :lastMeetingDate AND o.id < :lastId))
WHERE (o.offeringStatus = 'IMMINENT')
AND (o.meetingDate > :lastMeetingDate OR (o.meetingDate = :lastMeetingDate AND o.id < :lastId))
AND (:keyword IS NULL OR o.title LIKE %:keyword% OR o.meetingAddress LIKE %:keyword%)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오와 쿼리가 반토막 났네요 대박

ORDER BY o.meetingDate ASC, o.id DESC
""")
List<OfferingEntity> findImminentOfferingsWithKeyword(
LocalDateTime now, LocalDateTime threshold, LocalDateTime lastMeetingDate, Long lastId, String keyword,
Pageable pageable);
LocalDateTime lastMeetingDate, Long lastId, String keyword, Pageable pageable);

@Query("""
SELECT o
FROM OfferingEntity o
WHERE ((o.originPrice - (o.totalPrice * 1.0 / o.totalCount)) / o.originPrice < :discountRate
OR ((o.originPrice - (o.totalPrice * 1.0 / o.totalCount)) / o.originPrice = :discountRate AND o.id < :lastId))
WHERE (o.offeringStatus != 'CONFIRMED')
AND (o.discountRate < :lastDiscountRate OR (o.discountRate = :lastDiscountRate AND o.id < :lastId))
AND (:keyword IS NULL OR o.title LIKE %:keyword% OR o.meetingAddress LIKE %:keyword%)
ORDER BY (o.originPrice - (o.totalPrice * 1.0 / o.totalCount)) / o.originPrice DESC, o.id DESC
ORDER BY o.discountRate DESC, o.id DESC
""")
List<OfferingEntity> findHighDiscountOfferingsWithKeyword(
double discountRate, Long lastId, String keyword, Pageable pageable);
double lastDiscountRate, Long lastId, String keyword, Pageable pageable);

@Query("""
SELECT o
FROM OfferingEntity o
WHERE (o.offeringStatus = 'AVAILABLE' OR o.offeringStatus = 'IMMINENT')
AND (o.id < :lastId)
AND (:keyword IS NULL OR o.title LIKE %:keyword% OR o.meetingAddress LIKE %:keyword%)
ORDER BY o.id DESC
""")
List<OfferingEntity> findJoinableOfferingsWithKeyword(Long lastId, String keyword, Pageable pageable);


@Query("SELECT MAX(o.id) FROM OfferingEntity o")
Long findMaxId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.zzang.chongdae.offering.domain.OfferingFilter;
import com.zzang.chongdae.offering.domain.offeringfetchstrategy.HighDiscountOfferingStrategy;
import com.zzang.chongdae.offering.domain.offeringfetchstrategy.ImminentOfferingStrategy;
import com.zzang.chongdae.offering.domain.offeringfetchstrategy.JoinableOfferingStrategy;
import com.zzang.chongdae.offering.domain.offeringfetchstrategy.OfferingFetchStrategy;
import com.zzang.chongdae.offering.domain.offeringfetchstrategy.RecentOfferingStrategy;
import com.zzang.chongdae.offering.exception.OfferingErrorCode;
Expand All @@ -27,7 +28,8 @@ public OfferingFetcher(OfferingRepository offeringRepository) {
this.strategyMap = Map.of(
OfferingFilter.RECENT, new RecentOfferingStrategy(offeringRepository),
OfferingFilter.IMMINENT, new ImminentOfferingStrategy(offeringRepository),
OfferingFilter.HIGH_DISCOUNT, new HighDiscountOfferingStrategy(offeringRepository)
OfferingFilter.HIGH_DISCOUNT, new HighDiscountOfferingStrategy(offeringRepository),
OfferingFilter.JOINABLE, new JoinableOfferingStrategy(offeringRepository)
);
}

Expand Down