From a7e5ba3585a76b1a0a2f79994dc0f2baf496247f Mon Sep 17 00:00:00 2001 From: hyeonjaez Date: Sun, 22 Sep 2024 20:32:33 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=88=98?= =?UTF-8?q?=20=EA=B5=AC=ED=95=98=EB=8A=94=20=EC=A0=88=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/GatheringRepositoryImpl.java | 53 +++++++------------ .../repository/InformationRepositoryImpl.java | 41 ++++++-------- 2 files changed, 33 insertions(+), 61 deletions(-) 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 c614e7f..b1a0fac 100644 --- a/src/main/java/solitour_backend/solitour/gathering/repository/GatheringRepositoryImpl.java +++ b/src/main/java/solitour_backend/solitour/gathering/repository/GatheringRepositoryImpl.java @@ -3,11 +3,7 @@ import com.querydsl.core.BooleanBuilder; import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.Projections; -import com.querydsl.core.types.dsl.BooleanExpression; -import com.querydsl.core.types.dsl.CaseBuilder; -import com.querydsl.core.types.dsl.Expressions; -import com.querydsl.core.types.dsl.NumberExpression; -import com.querydsl.core.types.dsl.PathBuilder; +import com.querydsl.core.types.dsl.*; import com.querydsl.jpa.JPAExpressions; import com.querydsl.jpa.JPQLQuery; @@ -51,8 +47,6 @@ 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)) @@ -76,7 +70,7 @@ public List getGatheringRecommend(Long gatheringId, Long zoneCategoryChild.name, gathering.viewCount, isGatheringBookmark(userId), - likeCount, + countGreatGatheringByGatheringById(gathering.id), gathering.gatheringCategory.name, gathering.user.nickname, gathering.scheduleStartDate, @@ -86,7 +80,7 @@ public List getGatheringRecommend(Long gatheringId, Long gathering.startAge, gathering.endAge, gathering.personCount, - applicantsCount, + applicantsCountNowConsent(gathering.id), isUserGreatGathering(userId) )).limit(3L).fetch(); } @@ -98,11 +92,6 @@ public Page getGatheringPageFilterAndOrder(Pageable page Long userId) { BooleanBuilder booleanBuilder = makeWhereSQL(gatheringPageRequest); - OrderSpecifier orderSpecifier = getOrderSpecifier(gatheringPageRequest.getSort()); - - NumberExpression countGreatGathering = countGreatGatheringByGatheringById(); - NumberExpression applicantsCount = applicantsCountNowConsent(); - long total = from(gathering) .where(booleanBuilder) .select(gathering.id).fetchCount(); @@ -116,7 +105,7 @@ public Page getGatheringPageFilterAndOrder(Pageable page gathering.scheduleStartDate, gathering.scheduleEndDate, gathering.deadline, gathering.allowedSex, gathering.startAge, gathering.endAge, gathering.personCount) - .orderBy(orderSpecifier) + .orderBy(getOrderSpecifier(gatheringPageRequest.getSort(), gathering.id)) .select(Projections.constructor( GatheringBriefResponse.class, gathering.id, @@ -125,7 +114,7 @@ public Page getGatheringPageFilterAndOrder(Pageable page zoneCategoryChild.name, gathering.viewCount, isGatheringBookmark(userId), - countGreatGathering, + countGreatGatheringByGatheringById(gathering.id), gathering.gatheringCategory.name, gathering.user.nickname, gathering.scheduleStartDate, @@ -135,7 +124,7 @@ public Page getGatheringPageFilterAndOrder(Pageable page gathering.startAge, gathering.endAge, gathering.personCount, - applicantsCount, + applicantsCountNowConsent(gathering.id), isUserGreatGathering(userId) )) .offset(pageable.getOffset()) @@ -151,10 +140,6 @@ public Page getPageGatheringByTag(Pageable pageable, String decodedTag) { BooleanBuilder booleanBuilder = makeWhereSQL(gatheringPageRequest); - OrderSpecifier orderSpecifier = getOrderSpecifier(gatheringPageRequest.getSort()); - - NumberExpression countGreatGathering = countGreatGatheringByGatheringById(); - long total = from(gathering) .join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id)) .leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id)) @@ -176,7 +161,7 @@ public Page getPageGatheringByTag(Pageable pageable, .on(gatheringTag.gathering.id.eq(gathering.id).and(gatheringTag.tag.name.eq(decodedTag))) .where(booleanBuilder.and(gatheringTag.tag.name.eq(decodedTag))) .groupBy(gathering.id, zoneCategoryChild.id, zoneCategoryParent.id, category.id) - .orderBy(orderSpecifier) + .orderBy(getOrderSpecifier(gatheringPageRequest.getSort(), gathering.id)) .select(Projections.constructor( GatheringBriefResponse.class, gathering.id, @@ -185,7 +170,7 @@ public Page getPageGatheringByTag(Pageable pageable, zoneCategoryChild.name, gathering.viewCount, bookMarkGathering.user.id.isNotNull(), - countGreatGathering, + countGreatGatheringByGatheringById(gathering.id), gathering.gatheringCategory.name, gathering.user.name, gathering.scheduleStartDate, @@ -208,7 +193,7 @@ public Page getPageGatheringByTag(Pageable pageable, @Override public List getGatheringRankList() { return from(gathering) - .orderBy(countGreatGatheringByGatheringById().desc()) + .orderBy(countGreatGatheringByGatheringById(gathering.id).desc()) .groupBy(gathering.id, gathering.title) .where(gathering.isFinish.eq(Boolean.FALSE) .and(gathering.isDeleted.eq(Boolean.FALSE)) @@ -224,8 +209,6 @@ public List getGatheringRankList() { @Override public List getGatheringLikeCountFromCreatedIn3(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)) @@ -238,7 +221,7 @@ public List getGatheringLikeCountFromCreatedIn3(Long use gathering.scheduleStartDate, gathering.scheduleEndDate, gathering.deadline, gathering.allowedSex, gathering.startAge, gathering.endAge, gathering.personCount) - .orderBy(likeCount.desc()) + .orderBy(countGreatGatheringByGatheringById(gathering.id).desc()) .select(Projections.constructor( GatheringBriefResponse.class, gathering.id, @@ -247,7 +230,7 @@ public List getGatheringLikeCountFromCreatedIn3(Long use zoneCategoryChild.name, gathering.viewCount, isGatheringBookmark(userId), - likeCount, + countGreatGatheringByGatheringById(gathering.id), gathering.gatheringCategory.name, gathering.user.nickname, gathering.scheduleStartDate, @@ -257,7 +240,7 @@ public List getGatheringLikeCountFromCreatedIn3(Long use gathering.startAge, gathering.endAge, gathering.personCount, - applicantsCount, + applicantsCountNowConsent(gathering.id), isUserGreatGathering(userId) )).limit(6L).fetch(); } @@ -306,12 +289,12 @@ private BooleanBuilder makeWhereSQL(GatheringPageRequest gatheringPageRequest) { } // 정렬 방식 - private OrderSpecifier getOrderSpecifier(String sort) { + private OrderSpecifier getOrderSpecifier(String sort, NumberPath gatheringId) { PathBuilder entityPath = new PathBuilder<>(Gathering.class, "gathering"); if (Objects.nonNull(sort)) { if (LIKE_COUNT_SORT.equalsIgnoreCase(sort)) { - return countGreatGatheringByGatheringById().desc(); + return countGreatGatheringByGatheringById(gatheringId).desc(); } else if (VIEW_COUNT_SORT.equalsIgnoreCase(sort)) { return entityPath.getNumber("viewCount", Integer.class).desc(); } @@ -321,12 +304,12 @@ private OrderSpecifier getOrderSpecifier(String sort) { } // 좋아요 수 가져오는 식 - private NumberExpression countGreatGatheringByGatheringById() { + private NumberExpression countGreatGatheringByGatheringById(NumberPath gatheringId) { QGreatGathering greatGatheringSub = QGreatGathering.greatGathering; JPQLQuery likeCountSubQuery = JPAExpressions .select(greatGatheringSub.count()) .from(greatGatheringSub) - .where(greatGatheringSub.gathering.id.eq(gathering.id)); + .where(greatGatheringSub.gathering.id.eq(gatheringId)); return Expressions.numberTemplate(Long.class, "{0}", likeCountSubQuery) .coalesce(0L) @@ -355,11 +338,11 @@ private BooleanExpression isGatheringBookmark(Long userId) { .otherwise(false); } - private NumberExpression applicantsCountNowConsent() { + private NumberExpression applicantsCountNowConsent(NumberPath gatheringId) { JPQLQuery applicantsCountSubQuery = JPAExpressions .select(gatheringApplicants.count().intValue()) .from(gatheringApplicants) - .where(gatheringApplicants.gathering.id.eq(gathering.id) + .where(gatheringApplicants.gathering.id.eq(gatheringId) .and(gatheringApplicants.gatheringStatus.eq(GatheringStatus.CONSENT))); return Expressions.numberTemplate(Integer.class, "{0}", applicantsCountSubQuery) diff --git a/src/main/java/solitour_backend/solitour/information/repository/InformationRepositoryImpl.java b/src/main/java/solitour_backend/solitour/information/repository/InformationRepositoryImpl.java index 568320b..8bdb852 100644 --- a/src/main/java/solitour_backend/solitour/information/repository/InformationRepositoryImpl.java +++ b/src/main/java/solitour_backend/solitour/information/repository/InformationRepositoryImpl.java @@ -3,10 +3,7 @@ import com.querydsl.core.BooleanBuilder; import com.querydsl.core.types.OrderSpecifier; import com.querydsl.core.types.Projections; -import com.querydsl.core.types.dsl.BooleanExpression; -import com.querydsl.core.types.dsl.CaseBuilder; -import com.querydsl.core.types.dsl.Expressions; -import com.querydsl.core.types.dsl.NumberExpression; +import com.querydsl.core.types.dsl.*; import com.querydsl.jpa.JPAExpressions; import com.querydsl.jpa.JPQLQuery; @@ -72,8 +69,6 @@ public Page getInformationPageFilterAndOrder(Pageable whereClause.and(information.title.trim().containsIgnoreCase(searchKeyword)); } - OrderSpecifier orderSpecifier = getOrderSpecifier(informationPageRequest.getSort()); - NumberExpression countGreatInformation = countGreatInformationByInformationById(); long total = from(information) .where(whereClause) @@ -87,7 +82,7 @@ public Page getInformationPageFilterAndOrder(Pageable .join(category).on(category.id.eq(information.category.id).and(categoryCondition)) .where(whereClause) .groupBy(information.id, zoneCategoryChild.id, zoneCategoryParent.id, image.id) - .orderBy(orderSpecifier) + .orderBy(getOrderSpecifier(informationPageRequest.getSort(), information.id)) .select(Projections.constructor( InformationBriefResponse.class, information.id, @@ -98,7 +93,7 @@ public Page getInformationPageFilterAndOrder(Pageable information.viewCount, isInformationBookmark(userId), image.address, - countGreatInformation, + countGreatInformationByInformationById(information.id), isUserGreatInformation(userId) )).offset(pageable.getOffset()) .limit(pageable.getPageSize()) @@ -113,13 +108,12 @@ public List getInformationLikeCountFromCreatedIn3(Long return from(information) .leftJoin(zoneCategoryChild).on(zoneCategoryChild.id.eq(information.zoneCategory.id)) .leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id)) - .leftJoin(image) - .on(image.information.id.eq(information.id).and(image.imageStatus.eq(ImageStatus.THUMBNAIL))) + .leftJoin(image).on(image.information.id.eq(information.id).and(image.imageStatus.eq(ImageStatus.THUMBNAIL))) .leftJoin(category).on(category.id.eq(information.category.id)) .where(information.createdDate.after(LocalDateTime.now().minusMonths(3))) .groupBy(information.id, information.title, zoneCategoryParent.name, zoneCategoryChild.name, bookMarkInformation.id, image.address) - .orderBy(countGreatInformationByInformationById().desc()) + .orderBy(countGreatInformationByInformationById(information.id).desc()) // 파라미터로 information.id 전달 .select(Projections.constructor( InformationMainResponse.class, information.id, @@ -130,10 +124,9 @@ public List getInformationLikeCountFromCreatedIn3(Long information.viewCount, isInformationBookmark(userId), image.address, - countGreatInformationByInformationById(), + countGreatInformationByInformationById(information.id), // 파라미터 전달 isUserGreatInformation(userId) )).limit(6).fetch(); - } @Override @@ -157,7 +150,7 @@ public List getInformationRecommend(Long informationId information.viewCount, isInformationBookmark(userId), image.address, - countGreatInformationByInformationById(), + countGreatInformationByInformationById(information.id), isUserGreatInformation(userId) )) .limit(3L) @@ -183,8 +176,6 @@ public Page getInformationPageByTag(Pageable pageable, categoryCondition.and(category.parentCategory.id.eq(parentCategoryId)); } - OrderSpecifier orderSpecifier = getOrderSpecifier(informationPageRequest.getSort()); - NumberExpression countGreatInformation = countGreatInformationByInformationById(); long total = from(information) .join(zoneCategoryChild).on(zoneCategoryChild.id.eq(information.zoneCategory.id)) @@ -212,7 +203,7 @@ public Page getInformationPageByTag(Pageable pageable, .on(infoTag.information.id.eq(information.id)) .where(whereClause) .groupBy(information.id, zoneCategoryChild.id, zoneCategoryParent.id, image.id, infoTag.id) - .orderBy(orderSpecifier) + .orderBy(getOrderSpecifier(informationPageRequest.getSort(), information.id)) .select(Projections.constructor( InformationBriefResponse.class, information.id, @@ -223,7 +214,7 @@ public Page getInformationPageByTag(Pageable pageable, information.viewCount, bookMarkInformation.user.id.isNotNull(), image.address, - countGreatInformation, + countGreatInformationByInformationById(information.id), isUserGreatInformation(userId) )).offset(pageable.getOffset()) .limit(pageable.getPageSize()) @@ -238,7 +229,7 @@ public List getInformationRank() { .leftJoin(greatInformation) .on(greatInformation.information.id.eq(information.id)) .groupBy(information.id, information.title) - .orderBy(countGreatInformationByInformationById().desc()) + .orderBy(countGreatInformationByInformationById(information.id).desc()) .limit(5) .select(Projections.constructor( InformationRankResponse.class, @@ -247,10 +238,10 @@ public List getInformationRank() { )).fetch(); } - private OrderSpecifier getOrderSpecifier(String sort) { + private OrderSpecifier getOrderSpecifier(String sort, NumberPath informationId) { if (Objects.nonNull(sort)) { if (Objects.equals(LIKE_COUNT_SORT, sort)) { - return countGreatInformationByInformationById().desc(); + return countGreatInformationByInformationById(informationId).desc(); } else if (Objects.equals(VIEW_COUNT_SORT, sort)) { return information.viewCount.desc(); } @@ -258,16 +249,14 @@ private OrderSpecifier getOrderSpecifier(String sort) { return information.createdDate.desc(); } - private NumberExpression countGreatInformationByInformationById() { + private NumberExpression countGreatInformationByInformationById(NumberPath informationId) { QGreatInformation greatInformationSub = QGreatInformation.greatInformation; JPQLQuery likeCountSubQuery = JPAExpressions .select(greatInformationSub.count()) .from(greatInformationSub) - .where(greatInformationSub.information.id.eq(information.id)); + .where(greatInformationSub.information.id.eq(informationId)); // 파라미터로 받은 NumberPath와 비교 - return Expressions.numberTemplate(Long.class, "{0}", likeCountSubQuery) - .coalesce(0L) - .intValue(); + return Expressions.asNumber(likeCountSubQuery).longValue(); // 명확하게 Long 타입 반환 } private BooleanExpression isUserGreatInformation(Long userId) {