Skip to content

Commit

Permalink
Merge pull request #226 from TripInfoWeb/fix/information_Pagenation_f…
Browse files Browse the repository at this point in the history
…ilter_sort

Fix/information pagenation filter sort
  • Loading branch information
hyeonjaez authored Oct 11, 2024
2 parents e43d4dc + 70050e1 commit 9a410c8
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@Getter
@AllArgsConstructor
@ToString
public class InformationBriefResponse {

private Long informationId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
public interface InformationRepositoryCustom {
String LIKE_COUNT_SORT = "likes";
String VIEW_COUNT_SORT = "views";
Page<InformationBriefResponse> getPageInformationFilterAndOrder(Pageable pageable, InformationPageRequest informationPageRequest, Long userId, Long parentCategoryId);

Page<InformationBriefResponse> getInformationPageFilterAndOrder(Pageable pageable,
InformationPageRequest informationPageRequest,
Long userId, Long parentCategoryId);
// Page<InformationBriefResponse> getInformationPageFilterAndOrder(Pageable pageable,
// InformationPageRequest informationPageRequest,
// Long userId, Long parentCategoryId);

List<InformationRankResponse> getInformationRank();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand All @@ -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<InformationBriefResponse> getInformationPageFilterAndOrder(Pageable pageable,
InformationPageRequest informationPageRequest,
Long userId, Long parentCategoryId) {
public Page<InformationBriefResponse> 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<InformationBriefResponse> 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,
Expand All @@ -91,17 +94,104 @@ public Page<InformationBriefResponse> 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<Integer> countGreatInformation(QGreatInformation greatInformation) {
return greatInformation.id.count().intValue();
}

// @Override
// public Page<InformationBriefResponse> 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<InformationBriefResponse> 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<InformationMainResponse> getInformationLikeCountFromCreatedIn3(Long userId) {
Expand All @@ -112,7 +202,7 @@ public List<InformationMainResponse> 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,
Expand All @@ -121,10 +211,10 @@ public List<InformationMainResponse> 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();
}

Expand All @@ -147,17 +237,18 @@ public List<InformationBriefResponse> 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<InformationBriefResponse> getInformationPageByTag(Pageable pageable, Long userId, Long parentCategoryId,
public Page<InformationBriefResponse> getInformationPageByTag(Pageable pageable, Long userId, Long
parentCategoryId,
InformationPageRequest informationPageRequest,
String decodedTag) {
BooleanBuilder whereClause = new BooleanBuilder();
Expand Down Expand Up @@ -213,8 +304,8 @@ public Page<InformationBriefResponse> 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();
Expand All @@ -228,7 +319,7 @@ public List<InformationRankResponse> 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,
Expand All @@ -240,15 +331,15 @@ public List<InformationRankResponse> getInformationRank() {
private OrderSpecifier<?> getOrderSpecifier(String sort, NumberPath<Long> 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();
}
}
return information.createdDate.desc();
}

private NumberExpression<Integer> countGreatInformationByInformationById(NumberPath<Long> informationId) {
private NumberExpression<Integer> countGreatInformationByInformationByIdSubQuery(NumberPath<Long> informationId) {
QGreatInformation greatInformationSub = QGreatInformation.greatInformation;
JPQLQuery<Long> likeCountSubQuery = JPAExpressions
.select(greatInformationSub.count())
Expand All @@ -260,7 +351,7 @@ private NumberExpression<Integer> countGreatInformationByInformationById(NumberP
.intValue();
}

private BooleanExpression isUserGreatInformation(Long userId) {
private BooleanExpression isUserGreatInformationSubQuery(Long userId) {
return new CaseBuilder()
.when(JPAExpressions.selectOne()
.from(greatInformation)
Expand All @@ -271,7 +362,7 @@ private BooleanExpression isUserGreatInformation(Long userId) {
.otherwise(false);
}

private BooleanExpression isInformationBookmark(Long userId, NumberPath<Long> informationId) {
private BooleanExpression isInformationBookmarkSubQuery(Long userId, NumberPath<Long> informationId) {
return new CaseBuilder()
.when(JPAExpressions.selectOne()
.from(bookMarkInformation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ public Page<InformationBriefResponse> getPageInformation(Pageable pageable, Long
}
}

return informationRepository.getInformationPageFilterAndOrder(pageable, informationPageRequest, userId, parentCategoryId);
return informationRepository.getPageInformationFilterAndOrder(pageable, informationPageRequest, userId, parentCategoryId);
}

public List<InformationRankResponse> getRankInformation() {
Expand Down

0 comments on commit 9a410c8

Please sign in to comment.