diff --git a/src/main/java/solitour_backend/solitour/gathering/repository/GatheringRepositoryImpl.java b/src/main/java/solitour_backend/solitour/gathering/repository/GatheringRepositoryImpl.java index bc9a1d9..340508c 100644 --- a/src/main/java/solitour_backend/solitour/gathering/repository/GatheringRepositoryImpl.java +++ b/src/main/java/solitour_backend/solitour/gathering/repository/GatheringRepositoryImpl.java @@ -51,14 +51,11 @@ public GatheringRepositoryImpl() { @Override public List getGatheringRecommend(Long gatheringId, Long gatheringCategoryId, Long userId) { - + NumberExpression likeCount = countGreatGatheringByGatheringById(); + NumberExpression applicantsCount = applicantsCountNowConsent(); return from(gathering) .join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id)) .leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id)) - .leftJoin(bookMarkGathering) - .on(bookMarkGathering.gathering.id.eq(gathering.id).and(bookMarkGathering.user.id.eq(userId))) - .leftJoin(category).on(category.id.eq(gathering.gatheringCategory.id)) - .leftJoin(gatheringApplicants).on(gatheringApplicants.gathering.id.eq(gathering.id).and(gatheringApplicants.gatheringStatus.eq(GatheringStatus.CONSENT))) .where(gathering.isFinish.eq(Boolean.FALSE) .and(gathering.gatheringCategory.id.eq(gatheringCategoryId)) .and(gathering.id.ne(gatheringId)) @@ -78,9 +75,9 @@ public List getGatheringRecommend(Long gatheringId, Long zoneCategoryParent.name, zoneCategoryChild.name, gathering.viewCount, - bookMarkGathering.user.id.isNotNull(), - countGreatGatheringByGatheringById(), - category.name, + isGatheringBookmark(userId), + likeCount, + gathering.gatheringCategory.name, gathering.user.name, gathering.scheduleStartDate, gathering.scheduleEndDate, @@ -89,7 +86,7 @@ public List getGatheringRecommend(Long gatheringId, Long gathering.startAge, gathering.endAge, gathering.personCount, - gatheringApplicants.count().coalesce(0L).intValue(), + applicantsCount, isUserGreatGathering(userId) )).limit(3L).fetch(); } @@ -104,23 +101,15 @@ public Page getGatheringPageFilterAndOrder(Pageable page OrderSpecifier orderSpecifier = getOrderSpecifier(gatheringPageRequest.getSort()); NumberExpression countGreatGathering = countGreatGatheringByGatheringById(); + NumberExpression applicantsCount = applicantsCountNowConsent(); long total = from(gathering) - .join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id)) - .leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id)) - .leftJoin(bookMarkGathering) - .on(bookMarkGathering.gathering.id.eq(gathering.id).and(bookMarkGathering.user.id.eq(userId))) - .leftJoin(gatheringApplicants).on(gatheringApplicants.gathering.id.eq(gathering.id)) .where(booleanBuilder) - .select(gathering.id.countDistinct()).fetchCount(); + .select(gathering.id).fetchCount(); List content = from(gathering) - .distinct() .join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id)) .leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id)) - .leftJoin(bookMarkGathering) - .on(bookMarkGathering.gathering.id.eq(gathering.id).and(bookMarkGathering.user.id.eq(userId))) - .leftJoin(gatheringApplicants).on(gatheringApplicants.gathering.id.eq(gathering.id).and(gatheringApplicants.gatheringStatus.eq(GatheringStatus.CONSENT))) .where(booleanBuilder) .groupBy(gathering.id, zoneCategoryChild.id, zoneCategoryParent.id, category.id, gathering.title, gathering.viewCount, gathering.user.name, @@ -135,7 +124,7 @@ public Page getGatheringPageFilterAndOrder(Pageable page zoneCategoryParent.name, zoneCategoryChild.name, gathering.viewCount, - bookMarkGathering.user.id.isNotNull(), + isGatheringBookmark(userId), countGreatGathering, gathering.gatheringCategory.name, gathering.user.name, @@ -146,7 +135,7 @@ public Page getGatheringPageFilterAndOrder(Pageable page gathering.startAge, gathering.endAge, gathering.personCount, - gatheringApplicants.count().coalesce(0L).intValue(), + applicantsCount, isUserGreatGathering(userId) )) .offset(pageable.getOffset()) @@ -236,13 +225,10 @@ public List getGatheringRankList() { @Override public List getGatheringLikeCountFromCreatedIn3(Long userId) { NumberExpression likeCount = countGreatGatheringByGatheringById(); + NumberExpression applicantsCount = applicantsCountNowConsent(); return from(gathering) - .distinct() .join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id)) .leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id)) - .leftJoin(bookMarkGathering).on(bookMarkGathering.gathering.id.eq(gathering.id).and(bookMarkGathering.user.id.eq(userId))) - .leftJoin(category).on(category.id.eq(gathering.gatheringCategory.id)) - .leftJoin(gatheringApplicants).on(gatheringApplicants.gathering.id.eq(gathering.id).and(gatheringApplicants.gatheringStatus.eq(GatheringStatus.CONSENT))) .where(gathering.isFinish.eq(Boolean.FALSE) .and(gathering.isDeleted.eq(Boolean.FALSE)) .and(gathering.createdAt.after(LocalDateTime.now().minusMonths(3))) @@ -260,7 +246,7 @@ public List getGatheringLikeCountFromCreatedIn3(Long use zoneCategoryParent.name, zoneCategoryChild.name, gathering.viewCount, - bookMarkGathering.user.id.isNotNull(), + isGatheringBookmark(userId), likeCount, gathering.gatheringCategory.name, gathering.user.name, @@ -271,7 +257,7 @@ public List getGatheringLikeCountFromCreatedIn3(Long use gathering.startAge, gathering.endAge, gathering.personCount, - gatheringApplicants.count().coalesce(0L).intValue(), + applicantsCount, isUserGreatGathering(userId) )).limit(6L).fetch(); } @@ -358,4 +344,26 @@ private BooleanExpression isUserGreatGathering(Long userId) { .otherwise(false); } + private BooleanExpression isGatheringBookmark(Long userId) { + return new CaseBuilder() + .when(JPAExpressions.selectOne() + .from(bookMarkGathering) + .where(bookMarkGathering.gathering.id.eq(gathering.id) + .and(bookMarkGathering.user.id.eq(userId))) + .exists()) + .then(true) + .otherwise(false); + } + + private NumberExpression applicantsCountNowConsent() { + JPQLQuery applicantsCountSubQuery = JPAExpressions + .select(gatheringApplicants.count().intValue()) + .from(gatheringApplicants) + .where(gatheringApplicants.gathering.id.eq(gathering.id) + .and(gatheringApplicants.gatheringStatus.eq(GatheringStatus.CONSENT))); + + return Expressions.numberTemplate(Integer.class, "{0}", applicantsCountSubQuery) + .coalesce(0); + } + }