diff --git a/src/main/java/com/selfrunner/gwalit/domain/board/dto/response/BoardReplyRes.java b/src/main/java/com/selfrunner/gwalit/domain/board/dto/response/BoardReplyRes.java index b440a4a4..756ec10d 100644 --- a/src/main/java/com/selfrunner/gwalit/domain/board/dto/response/BoardReplyRes.java +++ b/src/main/java/com/selfrunner/gwalit/domain/board/dto/response/BoardReplyRes.java @@ -64,7 +64,7 @@ public BoardReplyRes(Board board, Member member, LocalDate lessonDate, Integer r this.category = board.getCategory(); this.status = board.getStatus(); this.fileList = fileList; - this.replyCount = replyCount; + this.replyCount = (replyCount != null) ? replyCount : 0; this.createdAt = board.getCreatedAt(); this.modifiedAt = board.getModifiedAt(); } diff --git a/src/main/java/com/selfrunner/gwalit/domain/board/repository/BoardRepositoryImpl.java b/src/main/java/com/selfrunner/gwalit/domain/board/repository/BoardRepositoryImpl.java index eed23b7a..cb12b089 100644 --- a/src/main/java/com/selfrunner/gwalit/domain/board/repository/BoardRepositoryImpl.java +++ b/src/main/java/com/selfrunner/gwalit/domain/board/repository/BoardRepositoryImpl.java @@ -18,6 +18,7 @@ import java.time.LocalDateTime; import java.util.List; import java.util.Optional; +import java.util.stream.Collectors; import static com.querydsl.core.group.GroupBy.groupBy; import static com.selfrunner.gwalit.domain.board.entity.QBoard.board; @@ -34,13 +35,13 @@ public class BoardRepositoryImpl implements BoardRepositoryCustom { @Override public Slice findBoardPaginationByCategory(Member m, Long lectureId, BoardCategory category, Long cursor, LocalDateTime cursorCreatedAt, Pageable pageable) { - List content; + List boardMetaResList; if (m.getType().equals(MemberType.TEACHER)) { - content = queryFactory.selectFrom(board) + boardMetaResList = queryFactory.selectFrom(board) .leftJoin(lecture).on(board.lecture.lectureId.eq(lecture.lectureId)) .leftJoin(member).on(board.member.memberId.eq(member.memberId)) .leftJoin(reply).on(board.boardId.eq(reply.board.boardId)) - .where(board.lecture.lectureId.eq(lectureId), eqCursorAndCursorCreatedAt(cursor, cursorCreatedAt), checkCategory(category), board.deletedAt.isNull(), reply.isNull().or(reply.deletedAt.isNull().and(reply.isNotNull()))) + .where(board.lecture.lectureId.eq(lectureId), eqCursorAndCursorCreatedAt(cursor, cursorCreatedAt), checkCategory(category), board.deletedAt.isNull()) .orderBy(board.createdAt.desc(), board.boardId.asc()) .groupBy(board.boardId) .limit(pageable.getPageSize() + 1) @@ -48,17 +49,26 @@ public Slice findBoardPaginationByCategory(Member m, Long lectureI } else { - content = queryFactory.selectFrom(board) + boardMetaResList = queryFactory.selectFrom(board) .leftJoin(lecture).on(board.lecture.lectureId.eq(lecture.lectureId)) .leftJoin(member).on(board.member.memberId.eq(member.memberId)) .leftJoin(reply).on(board.boardId.eq(reply.board.boardId)) - .where(board.lecture.lectureId.eq(lectureId), eqCursorAndCursorCreatedAt(cursor, cursorCreatedAt), board.isPublic.eq(Boolean.TRUE).or(checkWriter(m.getMemberId())), checkCategory(category), board.deletedAt.isNull(), reply.isNull().or(reply.deletedAt.isNull().and(reply.isNotNull()))) + .where(board.lecture.lectureId.eq(lectureId), eqCursorAndCursorCreatedAt(cursor, cursorCreatedAt), board.isPublic.eq(Boolean.TRUE).or(checkWriter(m.getMemberId())), checkCategory(category), board.deletedAt.isNull()) .orderBy(board.createdAt.desc(), board.boardId.asc()) .groupBy(board.boardId) .limit(pageable.getPageSize() + 1) .transform(groupBy(board.boardId).list(Projections.constructor(BoardMetaRes.class, board.boardId, lecture.lectureId, member.memberId, member.type, member.name, board.lessonId, board.title, board.body, board.category, board.status, reply.count(), board.createdAt, board.modifiedAt))); } + List content = boardMetaResList.stream() + .map(boardMetaRes -> { + Long replyCount = queryFactory.select(reply.count()) + .from(reply) + .where(reply.board.boardId.eq(boardMetaRes.getBoardId()), reply.deletedAt.isNull()) + .fetchFirst(); + return new BoardMetaRes(boardMetaRes.getBoardId(), boardMetaRes.getLectureId(), boardMetaRes.getMemberId(), boardMetaRes.getMemberType(), boardMetaRes.getMemberName(), boardMetaRes.getLessonId(), boardMetaRes.getTitle(), boardMetaRes.getBody(), boardMetaRes.getCategory(), boardMetaRes.getStatus(), replyCount, boardMetaRes.getCreatedAt(), boardMetaRes.getModifiedAt()); + }) + .collect(Collectors.toList()); // 다음 페이지 존재 여부 계산 boolean hasNext = false;