Skip to content

Commit

Permalink
refactor : #155 SliceResponse 매퍼 메서드를 게시글, 게시글 댓글 목록 페이징에 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
tnals2384 committed Sep 16, 2024
1 parent 20f09ce commit 895564c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,6 @@ public BoardInfo toBoardInfo(Board board, Integer boardHeartsCount, Boolean isLi
.build();
}

public SliceResponse<BoardInfo> toSliceBoardInfoForRedis(Member member, Slice<BoardInfoInterface> boardSlice) {
return SliceResponse.<BoardInfo>builder()
.number(boardSlice.getNumber())
.size(boardSlice.getSize())
.content(boardSlice.getContent().stream().map(boardInfoInterface -> {
int heartCount = boardHeartRedisRepository.getBoardHeartsCount(boardInfoInterface.getBoard().getId());
boolean isLiked = boardHeartRedisRepository.isMemberLikedBoard(member.getId(), boardInfoInterface.getBoard().getId());
return toBoardInfo(boardInfoInterface.getBoard(), heartCount, isLiked, boardInfoInterface.getIsMine());
})
.toList()
)
.numberOfElements(boardSlice.getNumberOfElements())
.hasPrevious(boardSlice.hasPrevious())
.hasNext(boardSlice.hasNext())
.build();
}

//rdb
public BoardInfo toBoardInfo(Board board, Boolean isLiked, Boolean isMine) {
return BoardInfo.builder()
Expand All @@ -73,19 +56,5 @@ public BoardInfo toBoardInfo(Board board, Boolean isLiked, Boolean isMine) {
.isReported(board.getIsReport())
.build();
}

public SliceResponse<BoardInfo> toSliceBoardInfo(Slice<BoardInfoInterface> boardSlice) {
return SliceResponse.<BoardInfo>builder()
.number(boardSlice.getNumber())
.size(boardSlice.getSize())
.content(boardSlice.getContent().stream().map(boardInfoInterface ->
toBoardInfo(boardInfoInterface.getBoard(), boardInfoInterface.getIsLike(), boardInfoInterface.getIsMine()))
.toList()
)
.numberOfElements(boardSlice.getNumberOfElements())
.hasPrevious(boardSlice.hasPrevious())
.hasNext(boardSlice.hasNext())
.build();
}
}

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.server.capple.domain.board.service;

import com.server.capple.domain.board.dao.BoardInfoInterface;
import com.server.capple.domain.board.dto.BoardResponse.BoardId;
import com.server.capple.domain.board.dto.BoardResponse.ToggleBoardHeart;
import com.server.capple.domain.board.entity.Board;
Expand All @@ -18,6 +19,7 @@
import com.server.capple.global.exception.errorCode.BoardErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -33,6 +35,7 @@ public class BoardServiceImpl implements BoardService {
private final BoardHeartRepository boardHeartRepository;
private final BoardHeartMapper boardHeartMapper;
private final NotificationService notificationService;
private final BoardHeartRedisRepository boardHeartRedisRepository;
private final BoardSubscribeMemberService boardSubscribeMemberService;

@Override
Expand All @@ -46,20 +49,47 @@ public BoardId createBoard(Member member, BoardType boardType, String content) {

@Override
public SliceResponse<BoardInfo> getBoardsByBoardType(Member member, BoardType boardType, Pageable pageable) {
return boardMapper.toSliceBoardInfo(boardRepository.findBoardInfosByMemberAndBoardType(member, boardType, pageable));
Slice<BoardInfoInterface> sliceBoardInfos = boardRepository.findBoardInfosByMemberAndBoardType(member, boardType, pageable);

return SliceResponse.toSliceResponse(sliceBoardInfos, sliceBoardInfos.getContent().stream().map(sliceBoardInfo ->
boardMapper.toBoardInfo(
sliceBoardInfo.getBoard(),
sliceBoardInfo.getIsLike(),
sliceBoardInfo.getIsMine()))
.toList()
);
}

@Override
public SliceResponse<BoardInfo> searchBoardsByKeyword(Member member, String keyword, Pageable pageable) {
return boardMapper.toSliceBoardInfo(boardRepository.findBoardInfosByMemberAndKeyword(member, keyword, pageable));
Slice<BoardInfoInterface> sliceBoardInfos = boardRepository.findBoardInfosByMemberAndKeyword(member, keyword, pageable);

return SliceResponse.toSliceResponse(sliceBoardInfos, sliceBoardInfos.getContent().stream().map(sliceBoardInfo ->
boardMapper.toBoardInfo(
sliceBoardInfo.getBoard(),
sliceBoardInfo.getIsLike(),
sliceBoardInfo.getIsMine()))
.toList()
);
}

/*
redis 성능 테스트 용
*/
@Override
public SliceResponse<BoardInfo> getBoardsByBoardTypeWithRedis(Member member, BoardType boardType, Pageable pageable) {
return boardMapper.toSliceBoardInfoForRedis(member, boardRepository.findBoardInfosForRedis(member, boardType, pageable));
Slice<BoardInfoInterface> sliceBoardInfos = boardRepository.findBoardInfosForRedis(member, boardType, pageable);

return SliceResponse.toSliceResponse(sliceBoardInfos, sliceBoardInfos.getContent().stream().map(sliceBoardInfo -> {
int heartCount = boardHeartRedisRepository.getBoardHeartsCount(sliceBoardInfo.getBoard().getId());
boolean isLiked = boardHeartRedisRepository.isMemberLikedBoard(member.getId(), sliceBoardInfo.getBoard().getId());
return boardMapper.toBoardInfo(
sliceBoardInfo.getBoard(),
heartCount,
isLiked,
sliceBoardInfo.getIsMine());
})
.toList());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,4 @@ public BoardCommentInfo toBoardCommentInfo(BoardComment comment, Boolean isLiked
.createdAt(comment.getCreatedAt())
.build();
}

public SliceResponse<BoardCommentInfo> toSliceBoardCommentInfo(Slice<BoardCommentInfoInterface> boardCommentSlice) {
return SliceResponse.<BoardCommentInfo>builder()
.number(boardCommentSlice.getNumber())
.size(boardCommentSlice.getSize())
.content(boardCommentSlice.getContent().stream().map(boardCommentInfoInterface ->
toBoardCommentInfo(boardCommentInfoInterface.getBoardComment(),
boardCommentInfoInterface.getIsLike(),
boardCommentInfoInterface.getIsMine()))
.toList()
)
.numberOfElements(boardCommentSlice.getNumberOfElements())
.hasPrevious(boardCommentSlice.hasPrevious())
.hasNext(boardCommentSlice.hasNext())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.server.capple.domain.board.entity.Board;
import com.server.capple.domain.board.service.BoardService;
import com.server.capple.domain.boardComment.dao.BoardCommentInfoInterface;
import com.server.capple.domain.boardComment.dto.BoardCommentRequest;
import com.server.capple.domain.boardComment.dto.BoardCommentResponse.BoardCommentId;
import com.server.capple.domain.boardComment.dto.BoardCommentResponse.BoardCommentInfo;
Expand All @@ -21,6 +22,7 @@
import com.server.capple.global.exception.errorCode.CommentErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -96,7 +98,14 @@ public ToggleBoardCommentHeart toggleBoardCommentHeart(Member member, Long board

@Override
public SliceResponse<BoardCommentInfo> getBoardCommentInfos(Member member, Long boardId, Pageable pageable) {
return boardCommentMapper.toSliceBoardCommentInfo(boardCommentRepository.findBoardCommentInfosByMemberAndBoardId(member, boardId, pageable));
Slice<BoardCommentInfoInterface> sliceBoardCommentInfos = boardCommentRepository.findBoardCommentInfosByMemberAndBoardId(member, boardId, pageable);
return SliceResponse.toSliceResponse(sliceBoardCommentInfos, sliceBoardCommentInfos.getContent().stream().map(sliceBoardCommentInfo ->
boardCommentMapper.toBoardCommentInfo(
sliceBoardCommentInfo.getBoardComment(),
sliceBoardCommentInfo.getIsLike(),
sliceBoardCommentInfo.getIsMine()))
.toList()
);
}

private void checkPermission(Member member, BoardComment boardComment) {
Expand Down

0 comments on commit 895564c

Please sign in to comment.