Skip to content

Commit

Permalink
Merge pull request #217 from CoolPeace-yanolza/feature/coupon/search
Browse files Browse the repository at this point in the history
쿠폰 조회 관련 싱크업
  • Loading branch information
tkddn204 authored Jan 27, 2024
2 parents 22571aa + bef487f commit 417d789
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ResponseEntity<CouponSearchResponse> searchCoupons(
Page<CouponResponse> couponResponses = couponService.searchCoupons(
Long.valueOf(jwtPrincipal.getMemberId()), accommodationId, searchCouponParams, pageable);
CouponCategoryResponse categoryResponse = couponService.getCouponCategories(
Long.valueOf(jwtPrincipal.getMemberId()), accommodationId);
Long.valueOf(jwtPrincipal.getMemberId()), accommodationId, searchCouponParams);
return ResponseEntity.ok(CouponSearchResponse.from(couponResponses, categoryResponse));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface CouponRepositoryCustom {

Page<Coupon> findAllCoupons(Long memberId, Long accommodationId, SearchCouponParams params, PageRequest pageable);

Map<CouponStatusType, Long> countCouponsByCouponStatus(Long memberId, Long accommodationId);
Map<CouponStatusType, Long> countCouponsByCouponStatus(Long memberId, Long accommodationId, SearchCouponParams params);

List<Coupon> findRecentCouponByMemberId(Long memberId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public CouponRepositoryImpl(JPAQueryFactory jpaQueryFactory) {
@Override
public Page<Coupon> findAllCoupons(Long memberId, Long accommodationId, SearchCouponParams params, PageRequest pageable) {
// 검색 필터링
BooleanBuilder searchCouponPredicate = new BooleanBuilder(coupon.member.id.eq(memberId));
BooleanBuilder searchCouponPredicate = new BooleanBuilder(
coupon.accommodation.id.eq(accommodationId).and(coupon.member.id.eq(memberId))
);

// 쿠폰 상태
if (params.status() != null) {
Expand All @@ -62,37 +64,32 @@ public Page<Coupon> findAllCoupons(Long memberId, Long accommodationId, SearchCo
}

// 쿠폰 날짜
if (params.date() != null) {
searchCouponPredicate.and(switch (ValuedEnum.of(SearchCouponDateFilterType.class, params.date())) {
case THREE_MONTHS -> coupon.createdAt.after(LocalDateTime.now().minusMonths(3));
case SIX_MONTHS -> coupon.createdAt.after(LocalDateTime.now().minusMonths(6));
case YEAR -> coupon.createdAt.after(LocalDateTime.now().minusYears(1));
});
} else {
searchCouponPredicate.and(coupon.createdAt.after(LocalDateTime.now().minusYears(1)));
}
putSearchCouponParamsDatePredicate(params, searchCouponPredicate);

JPAQuery<Coupon> couponJPAQuery = jpaQueryFactory.selectFrom(coupon)
.leftJoin(coupon.couponRooms, couponRooms).fetchJoin()
.leftJoin(couponRooms.room, room).fetchJoin()
.where(coupon.accommodation.id.eq(accommodationId))
.where(searchCouponPredicate);

JPAQuery<Long> totalQuery = jpaQueryFactory.select(coupon.count()).from(coupon)
.where(coupon.accommodation.id.eq(accommodationId))
.where(searchCouponPredicate);

List<Coupon> coupons = Objects.requireNonNull(getQuerydsl()).applyPagination(pageable, couponJPAQuery).fetch();
return PageableExecutionUtils.getPage(coupons, pageable, totalQuery::fetchOne);
}

@Override
public Map<CouponStatusType, Long> countCouponsByCouponStatus(Long memberId, Long accommodationId) {
public Map<CouponStatusType, Long> countCouponsByCouponStatus(Long memberId, Long accommodationId,
SearchCouponParams params) {
BooleanBuilder searchCouponPredicate = new BooleanBuilder(
coupon.accommodation.id.eq(accommodationId).and(coupon.member.id.eq(memberId))
);

putSearchCouponParamsDatePredicate(params, searchCouponPredicate);

List<Tuple> results = jpaQueryFactory
.select(coupon.couponStatus, coupon.count())
.where(coupon.member.id.eq(memberId)
.and(coupon.accommodation.id.eq(accommodationId)))
.where(searchCouponPredicate)
.from(coupon)
.groupBy(coupon.couponStatus)
.fetch();
Expand All @@ -104,6 +101,18 @@ public Map<CouponStatusType, Long> countCouponsByCouponStatus(Long memberId, Lon
));
}

private static void putSearchCouponParamsDatePredicate(SearchCouponParams params, BooleanBuilder searchCouponPredicate) {
if (params.date() != null) {
searchCouponPredicate.and(switch (ValuedEnum.of(SearchCouponDateFilterType.class, params.date())) {
case THREE_MONTHS -> coupon.createdAt.after(LocalDateTime.now().minusMonths(3));
case SIX_MONTHS -> coupon.createdAt.after(LocalDateTime.now().minusMonths(6));
case YEAR -> coupon.createdAt.after(LocalDateTime.now().minusYears(1));
});
} else {
searchCouponPredicate.and(coupon.createdAt.after(LocalDateTime.now().minusYears(1)));
}
}

@Override
public List<Coupon> findRecentCouponByMemberId(Long memberId) {
return jpaQueryFactory.selectFrom(coupon)
Expand Down Expand Up @@ -162,31 +171,31 @@ public List<Coupon> expiration3days(Long memberId, Long accommodationId) {
.where(coupon.member.id.eq(memberId)
.and(coupon.accommodation.id.eq(accommodationId))
.and(coupon.couponStatus.ne(DELETED))
.and(coupon.exposureEndDate.between(LocalDate.now().minusDays(1),
LocalDate.now().plusDays(4))))
.and(coupon.exposureEndDate.between(LocalDate.now().minusDays(1),
LocalDate.now().plusDays(4))))
.fetch();
}

@Override
public List<Coupon> findAllByExposureDate(Accommodation accommodation, LocalDate localDate) {
return jpaQueryFactory.selectFrom(coupon)
.where(coupon.accommodation.eq(accommodation)
.and(coupon.exposureStartDate.before(localDate))
.and(coupon.exposureEndDate.after(localDate))).fetch();
.where(coupon.accommodation.eq(accommodation)
.and(coupon.exposureStartDate.before(localDate))
.and(coupon.exposureEndDate.after(localDate))).fetch();
}

@Override
public List<Coupon> endExposureCoupons(LocalDate nowDate) {
return jpaQueryFactory.selectFrom(coupon)
.where(coupon.couponStatus.ne(CouponStatusType.DELETED)
.and(coupon.exposureEndDate.before(nowDate))).fetch();
.where(coupon.couponStatus.ne(CouponStatusType.DELETED)
.and(coupon.exposureEndDate.before(nowDate))).fetch();
}

@Override
public List<Coupon> startExposureCoupons(LocalDate nowDate) {
return jpaQueryFactory.selectFrom(coupon)
.where(coupon.couponStatus.eq(CouponStatusType.EXPOSURE_WAIT)
.and((coupon.exposureStartDate.before(nowDate))
.or(coupon.exposureStartDate.eq(nowDate)))).fetch();
.where(coupon.couponStatus.eq(CouponStatusType.EXPOSURE_WAIT)
.and((coupon.exposureStartDate.before(nowDate))
.or(coupon.exposureStartDate.eq(nowDate)))).fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ public Page<CouponResponse> searchCoupons(Long memberId, Long accommodationId,
}

@Transactional(readOnly = true)
public CouponCategoryResponse getCouponCategories(Long memberId, Long accommodationId) {
public CouponCategoryResponse getCouponCategories(Long memberId, Long accommodationId,
SearchCouponParams searchCouponParams) {
Map<CouponStatusType, Long> counts = couponRepository.countCouponsByCouponStatus(memberId,
accommodationId);

accommodationId, searchCouponParams);
long all = counts.values().stream().mapToLong(Long::longValue).sum()
- counts.getOrDefault(CouponStatusType.DELETED, 0L);
long exposureOn = counts.getOrDefault(CouponStatusType.EXPOSURE_ON, 0L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public enum ErrorCode {
COUPON_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 쿠폰의 정보를 찾을 수 없습니다."),
COUPON_ACCESS_DENIED(HttpStatus.FORBIDDEN, "해당 쿠폰에 대한 편집 권한이 없습니다."),
COUPON_UPDATE_LIMIT_EXPOSURE_STATE(HttpStatus.BAD_REQUEST, "노출 여부 변경은 노출 ON과 노출 OFF만 가능합니다."),
INVALID_COUPON_STATE_OUTSIDE_EXPOSURE_DATE(HttpStatus.BAD_REQUEST, "노출 날짜 기간 이내에만 ON/OFF일 수 있습니다."),
INVALID_COUPON_STATE_OUTSIDE_EXPOSURE_DATE(HttpStatus.BAD_REQUEST, "노출 날짜 기간 이내에만 ON/OFF일 수 있습니다."),
INVALID_COUPON_STATE_WAIT_EXPOSURE_DATE(HttpStatus.BAD_REQUEST, "노출 날짜 기간 이전에만 대기중일 수 있습니다."),
INVALID_COUPON_STATE_END_EXPOSURE_DATE(HttpStatus.BAD_REQUEST, "노출 날짜 기간 이후에만 종료할 수 있습니다."),

Expand Down

0 comments on commit 417d789

Please sign in to comment.