Skip to content

Commit

Permalink
Merge pull request #167 from CoolPeace-yanolza/fix/coupon/category
Browse files Browse the repository at this point in the history
쿠폰 카테고리 개수 조회 쿼리 수정
  • Loading branch information
tkddn204 authored Jan 22, 2024
2 parents 6c256a7 + 327169e commit 3589cec
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public ResponseEntity<CouponSearchResponse> searchCoupons(
) {
Page<CouponResponse> couponResponses = couponService.searchCoupons(
Long.valueOf(jwtPrincipal.getMemberId()), accommodationId, searchCouponParams, pageable);
CouponCategoryResponse categoryResponse = couponService.getCouponCategories();
CouponCategoryResponse categoryResponse = couponService.getCouponCategories(
Long.valueOf(jwtPrincipal.getMemberId()), accommodationId);
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();
Map<CouponStatusType, Long> countCouponsByCouponStatus(Long memberId, Long accommodationId);

List<Coupon> findRecentCouponByMemberId(Long memberId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@

import static com.coolpeace.domain.coupon.entity.QCoupon.coupon;
import static com.coolpeace.domain.coupon.entity.QCouponRooms.couponRooms;
import static com.coolpeace.domain.coupon.entity.type.CouponStatusType.EXPOSURE_OFF;
import static com.coolpeace.domain.coupon.entity.type.CouponStatusType.EXPOSURE_ON;
import static com.coolpeace.domain.coupon.entity.type.CouponStatusType.*;
import static com.coolpeace.domain.room.entity.QRoom.room;

public class CouponRepositoryImpl extends QuerydslRepositorySupport implements CouponRepositoryCustom {
Expand All @@ -49,13 +48,13 @@ public Page<Coupon> findAllCoupons(Long memberId, Long accommodationId, SearchCo
searchCouponPredicate.and(switch (ValuedEnum.of(SearchCouponStatusFilterType.class, params.status())) {
case EXPOSURE_ON -> coupon.couponStatus.eq(EXPOSURE_ON);
case EXPOSURE_OFF -> coupon.couponStatus.eq(EXPOSURE_OFF)
.or(coupon.couponStatus.eq(CouponStatusType.EXPOSURE_WAIT));
case EXPIRED -> coupon.couponStatus.eq(CouponStatusType.EXPOSURE_END)
.or(coupon.couponStatus.eq(CouponStatusType.DELETED));
case All -> coupon.couponStatus.ne(CouponStatusType.DELETED);
.or(coupon.couponStatus.eq(EXPOSURE_WAIT));
case EXPIRED -> coupon.couponStatus.eq(EXPOSURE_END)
.or(coupon.couponStatus.eq(DELETED));
case All -> coupon.couponStatus.ne(DELETED);
});
} else {
searchCouponPredicate.and(coupon.couponStatus.ne(CouponStatusType.DELETED));
searchCouponPredicate.and(coupon.couponStatus.ne(DELETED));
}

// 쿠폰 이름
Expand All @@ -70,6 +69,8 @@ public Page<Coupon> findAllCoupons(Long memberId, Long accommodationId, SearchCo
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)));
}

JPAQuery<Coupon> couponJPAQuery = jpaQueryFactory.selectFrom(coupon)
Expand All @@ -87,9 +88,12 @@ public Page<Coupon> findAllCoupons(Long memberId, Long accommodationId, SearchCo
}

@Override
public Map<CouponStatusType, Long> countCouponsByCouponStatus() {
public Map<CouponStatusType, Long> countCouponsByCouponStatus(Long memberId, Long accommodationId) {

List<Tuple> results = jpaQueryFactory
.select(coupon.couponStatus, coupon.count())
.where(coupon.member.id.eq(memberId)
.and(coupon.accommodation.id.eq(accommodationId)))
.from(coupon)
.groupBy(coupon.couponStatus)
.fetch();
Expand All @@ -107,7 +111,7 @@ public List<Coupon> findRecentCouponByMemberId(Long memberId) {
.leftJoin(coupon.couponRooms, couponRooms).fetchJoin()
.leftJoin(couponRooms.room, room).fetchJoin()
.where(coupon.member.id.eq(memberId),
coupon.couponStatus.eq(CouponStatusType.EXPOSURE_END))
coupon.couponStatus.eq(EXPOSURE_END))
.orderBy(coupon.createdAt.desc())
.limit(6)
.fetch();
Expand Down Expand Up @@ -138,7 +142,7 @@ public Boolean noRegister(Long memberId, Long accommodationId) {
return jpaQueryFactory.selectFrom(coupon)
.where(coupon.member.id.eq(memberId)
.and(coupon.accommodation.id.eq(accommodationId))
.and(coupon.couponStatus.ne(CouponStatusType.DELETED)))
.and(coupon.couponStatus.ne(DELETED)))
.fetch().isEmpty();
}

Expand All @@ -158,7 +162,7 @@ public List<Coupon> expiration3days(Long memberId, Long accommodationId) {
return jpaQueryFactory.selectFrom(coupon)
.where(coupon.member.id.eq(memberId)
.and(coupon.accommodation.id.eq(accommodationId))
.and(coupon.couponStatus.ne(CouponStatusType.DELETED))
.and(coupon.couponStatus.ne(DELETED))
.and(coupon.exposureEndDate.before(LocalDate.now().plusDays(3))))
.fetch();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public Page<CouponResponse> searchCoupons(Long memberId, Long accommodationId, S
}

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

long all = counts.values().stream().mapToLong(Long::longValue).sum();
long exposureOn = counts.getOrDefault(CouponStatusType.EXPOSURE_ON, 0L);
Expand Down

0 comments on commit 3589cec

Please sign in to comment.