Skip to content

Commit

Permalink
♻️ refactor: 댓글 ResponseDTO 수정, 비밀 댓글 이미지 처리 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
jinho7 committed Aug 17, 2024
1 parent 072328d commit cd64c73
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ public class BoardController {
private final BoardCommandService boardCommandService;

// QueryService
// TODO : 내가 쓴 게시글
// TODO : 내가 댓글 단 게시글
@Operation(summary = "게시글 목록 조회 (전체 및 카테고리별)", description = "커뮤니티 게시글 목록을 전체 또는 카테고리별로 무한 스크롤 방식으로 조회합니다..")
@GetMapping
public ApiResponse<BoardResponseDTO.BoardListDTO> getBoardList(
Expand Down Expand Up @@ -93,6 +91,7 @@ public ApiResponse<BoardResponseDTO.BoardImgDTO> uploadBoardImages(
public ResponseEntity<ApiResponse<BoardResponseDTO.BoardDTO>> createBoard(
@Valid @RequestBody BoardRequestDTO.CreateBoardDTO createBoardDTO,
@AuthenticatedMember Member member) {
// TODO: 어차피 게시글 목록 페이지로 리다이렉트 되어서, boardId만 Return 하는 방식 고려
return ResponseEntity
.status(HttpStatus.CREATED)
.body(ApiResponse.onSuccess(HttpStatus.CREATED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,13 @@ public static class CommentImgDTO {
public static class CommentDTO {
private Long id;
private String content;
private boolean secret;
private Long memberId;
private String memberName;
private LocalDateTime createdAt;
private List<String> images;
private boolean deleted;
private Long parentId;
// 현재 사용자가 댓글 작성자인지 여부
private boolean author; // true인 경우: 댓글 수정/삭제 옵션을 표시
// 현재 사용자가 비밀 댓글을 볼 수 있는지 여부 (게시글 작성자 or 댓글본인)
private boolean canViewSecret; // true인 경우: 비밀 댓글의 실제 내용을 표시
private List<CommentDTO> replies;

public static CommentDTO from(Comment comment, Member currentMember) {
Expand All @@ -50,22 +46,17 @@ public static CommentDTO from(Comment comment, Member currentMember) {
.id(comment.getId())
// 부모 댓글이 있는 경우 부모 댓글의 ID를 설정
.parentId(comment.getParent() != null ? comment.getParent().getId() : null)
.content(getCommentContent(comment, canViewSecret)).secret(comment.isSecret())
.content(getCommentContent(comment, canViewSecret))
// 삭제된 댓글인 경우 작성자 ID를 null로 설정
.memberId(comment.isDeleted() ? null : comment.getMember().getId())
// 삭제된 댓글인 경우 작성자 이름을 "(삭제)"로 표시
.memberName(comment.isDeleted() ? DELETED_MEMBER_NAME : comment.getMember().getName())
.createdAt(comment.getCreatedAt())
// 삭제된 댓글인 경우 이미지 목록을 비움, 그렇지 않으면 이미지 URL 목록 생성
.images(comment.isDeleted() ? Collections.emptyList() :
comment.getImages().stream().map(CommentImg::getCommentImgUrl).toList())
.deleted(comment.isDeleted())
.images(getCommentImages(comment, canViewSecret))
// 현재 사용자가 댓글 작성자이고 댓글이 삭제되지 않았을 경우 true
.author(isCommentAuthor && !comment.isDeleted())
// 비밀 댓글을 볼 수 있는 권한이 있고 댓글이 삭제되지 않았을 경우 true
.canViewSecret(canViewSecret && !comment.isDeleted())
// comment.getChildren()이 null일 경우 빈 리스트를 반환하고,
// null이 아닐 경우 모든 자식 댓글을 CommentDTO로 변환
// 대댓글
.replies(Optional.ofNullable(comment.getChildren())
.map(children -> children.stream()
.map(childComment -> CommentDTO.from(childComment, currentMember))
Expand Down Expand Up @@ -96,6 +87,13 @@ private static String getCommentContent(Comment comment, boolean canViewSecret)
}
return comment.getContent();
}

private static List<String> getCommentImages(Comment comment, boolean canViewSecret) {
if (comment.isDeleted() || (comment.isSecret() && !canViewSecret)) {
return Collections.emptyList();
}
return comment.getImages().stream().map(CommentImg::getCommentImgUrl).toList();
}
}

@Getter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.example.template.domain.board.service.queryService;

import com.example.template.domain.board.dto.response.CommentResponseDTO;
import com.example.template.domain.board.entity.Board;
import com.example.template.domain.board.entity.Comment;
import com.example.template.domain.board.exception.BoardErrorCode;
import com.example.template.domain.board.exception.BoardException;
import com.example.template.domain.board.repository.BoardRepository;
import com.example.template.domain.board.repository.CommentRepository;
import com.example.template.domain.member.entity.Member;
import lombok.RequiredArgsConstructor;
Expand All @@ -22,7 +18,6 @@
public class CommentQueryServiceImpl implements CommentQueryService {

private final CommentRepository commentRepository;
private final BoardRepository boardRepository;

@Override
public CommentResponseDTO.CommentsListDTO getCommentsList(Long boardId, Member member) {
Expand Down

0 comments on commit cd64c73

Please sign in to comment.