Skip to content

Commit

Permalink
Merge pull request #148 from kakao-tech-campus-2nd-step3/feat/#87-lik…
Browse files Browse the repository at this point in the history
…ed-place

[refactor] #87 totalElements를 계산해서 page를 생성하도록 변경
  • Loading branch information
dong-yxxn authored Nov 12, 2024
2 parents 29f6a0b + 7dd674d commit 8770a16
Showing 1 changed file with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;
import com.querydsl.core.types.dsl.NumberTemplate;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.List;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.support.PageableExecutionUtils;
import org.springframework.stereotype.Repository;
import team7.inplace.influencer.domain.QInfluencer;
import team7.inplace.place.domain.Category;
Expand Down Expand Up @@ -56,7 +58,8 @@ public Page<Place> findPlacesByDistanceAndFilters(String topLeftLongitude,
Double.parseDouble(latitude), place.coordinate.latitude, place.coordinate.longitude,
Double.parseDouble(longitude));

List<Place> places = jpaQueryFactory.selectFrom(place)
// 1. content를 가져오는 fetch() 쿼리
List<Place> content = jpaQueryFactory.selectFrom(place)
.leftJoin(video).on(video.place.eq(place))
.leftJoin(influencer).on(video.influencer.eq(influencer))
.where(
Expand All @@ -69,12 +72,31 @@ public Page<Place> findPlacesByDistanceAndFilters(String topLeftLongitude,
),
placeCategoryIn(categories),
placeInfluencerIn(influencers)
).orderBy(distanceExpression.asc())
)
.orderBy(distanceExpression.asc())
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

return new PageImpl<>(places, pageable, places.size());
// 2. countQuery를 따로 선언하여 필요할 때만 실행
JPAQuery<Long> countQuery = jpaQueryFactory.select(place.count())
.from(place)
.leftJoin(video).on(video.place.eq(place))
.leftJoin(influencer).on(video.influencer.eq(influencer))
.where(
withinBoundary(
place,
Double.parseDouble(topLeftLongitude),
Double.parseDouble(topLeftLatitude),
Double.parseDouble(bottomRightLongitude),
Double.parseDouble(bottomRightLatitude)
),
placeCategoryIn(categories),
placeInfluencerIn(influencers)
);

// 3. PageableExecutionUtils를 사용하여 필요할 때 countQuery 실행
return PageableExecutionUtils.getPage(content, pageable, countQuery::fetchOne);
}

private BooleanExpression withinBoundary(QPlace place, double topLeftLongitude,
Expand Down

0 comments on commit 8770a16

Please sign in to comment.