Skip to content

Commit

Permalink
refactor: 공모글 목록 조회 필터링 수정 및 추가 (#356)
Browse files Browse the repository at this point in the history
* refactor: 마감임박순 필터링 이름 마감임박만으로 변경

Co-authored-by: fromitive <[email protected]>

* refactor: 필터링 쿼리 수정

Co-authored-by: fromitive <[email protected]>

* feat: "참여가능만" 필터링 기능 구현

Co-authored-by: fromitive <[email protected]>

* feat: "참여가능만" 필터링 기능 연결

Co-authored-by: fromitive <[email protected]>

* fix: 쿼리 내 불필요한 파라미터 제거

Co-authored-by: fromitive <[email protected]>

* refactor: 할인율이 null일 경우 높은할인율 필터링 대상에서 제외

Co-authored-by: fromitive <[email protected]>

* feat: 참여가능만 필터링 전략 클래스 추가

* feat: 공모 목록 조회 API 응답값 변경

* fix: 높은 할인율 단위 변경 및 last-id 필터링 로직 수정

* style: 주석 제거

---------

Co-authored-by: fromitive <[email protected]>
  • Loading branch information
helenason and fromitive authored Aug 16, 2024
1 parent 01ee073 commit b7c1556
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
@AllArgsConstructor
public enum OfferingFilter {

JOINABLE("참여가능만", VISIBLE), // 처음: 참여 가능한 것 중 id가 가장 높은 값 10개 / 이후: 참여 가능한 것 중 마지막 id보다 낮은 것 x개 찾기
IMMINENT("마감임박순", VISIBLE), // 처음: 현재 시간보다 큰 것 중 가장 작은 값 10개 / 이후: lastMeetingDate 보다 큰 것 x개 찾기
HIGH_DISCOUNT("높은할인율순", VISIBLE), // 처음: 할인율(n빵가격/낱개가격) 가장 높은 값 10개 / 이후: 마지막 할인율(n빵가격/낱개가격)보다 낮은 것 x개 찾기
RECENT("최신순", INVISIBLE); // 처음: id가 가장 높은 값 10개 / 이후: 마지막 id보다 낮은 것 x개 찾기
JOINABLE("참여가능만", VISIBLE),
IMMINENT("마감임박만", VISIBLE),
HIGH_DISCOUNT("높은할인율순", VISIBLE),
RECENT("최신순", INVISIBLE);

private final String value;
private final OfferingFilterType type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public HighDiscountOfferingStrategy(OfferingRepository offeringRepository) {

@Override
protected List<OfferingEntity> fetchOfferingsWithoutLastId(String searchKeyword, Pageable pageable) {
double outOfRangeDiscountRate = 1;
double outOfRangeDiscountRate = 100;
Long outOfRangeId = findOutOfRangeId();
return offeringRepository.findHighDiscountOfferingsWithKeyword(
outOfRangeDiscountRate, outOfRangeId, searchKeyword, pageable);
Expand All @@ -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.getDiscountRate();
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
@@ -0,0 +1,25 @@
package com.zzang.chongdae.offering.domain.offeringfetchstrategy;

import com.zzang.chongdae.offering.repository.OfferingRepository;
import com.zzang.chongdae.offering.repository.entity.OfferingEntity;
import java.util.List;
import org.springframework.data.domain.Pageable;

public class JoinableOfferingStrategy extends OfferingFetchStrategy {

public JoinableOfferingStrategy(OfferingRepository offeringRepository) {
super(offeringRepository);
}

@Override
protected List<OfferingEntity> fetchOfferingsWithoutLastId(String searchKeyword, Pageable pageable) {
Long outOfRangeId = findOutOfRangeId();
return offeringRepository.findJoinableOfferingsWithKeyword(outOfRangeId, searchKeyword, pageable);
}

@Override
protected List<OfferingEntity> fetchOfferingsWithLastOffering(OfferingEntity lastOffering, String searchKeyword,
Pageable pageable) {
return offeringRepository.findJoinableOfferingsWithKeyword(lastOffering.getId(), 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,36 @@ 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%)
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 IS NOT NULL)
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ public OfferingAllResponse getAllOffering(String filterName, String searchKeywor
return new OfferingAllResponse(offerings.stream()
.map(offering -> {
OfferingPrice offeringPrice = offering.toOfferingPrice();
OfferingCondition offeringCondition = offering.toOfferingCondition();
return new OfferingAllResponseItem(offering, offeringPrice, offeringCondition);
return new OfferingAllResponseItem(offering, offeringPrice);
})
.toList()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.zzang.chongdae.offering.service.dto;

import com.zzang.chongdae.offering.domain.OfferingStatus;
import com.zzang.chongdae.offering.domain.OfferingPrice;
import com.zzang.chongdae.offering.domain.OfferingCondition;
import com.zzang.chongdae.offering.domain.OfferingStatus;
import com.zzang.chongdae.offering.repository.entity.OfferingEntity;

public record OfferingAllResponseItem(Long id,
Expand All @@ -13,21 +12,23 @@ public record OfferingAllResponseItem(Long id,
String thumbnailUrl,
Integer dividedPrice,
Integer originPrice,
Double discountRate,
OfferingStatus status,
Boolean isOpen) {

public OfferingAllResponseItem(
OfferingEntity offering, OfferingPrice offeringPrice, OfferingCondition offeringCondition) {
OfferingEntity offering, OfferingPrice offeringPrice) {
this(offering.getId(),
offering.getTitle(),
offering.getMeetingAddressDong(),
offeringCondition.getCurrentCount(),
offering.getCurrentCount(),
offering.getTotalCount(),
offering.getThumbnailUrl(),
offeringPrice.calculateDividedPrice(),
offeringPrice.getOriginPrice(),
offeringCondition.decideOfferingStatus(),
offeringCondition.isOpen()
offering.getDiscountRate(),
offering.getOfferingStatus(),
offering.getOfferingStatus().isOpen()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class GetAllOffering {
fieldWithPath("offerings[].thumbnailUrl").description("사진 링크"),
fieldWithPath("offerings[].dividedPrice").description("n빵 가격"),
fieldWithPath("offerings[].originPrice").description("원 가격"),
fieldWithPath("offerings[].discountRate").description("할인율"),
fieldWithPath("offerings[].status").description("공모 상태"
+ getEnumValuesAsString(OfferingStatus.class)),
fieldWithPath("offerings[].isOpen").description("공모 참여 가능 여부")
Expand Down

0 comments on commit b7c1556

Please sign in to comment.