Skip to content

Commit

Permalink
Merge pull request #40 from ShwimPing/refactor/ldy/place
Browse files Browse the repository at this point in the history
[REFACTOR] hasNext에서 쿼리가 추가로 발생하던 문제 해결
  • Loading branch information
Amepistheo authored Sep 30, 2024
2 parents 3f731c6 + 0a69748 commit 6f5ba75
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.shwimping.be.place.application;

import com.shwimping.be.bookmark.domain.BookMark;
import com.shwimping.be.bookmark.dto.response.BookMarkPlaceResponse;
import com.shwimping.be.place.application.type.SortType;
import com.shwimping.be.place.domain.Place;
import com.shwimping.be.place.domain.type.Category;
Expand Down Expand Up @@ -45,11 +43,13 @@ public SearchPlaceResponseList findNearestPlaces(
List<SearchPlaceResponse> allSearchResult = placeRepository.findAllByLocationWithDistance(
longitude, latitude, maxDistant, categoryList, sortType, keyword, page, size);

// 전체 데이터 수 카운트
long totalCount = placeRepository.countByLocationWithDistance(longitude, latitude, maxDistant, categoryList);

// hasNext 계산: 현재 페이지의 데이터 수가 10개이고 전체 데이터 수가 page가 끝나는 지점보다 클 경우 false
boolean hasNext = allSearchResult.size() == 10 && (page + 1) * 10 < totalCount;
boolean hasNext = allSearchResult.size() == size + 1;

// 마지막 원소를 제외한 서브 리스트 생성
if (hasNext) {
allSearchResult = allSearchResult.subList(0, allSearchResult.size() - 1);
}

return SearchPlaceResponseList.of(page, hasNext, longitude, latitude, maxDistant, keyword, sortType,
categoryList, allSearchResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,5 @@ List<SearchPlaceResponse> findAllByLocationWithDistance(
double longitude, double latitude, int maxDistance, List<Category> categoryList, SortType sortType,
String keyword, long page, long size);

Long countByLocationWithDistance(double longitude, double latitude, int maxDistant, List<Category> categoryList);

PlaceDetailWithReviews findPlaceDetail(Long placeId, Long userId, Long size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public List<SearchPlaceResponse> findAllByLocationWithDistance(
.groupBy(place.id)
.orderBy(orderExpression(sortType, longitude, latitude), reviewCntOrder()) // 정렬 조건 추가
.offset(page * size) // 페이지 번호에 따라 결과를 가져오도록 설정
.limit(size)
.limit(size + 1)
.fetch();
}

Expand Down Expand Up @@ -101,16 +101,6 @@ private BooleanExpression keywordSearchExpression(String keyword) {
}
}

public Long countByLocationWithDistance(
double longitude, double latitude, int maxDistance, List<Category> categoryList) {

return jpaQueryFactory.select(place.count())
.from(place)
.where(rangeTemplate(longitude, latitude, maxDistance),
place.category.in(categoryList))
.fetchOne();
}

@Override
public PlaceDetailWithReviews findPlaceDetail(Long placeId, Long userId, Long size) {
// 리뷰를 최신 5개만 가져오기 위한 서브쿼리
Expand All @@ -127,17 +117,10 @@ public PlaceDetailWithReviews findPlaceDetail(Long placeId, Long userId, Long si
.leftJoin(review.user)
.where(review.place.id.eq(placeId))
.orderBy(review.date.desc())
.limit(5)
.limit(size + 1)
.fetch();

// hasNext 확인을 위한 전체 리뷰 수 쿼리
Long totalReviewsCount = jpaQueryFactory
.select(review.count())
.from(review)
.where(review.place.id.eq(placeId))
.fetchOne();

boolean hasNext = totalReviewsCount != null && totalReviewsCount > size; // 5개 이상의 리뷰가 있는지 확인
boolean hasNext = recentReviews.size() == size + 1;

// PlaceDetailResponse를 생성하기 위한 쿼리
PlaceDetailResponse placeDetailResponse = jpaQueryFactory.select(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,17 @@ public class ReviewService {

public ReviewSimpleResponseList getReviewSimpleResponse(Long placeId, Long lastReviewId, Long size) {

List<ReviewSimpleResponse> reviewSimpleResponse =
List<ReviewSimpleResponse> reviewSimpleResponseList =
reviewRepository.getReviewSimpleResponse(placeId, lastReviewId, size);

Boolean hasNext = reviewRepository.hasNext(placeId, lastReviewId, size);
boolean hasNext = reviewSimpleResponseList.size() == size + 1;

return ReviewSimpleResponseList.of(hasNext, reviewSimpleResponse);
// 마지막 원소를 제외한 서브 리스트 생성
if (hasNext) {
reviewSimpleResponseList = reviewSimpleResponseList.subList(0, reviewSimpleResponseList.size() - 1);
}

return ReviewSimpleResponseList.of(hasNext, reviewSimpleResponseList);
}

public ReviewSimpleResponseList getMyReview(Long userId, Long lastReviewId, Long size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@ public interface ReviewRepositoryCustom {

List<ReviewSimpleResponse> getMyReview(Long userId, Long lastReviewId, Long size);

Boolean hasNext(Long placeId, Long lastReviewId, Long size);

Boolean hasNextMyReview(Long userId, Long lastReviewId, Long size);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public List<ReviewSimpleResponse> getReviewSimpleResponse(Long placeId, Long las
.from(review)
.where(review.place.id.eq(placeId), review.id.lt(lastReviewId)) // lastReviewId를 기준으로 필터링
.orderBy(review.id.desc())
.limit(size) // 가져올 리뷰 수 제한
.limit(size + 1) // 가져올 리뷰 수 제한
.fetch();
}

Expand Down Expand Up @@ -72,14 +72,6 @@ public List<ReviewSimpleResponse> getMyReview(Long userId, Long lastReviewId, Lo
.fetch();
}

@Override
public Boolean hasNext(Long placeId, Long lastReviewId, Long size) {
return jpaQueryFactory.selectOne()
.from(review)
.where(review.place.id.eq(placeId), review.id.lt(lastReviewId - size))
.fetchFirst() != null;
}

@Override
public Boolean hasNextMyReview(Long userId, Long lastReviewId, Long size) {
return jpaQueryFactory.selectOne()
Expand Down

0 comments on commit 6f5ba75

Please sign in to comment.