Skip to content

Commit

Permalink
[hotfix] 게시물 전체 조회 페이지네이션 오류 해결
Browse files Browse the repository at this point in the history
distinct 추가해서 중복 객체 카운팅 해결
  • Loading branch information
chahyunsoo committed Oct 23, 2024
1 parent 71142d7 commit c332741
Showing 1 changed file with 9 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,6 @@ public Page<Post> findAllByBoardIdAndGroupCodeAndMemberCode(Long boardId, GroupC
.otherwise(Integer.MAX_VALUE);


// if (whereClause.getValue() == null) {
// throw new IllegalArgumentException("At least one of memberCode, or groupCode must be provided");
// }

JPAQuery<PostEntity> query = queryFactory
.selectFrom(postEntity)
.leftJoin(postEntity.userEntity, userEntity)
Expand All @@ -205,34 +201,28 @@ public Page<Post> findAllByBoardIdAndGroupCodeAndMemberCode(Long boardId, GroupC
.leftJoin(postFileEntity).on(postFileEntity.postEntity.eq(postEntity))
.where(whereClause)
// .orderBy(postEntity.createdAt.desc());
.orderBy(statusOrder.asc(), postEntity.createdAt.desc());

.orderBy(statusOrder.asc(), postEntity.createdAt.desc()).distinct();


List<PostEntity> content = query
.offset(pageable.getOffset())
.limit(pageable.getPageSize())
.fetch();

// JPAQuery<Long> countQuery = queryFactory
// .select(postEntity.count())
// .from(postEntity)
// .where(whereClause);

JPAQuery<Long> countQuery = queryFactory
.select(postEntity.count())
.from(postEntity)
.leftJoin(postEntity.userEntity, userEntity)
.leftJoin(memberEntity).on(memberEntity.userEntity.eq(userEntity))
.leftJoin(memberEntity.groupEntity, groupEntity)
.leftJoin(postFileEntity).on(postFileEntity.postEntity.eq(postEntity))
.where(whereClause);

.where(whereClause)
.distinct();

return PageableExecutionUtils.getPage(
content.stream().map(postMapper::toDomain).collect(Collectors.toList()),
pageable,
countQuery::fetchOne
countQuery::fetchCount
);
}

Expand Down Expand Up @@ -469,10 +459,6 @@ public Page<Post> searchAllByBoardIdAndGroupCodeAndMemberCode(Long boardId, Stri
.otherwise(Integer.MAX_VALUE);


// if (whereClause.getValue() == null) {
// throw new IllegalArgumentException("At least one of memberCode, or groupCode must be provided");
// }

// 검색어 q가 지정된 경우, 제목에 해당 검색어가 포함된 게시물만 필터링
if (q != null && !q.isEmpty()) {
whereClause.and(postEntity.title.like("%" + q + "%"));
Expand All @@ -486,7 +472,8 @@ public Page<Post> searchAllByBoardIdAndGroupCodeAndMemberCode(Long boardId, Stri
.leftJoin(postFileEntity).on(postFileEntity.postEntity.eq(postEntity))
.where(whereClause)
// .orderBy(postEntity.createdAt.desc());
.orderBy(statusOrder.asc(), postEntity.createdAt.desc());
.orderBy(statusOrder.asc(), postEntity.createdAt.desc()).distinct();


List<PostEntity> content = query
.offset(pageable.getOffset())
Expand All @@ -500,12 +487,13 @@ public Page<Post> searchAllByBoardIdAndGroupCodeAndMemberCode(Long boardId, Stri
.leftJoin(memberEntity).on(memberEntity.userEntity.eq(userEntity))
.leftJoin(memberEntity.groupEntity, groupEntity)
.leftJoin(postFileEntity).on(postFileEntity.postEntity.eq(postEntity))
.where(whereClause);
.where(whereClause)
.distinct();

return PageableExecutionUtils.getPage(
content.stream().map(postMapper::toDomain).collect(Collectors.toList()),
pageable,
countQuery::fetchOne
countQuery::fetchCount
);

}
Expand Down

0 comments on commit c332741

Please sign in to comment.