Skip to content

Commit

Permalink
fix: 스토어 정보 리스트 조회(인기순) - #125
Browse files Browse the repository at this point in the history
  • Loading branch information
sjk4618 committed Jan 22, 2025
1 parent f24ea59 commit 58e99fa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ResponseEntity<BaseResponse<?>> getStoreCoordinateList(
StoreCoordinateListRes.from(storeService.getStoreCoordinateList(station)));
}

//지하철역 스토어 리스트 조회(인기순)
//스토어 정보 리스트 조회(인기순)
@GetMapping("/popularity")
public ResponseEntity<BaseResponse<?>> getStoreInfoListByStationAndLikes(
@UserId final Long userId,
Expand All @@ -47,7 +47,7 @@ public ResponseEntity<BaseResponse<?>> getStoreInfoListByStationAndLikes(
size));
}

//지하철역 스토어 리스트 조회(최신순)
//스토어 정보 리스트 조회(최신순)
@GetMapping("/latest")
public ResponseEntity<BaseResponse<?>> getStoreInfoListByStationAndLatest(
//todo: @UserId final Long userId //다음 피알에서 추가예정
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,58 +57,79 @@ public List<StoreInfoDto> findPopularitryStoreInfoByStation(final Long userId,
final Long storeIdCursor,
final int size) {

// 좋아요 개수를 계산하는 서브쿼리
final Expression<Integer> storeLikesCountSubQuery = getStoreLikesCountSubQuery();
/// 좋아요 개수를 계산하는 서브쿼리
final NumberExpression<Integer> storeLikesOrderExpression = Expressions.asNumber(
JPAExpressions
.select(storeLike.count().intValue())
.from(storeLike)
.where(storeLike.storeId.eq(store.id))
);


// 좋아요 여부 서브쿼리
/// 좋아요 여부 서브쿼리
final BooleanExpression isLikedExpression = isLikedExpression(userId);

// 커서 조건 처리
/// 커서 조건 처리
final BooleanExpression storeIdCursorCondition = (storeIdCursor == null || storeIdCursor == 0)
? null
: store.id.gt(storeIdCursor); // storeIdCursor가 존재하면 store.id > storeIdCursor 조건 추가
: store.id.gt(storeIdCursor); /// storeIdCursor가 존재하면 store.id > storeIdCursor 조건 추가

// 역 조건
/// 역 조건
final BooleanExpression stationCondition = station != Station.ALL
? store.station.eq(station)
: null; // 기본 조건
: null; /// 기본 조건

// 쿼리 실행
/// 쿼리 실행
JPQLQuery<StoreInfoDto> query =
queryFactory.select(new QStoreInfoDto(
store.id,
store.name,
store.station,
store.address,
isLikedExpression,
storeLikesCountSubQuery,
store.id, // Cursor로 사용할 storeId
storeLikesOrderExpression,
store.id, /// Cursor로 사용할 storeId
Expressions.asBoolean(false)
))
.from(store)
.leftJoin(storeLike).on(storeLike.storeId.eq(store.id))
.where(stationCondition) // 역 조건
.groupBy(store.id);

/// likesCursor 조건 추가
if (likesCursor != null) {
if (likesCursor == 0) {
// likesCursor가 0이면 처음부터 조회 (조건 없이 정렬만 적용)
System.out.println("likesCursor가 0이므로 전체중 좋아요가 많은 순서로 조회합니다.");
} else {
query.having(
storeLike.count().lt(likesCursor) /// 좋아요 수가 likesCursor보다 작은 데이터
.or(storeLike.count().intValue().eq(likesCursor))
.and(storeIdCursorCondition) /// 좋아요 수가 같으면 storeId로 페이징
);
}
} else if (storeIdCursorCondition != null) {
/// likesCursor가 없고 storeIdCursor만 존재하면
System.out.println("예외처리 추가"); //todo:likesCursor없이 storeIdCursor만 있으면 예외처리
/// 조건 처리
if (likesCursor == null && storeIdCursor == null) {
/// 1. 아이디커서와 좋아요커서 둘 다 없을 때
query.orderBy(
storeLikesOrderExpression.desc(), /// 좋아요 개수 내림차순
store.id.asc() /// 같은 좋아요 개수일 경우 storeId 오름차순
);
} else if (likesCursor != null && likesCursor == 0 && storeIdCursor != null && storeIdCursor > 0) {
/// 2. 좋아요커서가 0이고, 아이디커서가 0보다 클 때
query.having(storeLikesOrderExpression.eq(0).and(storeIdCursorCondition));
query.orderBy(store.id.asc()); /// 아이디 오름차순
} else if (likesCursor != null && likesCursor == 0 && (storeIdCursor == null || storeIdCursor == 0)) {
/// 3. 좋아요커서가 0이고, 아이디커서가 없거나 0일 때 (예외 처리)
throw new IllegalArgumentException("Invalid cursor combination: likesCursor=0 and storeIdCursor=0 or null");
} else if (likesCursor != null && likesCursor > 0 && (storeIdCursor == null || storeIdCursor == 0)) {
/// 4. 좋아요커서가 0보다 크고, 아이디커서가 없을 때
query.having(storeLikesOrderExpression.lt(likesCursor)); /// 좋아요 수가 likesCursor보다 작은 스토어 조회
query.orderBy(
storeLikesOrderExpression.desc(), /// 좋아요 개수 내림차순
store.id.asc() /// 같은 좋아요 개수일 경우 storeId 오름차순
);
} else if (likesCursor != null && likesCursor > 0 && storeIdCursor != null && storeIdCursor > 0) {
/// 5. 좋아요커서가 0보다 크고, 아이디커서가 0보다 클 때
query.having(
storeLikesOrderExpression.eq(likesCursor).and(storeIdCursorCondition) /// 좋아요 수가 likesCursor와 같고, storeIdCursor보다 큰 스토어
.or(storeLikesOrderExpression.lt(likesCursor)) /// 이후 좋아요 수가 likesCursor보다 작은 스토어
);
query.orderBy(
storeLikesOrderExpression.desc(), /// 좋아요 개수 내림차순
store.id.asc() /// 같은 좋아요 개수일 경우 storeId 오름차순
);
}

query.orderBy(storeLike.id.count().desc(), store.id.asc()) /// 좋아요 수 -> storeId순(최신순) 정렬
.limit(size + 1); /// limit + 1개 조회
query.limit(size + 1);

/// 쿼리 실행
List<StoreInfoDto> stores = query.fetch();
Expand Down

0 comments on commit 58e99fa

Please sign in to comment.