diff --git a/src/main/java/mvp/deplog/domain/comment/application/CommentService.java b/src/main/java/mvp/deplog/domain/comment/application/CommentService.java index d5d1576..0cb46f6 100644 --- a/src/main/java/mvp/deplog/domain/comment/application/CommentService.java +++ b/src/main/java/mvp/deplog/domain/comment/application/CommentService.java @@ -29,59 +29,65 @@ public class CommentService { public SuccessResponse> getCommentList(Long postId){ Post post = postRepository.findById(postId) .orElseThrow(() -> new IllegalArgumentException("해당하는 아이디의 게시글이 없습니다: " + postId)); - List commentList = commentRepository.findByPost(post); + + List commentList = commentRepository.findByPostAndParentCommentIsNull(post); + List replyList = commentRepository.findByPostAndParentCommentIsNotNull(post); List commentDetails = new ArrayList<>(); for(Comment comment : commentList) { - Avatar avatar; - if (comment.isUseNickname()) { - avatar = Avatar.builder() - .avatarFace(null) - .avatarBody(null) - .avatarEyes(null) - .avatarNose(null) - .avatarMouth(null) - .build(); - } else { - Member member = comment.getMember(); - avatar = Avatar.builder() - .avatarFace(member.getAvatarFace()) - .avatarBody(member.getAvatarBody()) - .avatarEyes(member.getAvatarEyes()) - .avatarNose(member.getAvatarNose()) - .avatarMouth(member.getAvatarMouth()) - .build(); - } + Avatar avatar = commentAvatar(comment); + + CommentListRes commentListRes = CommentListRes.builder() + .commentId(comment.getId()) + .avatar(avatar) + .nickname(comment.getNickname()) + .createdDate(comment.getCreatedDate().toLocalDate()) + .content(comment.getContent()) + .replyList(new ArrayList<>()) // 대댓글 리스트 초기화 + .build(); + + commentDetails.add(commentListRes); + } - if(comment.getParentComment() == null){ - CommentListRes commentListRes = CommentListRes.builder() - .commentId(comment.getId()) - .avatar(avatar) - .nickname(comment.getNickname()) - .createdDate(comment.getCreatedDate().toLocalDate()) - .content(comment.getContent()) - .replyList(new ArrayList<>()) // 대댓글 리스트 초기화 - .build(); + for(Comment reply : replyList) { + Avatar avatar = commentAvatar(reply); - commentDetails.add(commentListRes); - } else { - ReplyListRes replyListRes = ReplyListRes.builder() - .commentId(comment.getId()) - .parentCommentId(comment.getParentComment().getId()) - .avatar(avatar) - .nickname(comment.getNickname()) - .createdDate(comment.getCreatedDate().toLocalDate()) - .content(comment.getContent()) - .build(); + ReplyListRes replyListRes = ReplyListRes.builder() + .commentId(reply.getId()) + .parentCommentId(reply.getParentComment().getId()) + .avatar(avatar) + .nickname(reply.getNickname()) + .createdDate(reply.getCreatedDate().toLocalDate()) + .content(reply.getContent()) + .build(); - commentDetails.stream() - .filter(parentComment -> parentComment.getCommentId().equals(comment.getParentComment().getId())) - .findFirst() - .ifPresent(parentComment -> parentComment.getReplyList().add(replyListRes)); - } + commentDetails.stream() + .filter(parentComment -> parentComment.getCommentId().equals(reply.getParentComment().getId())) + .findFirst() + .ifPresent(parentComment -> parentComment.getReplyList().add(replyListRes)); } return SuccessResponse.of(commentDetails); } + + private Avatar commentAvatar(Comment comment){ + if(!comment.isUseNickname()) { + Member member = comment.getMember(); + return Avatar.builder() + .avatarFace(member.getAvatarFace()) + .avatarBody(member.getAvatarBody()) + .avatarEyes(member.getAvatarEyes()) + .avatarNose(member.getAvatarNose()) + .avatarMouth(member.getAvatarMouth()) + .build(); + } + return Avatar.builder() + .avatarFace(null) + .avatarBody(null) + .avatarEyes(null) + .avatarNose(null) + .avatarMouth(null) + .build(); + } } diff --git a/src/main/java/mvp/deplog/domain/comment/domain/repository/CommentRepository.java b/src/main/java/mvp/deplog/domain/comment/domain/repository/CommentRepository.java index 8da159e..5ea1b61 100644 --- a/src/main/java/mvp/deplog/domain/comment/domain/repository/CommentRepository.java +++ b/src/main/java/mvp/deplog/domain/comment/domain/repository/CommentRepository.java @@ -8,7 +8,15 @@ public interface CommentRepository extends JpaRepository { - List findByPost(Post post); + // 댓글 목록 조히 + List findByPostAndParentCommentIsNull(Post post); - void deleteByPost(Post post); + // 대댓글 목록 조회 + List findByPostAndParentCommentIsNotNull(Post post); + + // 대댓글 삭제 + void deleteByPostAndParentCommentIsNotNull(Post post); + + // 댓글 삭제 + void deleteByPostAndParentCommentIsNull(Post post); } diff --git a/src/main/java/mvp/deplog/domain/comment/dto/request/CreateCommentReq.java b/src/main/java/mvp/deplog/domain/comment/dto/request/CreateCommentReq.java index 9d4c25d..606527f 100644 --- a/src/main/java/mvp/deplog/domain/comment/dto/request/CreateCommentReq.java +++ b/src/main/java/mvp/deplog/domain/comment/dto/request/CreateCommentReq.java @@ -1,6 +1,7 @@ package mvp.deplog.domain.comment.dto.request; import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; import lombok.Data; @Data @@ -13,11 +14,13 @@ public class CreateCommentReq { private Long postId; @Schema(type = "String", example = "**댓글 내용**", description = "댓글 내용입니다.") + @NotBlank private String content; @Schema(type = "String", example = "닉네임", description = "닉네임입니다.") + @NotBlank private String nickname; - @Schema(type = "boolean", example = "true", description = "닉네임 사용 체크박스 체크 여부입니다. 회원이 체크 안한 경우, 비회원의 경우 모두 true입니다. 회원이 누른 경우만 falseh입니다.") + @Schema(type = "boolean", example = "true", description = "닉네임 사용 체크박스 체크 여부입니다. 회원이 체크 안한 경우, 비회원의 경우 모두 true입니다. 회원이 누른 경우만 false입니다.") private boolean useNicknameChecked; } diff --git a/src/main/java/mvp/deplog/domain/comment/presentation/CommentApi.java b/src/main/java/mvp/deplog/domain/comment/presentation/CommentApi.java index 6fd3995..c3b9d5f 100644 --- a/src/main/java/mvp/deplog/domain/comment/presentation/CommentApi.java +++ b/src/main/java/mvp/deplog/domain/comment/presentation/CommentApi.java @@ -8,6 +8,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; import mvp.deplog.domain.comment.dto.request.CreateCommentReq; import mvp.deplog.domain.comment.dto.response.CommentListRes; import mvp.deplog.global.common.Message; @@ -40,7 +41,7 @@ public interface CommentApi { @PostMapping ResponseEntity> createComment( @Parameter(description = "Access Token을 입력해주세요.", required = true) @AuthenticationPrincipal UserDetailsImpl userDetails, - @Parameter(description = "Schemas의 CommentReq를 참고해주세요.", required = true) @RequestBody CreateCommentReq createCommentReq + @Parameter(description = "Schemas의 CommentReq를 참고해주세요.", required = true) @Valid @RequestBody CreateCommentReq createCommentReq ); @Operation(summary = "댓글 목록 조회 API", description = "해당 게시글을 댓글 목록을 조회합니다.") diff --git a/src/main/java/mvp/deplog/domain/comment/presentation/CommentController.java b/src/main/java/mvp/deplog/domain/comment/presentation/CommentController.java index 28a97a9..e7ac15e 100644 --- a/src/main/java/mvp/deplog/domain/comment/presentation/CommentController.java +++ b/src/main/java/mvp/deplog/domain/comment/presentation/CommentController.java @@ -1,5 +1,6 @@ package mvp.deplog.domain.comment.presentation; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import mvp.deplog.domain.comment.application.CommentService; import mvp.deplog.domain.comment.application.CreateCommentService; @@ -26,7 +27,8 @@ public class CommentController implements CommentApi { @Override @PostMapping - public ResponseEntity> createComment(@AuthenticationPrincipal UserDetailsImpl userDetails, @RequestBody CreateCommentReq createCommentReq) { + public ResponseEntity> createComment(@AuthenticationPrincipal UserDetailsImpl userDetails, + @Valid @RequestBody CreateCommentReq createCommentReq) { CreateCommentService createCommentService = createCommentServiceFactory.find(userDetails, createCommentReq.getParentCommentId()); return ResponseEntity .status(HttpStatus.CREATED) diff --git a/src/main/java/mvp/deplog/domain/post/application/PostService.java b/src/main/java/mvp/deplog/domain/post/application/PostService.java index be33a6a..4f55a61 100644 --- a/src/main/java/mvp/deplog/domain/post/application/PostService.java +++ b/src/main/java/mvp/deplog/domain/post/application/PostService.java @@ -222,8 +222,11 @@ public SuccessResponse deletePost(Long memberId, Long postId) { throw new UnauthorizedException("본인이 작성한 게시글이 아니므로 삭제할 수 없습니다."); } + // 게시글 내 대댓글 삭제 + commentRepository.deleteByPostAndParentCommentIsNotNull(post); + // 게시글 내 댓글 삭제 - commentRepository.deleteByPost(post); + commentRepository.deleteByPostAndParentCommentIsNull(post); // 게시글 내 태그 삭제 taggingRepository.deleteByPost(post);