Skip to content

Commit

Permalink
refactor(Board): 커뮤니티 게시글 카테고리별 조회시 댓글 개수 정보를 반환하도록 변경 (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
limehee authored May 26, 2024
1 parent f1f6ada commit f5e9ab3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public PagedResponseDto<BoardMyResponseDto> getMyBoards(Pageable pageable) {
@Transactional(readOnly = true)
public PagedResponseDto<BoardCategoryResponseDto> getBoardsByCategory(BoardCategory category, Pageable pageable) {
Page<Board> boards = getBoardByCategory(category, pageable);
return new PagedResponseDto<>(boards.map(BoardCategoryResponseDto::toDto));
return new PagedResponseDto<>(boards.map(this::mapToBoardCategoryResponseDto));
}

@Transactional
Expand Down Expand Up @@ -137,6 +137,12 @@ private BoardListResponseDto mapToBoardListResponseDto(Board board) {
return BoardListResponseDto.toDto(board, commentCount);
}

@NotNull
private BoardCategoryResponseDto mapToBoardCategoryResponseDto(Board board) {
Long commentCount = commentRepository.countByBoard(board);
return BoardCategoryResponseDto.toDto(board, commentCount);
}

public Board getBoardByIdOrThrow(Long boardId) {
return boardRepository.findById(boardId)
.orElseThrow(() -> new NotFoundException("해당 게시글이 존재하지 않습니다."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,21 @@ public class BoardCategoryResponseDto {

private String title;

private Long commentCount;

private String imageUrl;

private LocalDateTime createdAt;

public static BoardCategoryResponseDto toDto(Board board) {
public static BoardCategoryResponseDto toDto(Board board, Long commentCount) {
WriterInfo writerInfo = WriterInfo.fromBoard(board);
return BoardCategoryResponseDto.builder()
.id(board.getId())
.category(board.getCategory().getKey())
.writerId(writerInfo.getId())
.writerName(writerInfo.getName())
.title(board.getTitle())
.commentCount(commentCount)
.imageUrl(board.getImageUrl())
.createdAt(board.getCreatedAt())
.build();
Expand Down

0 comments on commit f5e9ab3

Please sign in to comment.