From 42f64ba204992c1a18f03d34a313efa0913baaf2 Mon Sep 17 00:00:00 2001 From: hyeonjaez Date: Fri, 11 Oct 2024 11:55:19 +0900 Subject: [PATCH 1/4] =?UTF-8?q?feat:=20toString()=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../information/dto/response/InformationBriefResponse.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/solitour_backend/solitour/information/dto/response/InformationBriefResponse.java b/src/main/java/solitour_backend/solitour/information/dto/response/InformationBriefResponse.java index c833211..2478487 100644 --- a/src/main/java/solitour_backend/solitour/information/dto/response/InformationBriefResponse.java +++ b/src/main/java/solitour_backend/solitour/information/dto/response/InformationBriefResponse.java @@ -2,9 +2,11 @@ import lombok.AllArgsConstructor; import lombok.Getter; +import lombok.ToString; @Getter @AllArgsConstructor +@ToString public class InformationBriefResponse { private Long informationId; From 5f962dcc09762d47270516aeacf456455f0c242a Mon Sep 17 00:00:00 2001 From: hyeonjaez Date: Fri, 11 Oct 2024 11:55:40 +0900 Subject: [PATCH 2/4] =?UTF-8?q?fix:=20=ED=8E=98=EC=9D=B4=EC=A7=80=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=20=EB=B0=8F=20=EA=B8=B0=EC=A1=B4=EC=9D=98=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EC=A3=BC=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/InformationRepositoryCustom.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/solitour_backend/solitour/information/repository/InformationRepositoryCustom.java b/src/main/java/solitour_backend/solitour/information/repository/InformationRepositoryCustom.java index 4100ea6..0076f59 100644 --- a/src/main/java/solitour_backend/solitour/information/repository/InformationRepositoryCustom.java +++ b/src/main/java/solitour_backend/solitour/information/repository/InformationRepositoryCustom.java @@ -14,10 +14,11 @@ public interface InformationRepositoryCustom { String LIKE_COUNT_SORT = "likes"; String VIEW_COUNT_SORT = "views"; + Page getPageInformationFilterAndOrder(Pageable pageable, InformationPageRequest informationPageRequest, Long userId, Long parentCategoryId); - Page getInformationPageFilterAndOrder(Pageable pageable, - InformationPageRequest informationPageRequest, - Long userId, Long parentCategoryId); +// Page getInformationPageFilterAndOrder(Pageable pageable, +// InformationPageRequest informationPageRequest, +// Long userId, Long parentCategoryId); List getInformationRank(); From ce202600478b183ec42fced6073a98e9c95307b8 Mon Sep 17 00:00:00 2001 From: hyeonjaez Date: Fri, 11 Oct 2024 11:56:00 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20=ED=8E=98=EC=9D=B4=EC=A7=80=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EC=BF=BC=EB=A6=AC=EB=AC=B8=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=B4=20=EB=A9=94?= =?UTF-8?q?=EC=84=9C=EB=93=9C=20=EB=B3=80=EA=B2=BD=20=EB=B0=8F=20=EA=B8=B0?= =?UTF-8?q?=EC=A1=B4=EC=9D=98=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=A3=BC?= =?UTF-8?q?=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/InformationRepositoryImpl.java | 171 ++++++++++++++---- 1 file changed, 131 insertions(+), 40 deletions(-) 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 67a2657..c7799cb 100644 --- a/src/main/java/solitour_backend/solitour/information/repository/InformationRepositoryImpl.java +++ b/src/main/java/solitour_backend/solitour/information/repository/InformationRepositoryImpl.java @@ -11,6 +11,7 @@ import java.util.List; import java.util.Objects; +import lombok.extern.slf4j.Slf4j; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageImpl; import org.springframework.data.domain.Pageable; @@ -29,6 +30,7 @@ import solitour_backend.solitour.information.entity.QInformation; import solitour_backend.solitour.zone_category.entity.QZoneCategory; +@Slf4j public class InformationRepositoryImpl extends QuerydslRepositorySupport implements InformationRepositoryCustom { public InformationRepositoryImpl() { @@ -42,47 +44,48 @@ public InformationRepositoryImpl() { QImage image = QImage.image; QGreatInformation greatInformation = QGreatInformation.greatInformation; QCategory category = QCategory.category; + QCategory categoryParent = new QCategory("categoryParent"); QInfoTag infoTag = QInfoTag.infoTag; - - @Override - public Page getInformationPageFilterAndOrder(Pageable pageable, - InformationPageRequest informationPageRequest, - Long userId, Long parentCategoryId) { + public Page getPageInformationFilterAndOrder(Pageable pageable, InformationPageRequest informationPageRequest, Long userId, Long parentCategoryId) { BooleanBuilder whereClause = new BooleanBuilder(); - if (Objects.nonNull(informationPageRequest.getZoneCategoryId())) { whereClause.and( - information.zoneCategory.parentZoneCategory.id.eq(informationPageRequest.getZoneCategoryId())); + zoneCategoryParent.id.eq(informationPageRequest.getZoneCategoryId()) + ); } - BooleanBuilder categoryCondition = new BooleanBuilder(); - if (Objects.nonNull(informationPageRequest.getChildCategoryId())) { - whereClause.and(information.category.id.eq(informationPageRequest.getChildCategoryId())); + whereClause.and(category.id.eq(informationPageRequest.getChildCategoryId())); } else { - categoryCondition.and(category.parentCategory.id.eq(parentCategoryId)); + whereClause.and(categoryParent.id.eq(parentCategoryId)); } - if (Objects.nonNull(informationPageRequest.getSearch())) { String searchKeyword = informationPageRequest.getSearch().trim().replace(" ", ""); whereClause.and(information.title.trim().containsIgnoreCase(searchKeyword)); } - long total = from(information) + .leftJoin(zoneCategoryChild).on(zoneCategoryChild.id.eq(information.zoneCategory.id)) + .leftJoin(zoneCategoryParent).on(zoneCategoryChild.parentZoneCategory.id.eq(zoneCategoryParent.id)) + .leftJoin(category).on(category.id.eq(information.category.id)) + .leftJoin(categoryParent).on(categoryParent.id.eq(category.parentCategory.id)) + .join(image).on(image.information.id.eq(information.id).and(image.imageStatus.eq(ImageStatus.THUMBNAIL))) .where(whereClause) - .select(information.id).fetchCount(); + .distinct() + .fetchCount(); List list = from(information) - .join(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))) - .join(category).on(category.id.eq(information.category.id).and(categoryCondition)) + .leftJoin(zoneCategoryChild).on(zoneCategoryChild.id.eq(information.zoneCategory.id)) + .leftJoin(zoneCategoryParent).on(zoneCategoryChild.parentZoneCategory.id.eq(zoneCategoryParent.id)) + .leftJoin(category).on(category.id.eq(information.category.id)) + .leftJoin(categoryParent).on(categoryParent.id.eq(category.parentCategory.id)) + .leftJoin(image).on(image.information.id.eq(information.id).and(image.imageStatus.eq(ImageStatus.THUMBNAIL))) + .leftJoin(bookMarkInformation).on(bookMarkInformation.information.id.eq(information.id).and(bookMarkInformation.user.id.eq(userId))) + .leftJoin(greatInformation).on(greatInformation.information.id.eq(information.id)) .where(whereClause) - .groupBy(information.id, zoneCategoryChild.id, zoneCategoryParent.id, image.id) - .orderBy(getOrderSpecifier(informationPageRequest.getSort(), information.id)) + .groupBy(information.id, information.createdDate, information.viewCount, zoneCategoryChild.name, bookMarkInformation.id, image.address) + .orderBy(getOrderSpecifiers(informationPageRequest.getSort())) .select(Projections.constructor( InformationBriefResponse.class, information.id, @@ -91,17 +94,104 @@ public Page getInformationPageFilterAndOrder(Pageable zoneCategoryChild.name, information.category.name, information.viewCount, - isInformationBookmark(userId, information.id), + isBookMarkBooleanExpression(bookMarkInformation), image.address, - countGreatInformationByInformationById(information.id), - isUserGreatInformation(userId) + countGreatInformation(greatInformation), + isGreatBooleanExpression(userId, greatInformation) )).offset(pageable.getOffset()) .limit(pageable.getPageSize()) .fetch(); - + log.info("정보 페이지 네이션 총 갯수 : " + total + "\n"); + log.info("정보 들 : "); + log.info(list.toString()); return new PageImpl<>(list, pageable, total); } + private OrderSpecifier getOrderSpecifiers(String sort) { + if (Objects.nonNull(sort)) { + if (Objects.equals(LIKE_COUNT_SORT, sort)) { + return countGreatInformation(greatInformation).desc(); + } else if (Objects.equals(VIEW_COUNT_SORT, sort)) { + return information.viewCount.desc(); + } + } + return information.createdDate.desc(); + } + + private BooleanExpression isBookMarkBooleanExpression(QBookMarkInformation bookMarkInformation) { + return new CaseBuilder() + .when(bookMarkInformation.id.isNotNull()) + .then(true) + .otherwise(false); + } + + private BooleanExpression isGreatBooleanExpression(Long userId, QGreatInformation greatInformation) { + return greatInformation.user.id.eq(userId).count().gt(0); + } + + private NumberExpression countGreatInformation(QGreatInformation greatInformation) { + return greatInformation.id.count().intValue(); + } + +// @Override +// public Page getInformationPageFilterAndOrder(Pageable pageable, +// InformationPageRequest informationPageRequest, +// Long userId, Long parentCategoryId) { +// BooleanBuilder whereClause = new BooleanBuilder(); +// +// if (Objects.nonNull(informationPageRequest.getZoneCategoryId())) { +// whereClause.and( +// information.zoneCategory.parentZoneCategory.id.eq(informationPageRequest.getZoneCategoryId())); +// } +// +// BooleanBuilder categoryCondition = new BooleanBuilder(); +// +// if (Objects.nonNull(informationPageRequest.getChildCategoryId())) { +// whereClause.and(information.category.id.eq(informationPageRequest.getChildCategoryId())); +// } else { +// categoryCondition.and(category.parentCategory.id.eq(parentCategoryId)); +// } +// +// if (Objects.nonNull(informationPageRequest.getSearch())) { +// String searchKeyword = informationPageRequest.getSearch().trim().replace(" ", ""); +// whereClause.and(information.title.trim().containsIgnoreCase(searchKeyword)); +// } +// +// +// long total = from(information) +// .where(whereClause) +// .select(information.id).fetchCount(); +// System.out.println("page 네이션 총 데이터 갯수 : " + total); +// List list = from(information) +// .join(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))) +// .join(category).on(category.id.eq(information.category.id).and(categoryCondition)) +// .where(whereClause) +// .groupBy(information.id, zoneCategoryChild.id, zoneCategoryParent.id, image.id) +// .orderBy(getOrderSpecifier(informationPageRequest.getSort(), information.id)) +// .select(Projections.constructor( +// InformationBriefResponse.class, +// information.id, +// information.title, +// zoneCategoryParent.name, +// zoneCategoryChild.name, +// information.category.name, +// information.viewCount, +// isInformationBookmark(userId, information.id), +// image.address, +// countGreatInformationByInformationById(information.id), +// isUserGreatInformation(userId) +// )).offset(pageable.getOffset()) +// .limit(pageable.getPageSize()) +// .fetch(); +// System.out.println(list.size()); +// System.out.println(list); +// +// return new PageImpl<>(list, pageable, total); +// } + @Override public List getInformationLikeCountFromCreatedIn3(Long userId) { @@ -112,7 +202,7 @@ public List getInformationLikeCountFromCreatedIn3(Long .leftJoin(category).on(category.id.eq(information.category.id)) .where(information.createdDate.after(LocalDateTime.now().minusMonths(3))) .groupBy(information.id, zoneCategoryParent.name, zoneCategoryChild.name, image.address) - .orderBy(countGreatInformationByInformationById(information.id).desc()) + .orderBy(countGreatInformationByInformationByIdSubQuery(information.id).desc()) .select(Projections.constructor( InformationMainResponse.class, information.id, @@ -121,10 +211,10 @@ public List getInformationLikeCountFromCreatedIn3(Long zoneCategoryChild.name, category.parentCategory.name, information.viewCount, - isInformationBookmark(userId, information.id), + isInformationBookmarkSubQuery(userId, information.id), image.address, - countGreatInformationByInformationById(information.id), // 파라미터 전달 - isUserGreatInformation(userId) + countGreatInformationByInformationByIdSubQuery(information.id), // 파라미터 전달 + isUserGreatInformationSubQuery(userId) )).limit(6).fetch(); } @@ -147,17 +237,18 @@ public List getInformationRecommend(Long informationId zoneCategoryChild.name, information.category.name, information.viewCount, - isInformationBookmark(userId, information.id), + isInformationBookmarkSubQuery(userId, information.id), image.address, - countGreatInformationByInformationById(information.id), - isUserGreatInformation(userId) + countGreatInformationByInformationByIdSubQuery(information.id), + isUserGreatInformationSubQuery(userId) )) .limit(3L) .fetch(); } @Override - public Page getInformationPageByTag(Pageable pageable, Long userId, Long parentCategoryId, + public Page getInformationPageByTag(Pageable pageable, Long userId, Long + parentCategoryId, InformationPageRequest informationPageRequest, String decodedTag) { BooleanBuilder whereClause = new BooleanBuilder(); @@ -213,8 +304,8 @@ public Page getInformationPageByTag(Pageable pageable, information.viewCount, bookMarkInformation.user.id.isNotNull(), image.address, - countGreatInformationByInformationById(information.id), - isUserGreatInformation(userId) + countGreatInformationByInformationByIdSubQuery(information.id), + isUserGreatInformationSubQuery(userId) )).offset(pageable.getOffset()) .limit(pageable.getPageSize()) .fetch(); @@ -228,7 +319,7 @@ public List getInformationRank() { .leftJoin(greatInformation) .on(greatInformation.information.id.eq(information.id)) .groupBy(information.id, information.title) - .orderBy(countGreatInformationByInformationById(information.id).desc()) + .orderBy(countGreatInformationByInformationByIdSubQuery(information.id).desc()) .limit(5) .select(Projections.constructor( InformationRankResponse.class, @@ -240,7 +331,7 @@ public List getInformationRank() { private OrderSpecifier getOrderSpecifier(String sort, NumberPath informationId) { if (Objects.nonNull(sort)) { if (Objects.equals(LIKE_COUNT_SORT, sort)) { - return countGreatInformationByInformationById(informationId).desc(); + return countGreatInformationByInformationByIdSubQuery(informationId).desc(); } else if (Objects.equals(VIEW_COUNT_SORT, sort)) { return information.viewCount.desc(); } @@ -248,7 +339,7 @@ private OrderSpecifier getOrderSpecifier(String sort, NumberPath inform return information.createdDate.desc(); } - private NumberExpression countGreatInformationByInformationById(NumberPath informationId) { + private NumberExpression countGreatInformationByInformationByIdSubQuery(NumberPath informationId) { QGreatInformation greatInformationSub = QGreatInformation.greatInformation; JPQLQuery likeCountSubQuery = JPAExpressions .select(greatInformationSub.count()) @@ -260,7 +351,7 @@ private NumberExpression countGreatInformationByInformationById(NumberP .intValue(); } - private BooleanExpression isUserGreatInformation(Long userId) { + private BooleanExpression isUserGreatInformationSubQuery(Long userId) { return new CaseBuilder() .when(JPAExpressions.selectOne() .from(greatInformation) @@ -271,7 +362,7 @@ private BooleanExpression isUserGreatInformation(Long userId) { .otherwise(false); } - private BooleanExpression isInformationBookmark(Long userId, NumberPath informationId) { + private BooleanExpression isInformationBookmarkSubQuery(Long userId, NumberPath informationId) { return new CaseBuilder() .when(JPAExpressions.selectOne() .from(bookMarkInformation) From 70050e11c05529a35aac78c90f01b655fa3edc93 Mon Sep 17 00:00:00 2001 From: hyeonjaez Date: Fri, 11 Oct 2024 11:56:23 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20=ED=8E=98=EC=9D=B4=EC=A7=80=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EC=85=98=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD=EC=9C=BC=EB=A1=9C=20=EC=9D=B8=ED=95=B4=20service?= =?UTF-8?q?=EC=97=90=EC=84=9C=20=ED=98=B8=EC=B6=9C=ED=95=98=EB=8A=94=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../solitour/information/service/InformationService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/solitour_backend/solitour/information/service/InformationService.java b/src/main/java/solitour_backend/solitour/information/service/InformationService.java index 8cfef9e..f0b04ec 100644 --- a/src/main/java/solitour_backend/solitour/information/service/InformationService.java +++ b/src/main/java/solitour_backend/solitour/information/service/InformationService.java @@ -458,7 +458,7 @@ public Page getPageInformation(Pageable pageable, Long } } - return informationRepository.getInformationPageFilterAndOrder(pageable, informationPageRequest, userId, parentCategoryId); + return informationRepository.getPageInformationFilterAndOrder(pageable, informationPageRequest, userId, parentCategoryId); } public List getRankInformation() {