Skip to content

Commit

Permalink
Merge pull request #197 from TripInfoWeb/fix/information_gathering_li…
Browse files Browse the repository at this point in the history
…keCount

fix: 좋아요 수 구하는 절 수정
  • Loading branch information
hyeonjaez authored Sep 22, 2024
2 parents c0b6f12 + a7e5ba3 commit d283fe7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -51,8 +47,6 @@ public GatheringRepositoryImpl() {

@Override
public List<GatheringBriefResponse> getGatheringRecommend(Long gatheringId, Long gatheringCategoryId, Long userId) {
NumberExpression<Integer> likeCount = countGreatGatheringByGatheringById();
NumberExpression<Integer> applicantsCount = applicantsCountNowConsent();
return from(gathering)
.join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id))
.leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id))
Expand All @@ -76,7 +70,7 @@ public List<GatheringBriefResponse> getGatheringRecommend(Long gatheringId, Long
zoneCategoryChild.name,
gathering.viewCount,
isGatheringBookmark(userId),
likeCount,
countGreatGatheringByGatheringById(gathering.id),
gathering.gatheringCategory.name,
gathering.user.nickname,
gathering.scheduleStartDate,
Expand All @@ -86,7 +80,7 @@ public List<GatheringBriefResponse> getGatheringRecommend(Long gatheringId, Long
gathering.startAge,
gathering.endAge,
gathering.personCount,
applicantsCount,
applicantsCountNowConsent(gathering.id),
isUserGreatGathering(userId)
)).limit(3L).fetch();
}
Expand All @@ -98,11 +92,6 @@ public Page<GatheringBriefResponse> getGatheringPageFilterAndOrder(Pageable page
Long userId) {
BooleanBuilder booleanBuilder = makeWhereSQL(gatheringPageRequest);

OrderSpecifier<?> orderSpecifier = getOrderSpecifier(gatheringPageRequest.getSort());

NumberExpression<Integer> countGreatGathering = countGreatGatheringByGatheringById();
NumberExpression<Integer> applicantsCount = applicantsCountNowConsent();

long total = from(gathering)
.where(booleanBuilder)
.select(gathering.id).fetchCount();
Expand All @@ -116,7 +105,7 @@ public Page<GatheringBriefResponse> 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,
Expand All @@ -125,7 +114,7 @@ public Page<GatheringBriefResponse> getGatheringPageFilterAndOrder(Pageable page
zoneCategoryChild.name,
gathering.viewCount,
isGatheringBookmark(userId),
countGreatGathering,
countGreatGatheringByGatheringById(gathering.id),
gathering.gatheringCategory.name,
gathering.user.nickname,
gathering.scheduleStartDate,
Expand All @@ -135,7 +124,7 @@ public Page<GatheringBriefResponse> getGatheringPageFilterAndOrder(Pageable page
gathering.startAge,
gathering.endAge,
gathering.personCount,
applicantsCount,
applicantsCountNowConsent(gathering.id),
isUserGreatGathering(userId)
))
.offset(pageable.getOffset())
Expand All @@ -151,10 +140,6 @@ public Page<GatheringBriefResponse> getPageGatheringByTag(Pageable pageable,
String decodedTag) {
BooleanBuilder booleanBuilder = makeWhereSQL(gatheringPageRequest);

OrderSpecifier<?> orderSpecifier = getOrderSpecifier(gatheringPageRequest.getSort());

NumberExpression<Integer> countGreatGathering = countGreatGatheringByGatheringById();

long total = from(gathering)
.join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id))
.leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id))
Expand All @@ -176,7 +161,7 @@ public Page<GatheringBriefResponse> 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,
Expand All @@ -185,7 +170,7 @@ public Page<GatheringBriefResponse> getPageGatheringByTag(Pageable pageable,
zoneCategoryChild.name,
gathering.viewCount,
bookMarkGathering.user.id.isNotNull(),
countGreatGathering,
countGreatGatheringByGatheringById(gathering.id),
gathering.gatheringCategory.name,
gathering.user.name,
gathering.scheduleStartDate,
Expand All @@ -208,7 +193,7 @@ public Page<GatheringBriefResponse> getPageGatheringByTag(Pageable pageable,
@Override
public List<GatheringRankResponse> 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))
Expand All @@ -224,8 +209,6 @@ public List<GatheringRankResponse> getGatheringRankList() {

@Override
public List<GatheringBriefResponse> getGatheringLikeCountFromCreatedIn3(Long userId) {
NumberExpression<Integer> likeCount = countGreatGatheringByGatheringById();
NumberExpression<Integer> applicantsCount = applicantsCountNowConsent();
return from(gathering)
.join(zoneCategoryChild).on(zoneCategoryChild.id.eq(gathering.zoneCategory.id))
.leftJoin(zoneCategoryParent).on(zoneCategoryParent.id.eq(zoneCategoryChild.parentZoneCategory.id))
Expand All @@ -238,7 +221,7 @@ public List<GatheringBriefResponse> 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,
Expand All @@ -247,7 +230,7 @@ public List<GatheringBriefResponse> getGatheringLikeCountFromCreatedIn3(Long use
zoneCategoryChild.name,
gathering.viewCount,
isGatheringBookmark(userId),
likeCount,
countGreatGatheringByGatheringById(gathering.id),
gathering.gatheringCategory.name,
gathering.user.nickname,
gathering.scheduleStartDate,
Expand All @@ -257,7 +240,7 @@ public List<GatheringBriefResponse> getGatheringLikeCountFromCreatedIn3(Long use
gathering.startAge,
gathering.endAge,
gathering.personCount,
applicantsCount,
applicantsCountNowConsent(gathering.id),
isUserGreatGathering(userId)
)).limit(6L).fetch();
}
Expand Down Expand Up @@ -306,12 +289,12 @@ private BooleanBuilder makeWhereSQL(GatheringPageRequest gatheringPageRequest) {
}

// 정렬 방식
private OrderSpecifier<?> getOrderSpecifier(String sort) {
private OrderSpecifier<?> getOrderSpecifier(String sort, NumberPath<Long> gatheringId) {
PathBuilder<Gathering> 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();
}
Expand All @@ -321,12 +304,12 @@ private OrderSpecifier<?> getOrderSpecifier(String sort) {
}

// 좋아요 수 가져오는 식
private NumberExpression<Integer> countGreatGatheringByGatheringById() {
private NumberExpression<Integer> countGreatGatheringByGatheringById(NumberPath<Long> gatheringId) {
QGreatGathering greatGatheringSub = QGreatGathering.greatGathering;
JPQLQuery<Long> 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)
Expand Down Expand Up @@ -355,11 +338,11 @@ private BooleanExpression isGatheringBookmark(Long userId) {
.otherwise(false);
}

private NumberExpression<Integer> applicantsCountNowConsent() {
private NumberExpression<Integer> applicantsCountNowConsent(NumberPath<Long> gatheringId) {
JPQLQuery<Integer> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -72,8 +69,6 @@ public Page<InformationBriefResponse> getInformationPageFilterAndOrder(Pageable
whereClause.and(information.title.trim().containsIgnoreCase(searchKeyword));
}

OrderSpecifier<?> orderSpecifier = getOrderSpecifier(informationPageRequest.getSort());
NumberExpression<Integer> countGreatInformation = countGreatInformationByInformationById();

long total = from(information)
.where(whereClause)
Expand All @@ -87,7 +82,7 @@ public Page<InformationBriefResponse> 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,
Expand All @@ -98,7 +93,7 @@ public Page<InformationBriefResponse> getInformationPageFilterAndOrder(Pageable
information.viewCount,
isInformationBookmark(userId),
image.address,
countGreatInformation,
countGreatInformationByInformationById(information.id),
isUserGreatInformation(userId)
)).offset(pageable.getOffset())
.limit(pageable.getPageSize())
Expand All @@ -113,13 +108,12 @@ public List<InformationMainResponse> 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,
Expand All @@ -130,10 +124,9 @@ public List<InformationMainResponse> getInformationLikeCountFromCreatedIn3(Long
information.viewCount,
isInformationBookmark(userId),
image.address,
countGreatInformationByInformationById(),
countGreatInformationByInformationById(information.id), // 파라미터 전달
isUserGreatInformation(userId)
)).limit(6).fetch();

}

@Override
Expand All @@ -157,7 +150,7 @@ public List<InformationBriefResponse> getInformationRecommend(Long informationId
information.viewCount,
isInformationBookmark(userId),
image.address,
countGreatInformationByInformationById(),
countGreatInformationByInformationById(information.id),
isUserGreatInformation(userId)
))
.limit(3L)
Expand All @@ -183,8 +176,6 @@ public Page<InformationBriefResponse> getInformationPageByTag(Pageable pageable,
categoryCondition.and(category.parentCategory.id.eq(parentCategoryId));
}

OrderSpecifier<?> orderSpecifier = getOrderSpecifier(informationPageRequest.getSort());
NumberExpression<Integer> countGreatInformation = countGreatInformationByInformationById();

long total = from(information)
.join(zoneCategoryChild).on(zoneCategoryChild.id.eq(information.zoneCategory.id))
Expand Down Expand Up @@ -212,7 +203,7 @@ public Page<InformationBriefResponse> 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,
Expand All @@ -223,7 +214,7 @@ public Page<InformationBriefResponse> getInformationPageByTag(Pageable pageable,
information.viewCount,
bookMarkInformation.user.id.isNotNull(),
image.address,
countGreatInformation,
countGreatInformationByInformationById(information.id),
isUserGreatInformation(userId)
)).offset(pageable.getOffset())
.limit(pageable.getPageSize())
Expand All @@ -238,7 +229,7 @@ public List<InformationRankResponse> 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,
Expand All @@ -247,27 +238,25 @@ public List<InformationRankResponse> getInformationRank() {
)).fetch();
}

private OrderSpecifier<?> getOrderSpecifier(String sort) {
private OrderSpecifier<?> getOrderSpecifier(String sort, NumberPath<Long> 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();
}
}
return information.createdDate.desc();
}

private NumberExpression<Integer> countGreatInformationByInformationById() {
private NumberExpression<Long> countGreatInformationByInformationById(NumberPath<Long> informationId) {
QGreatInformation greatInformationSub = QGreatInformation.greatInformation;
JPQLQuery<Long> 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) {
Expand Down

0 comments on commit d283fe7

Please sign in to comment.