From 9030470fe0d14e6e655f53325769aacee9a02c15 Mon Sep 17 00:00:00 2001
From: challonsy <musain2019@naver.com>
Date: Wed, 12 Feb 2025 14:48:27 +0900
Subject: [PATCH] feat : isMyComment field added to commentDTO and
 getOneComment API created

---
 .../backend/controller/CommentController.java | 46 ++++++++++++++++++-
 .../backend/converter/CommentConverter.java   | 15 ++++--
 .../commentDTO/CommentReplyResponseDTO.java   |  2 +
 .../muit/backend/service/CommentService.java  |  2 +-
 .../backend/service/CommentServiceImpl.java   |  8 ++--
 5 files changed, 61 insertions(+), 12 deletions(-)

diff --git a/src/main/java/muit/backend/controller/CommentController.java b/src/main/java/muit/backend/controller/CommentController.java
index 54ee5dc..2a3463d 100644
--- a/src/main/java/muit/backend/controller/CommentController.java
+++ b/src/main/java/muit/backend/controller/CommentController.java
@@ -6,14 +6,22 @@
 import io.swagger.v3.oas.annotations.tags.Tag;
 import lombok.RequiredArgsConstructor;
 import muit.backend.apiPayLoad.ApiResponse;
+import muit.backend.apiPayLoad.code.status.ErrorStatus;
+import muit.backend.apiPayLoad.exception.GeneralException;
+import muit.backend.converter.CommentConverter;
+import muit.backend.domain.entity.member.Comment;
 import muit.backend.domain.entity.member.Member;
+import muit.backend.domain.entity.member.Reply;
 import muit.backend.domain.enums.PostType;
+import muit.backend.domain.enums.Role;
 import muit.backend.dto.commentDTO.CommentReplyRequestDTO;
 import muit.backend.dto.commentDTO.CommentReplyResponseDTO;
 import muit.backend.dto.postDTO.LostRequestDTO;
 import muit.backend.dto.postDTO.LostResponseDTO;
 import muit.backend.dto.reportDTO.ReportRequestDTO;
 import muit.backend.dto.reportDTO.ReportResponseDTO;
+import muit.backend.repository.CommentRepository;
+import muit.backend.repository.ReplyRepository;
 import muit.backend.service.CommentService;
 import muit.backend.service.MemberService;
 import org.springframework.web.bind.annotation.*;
@@ -26,6 +34,8 @@ public class CommentController {
 
     private final CommentService commentService;
     private final MemberService memberService;
+    private final CommentRepository commentRepository;
+    private final ReplyRepository replyRepository;
 
     @GetMapping("/{postId}")
     @Operation(summary = "댓글 조회 API", description = "특정 게시물의 댓글을 모두 조회하는 API")
@@ -37,9 +47,9 @@ public ApiResponse<CommentReplyResponseDTO.CommentListResponseDTO> getCommentLis
                                                                                       @PathVariable("postId") Long postId,
                                                                                       @RequestParam(defaultValue = "0", name = "page") Integer page,
                                                                                       @RequestParam(defaultValue = "20", name = "size") Integer size) {
-        memberService.getMemberByToken(accessToken);
+        Member member = memberService.getMemberByToken(accessToken);
 
-        return ApiResponse.onSuccess(commentService.getCommentList(postId, page, size));
+        return ApiResponse.onSuccess(commentService.getCommentList(postId, member, page, size));
     }
 
     @PostMapping("/{postId}")
@@ -80,4 +90,36 @@ public ApiResponse<ReportResponseDTO.ReportResultDTO> reportComment(@RequestHead
         Member member = memberService.getMemberByToken(accessToken);
         return ApiResponse.onSuccess(commentService.reportComment(commentType,commentId,member,requestDTO));
     }
+
+    @GetMapping("/admin/comment")
+    @Operation(summary = "댓글 단건 조회 API", description = "신고 내용 중 댓글 id로 댓글 정보 불러오는 API")
+    @Parameters({
+            @Parameter(name = "commentId", description = "댓글 아이디")
+    })
+    public ApiResponse<CommentReplyResponseDTO.CommentResponseDTO> getComment(@RequestHeader("Authorization") String accessToken,
+                                                                              @RequestParam("commentId") Long commentId) {
+        Member member = memberService.getMemberByToken(accessToken);
+        if(member.getRole()!= Role.ADMIN){
+            throw new GeneralException(ErrorStatus.MEMBER_NOT_ADMIN);
+        }
+        Comment comment = commentRepository.findById(commentId).orElseThrow(()->new GeneralException(ErrorStatus.COMMENT_NOT_FOUND));
+
+        return ApiResponse.onSuccess(CommentConverter.toCommentResponseDTO(comment,member));
+    }
+
+    @GetMapping("/admin/reply")
+    @Operation(summary = "대댓글 단건 조회 API", description = "신고 내용 중 대댓글 id로 댓글 정보 불러오는 API")
+    @Parameters({
+            @Parameter(name = "replyId", description = "대댓글 아이디")
+    })
+    public ApiResponse<CommentReplyResponseDTO.ReplyResponseDTO> getReply(@RequestHeader("Authorization") String accessToken,
+                                                                            @RequestParam("commentId") Long commentId) {
+        Member member = memberService.getMemberByToken(accessToken);
+        if(member.getRole()!= Role.ADMIN){
+            throw new GeneralException(ErrorStatus.MEMBER_NOT_ADMIN);
+        }
+        Reply reply = replyRepository.findById(commentId).orElseThrow(()->new GeneralException(ErrorStatus.COMMENT_NOT_FOUND));
+
+        return ApiResponse.onSuccess(CommentConverter.toReplyResponseDTO(reply,member));
+    }
 }
diff --git a/src/main/java/muit/backend/converter/CommentConverter.java b/src/main/java/muit/backend/converter/CommentConverter.java
index 90577d2..46876cf 100644
--- a/src/main/java/muit/backend/converter/CommentConverter.java
+++ b/src/main/java/muit/backend/converter/CommentConverter.java
@@ -94,10 +94,10 @@ public static Reply toReply(CommentReplyRequestDTO.ReplyRequestDTO requestDTO, C
 
     //Comment -> DTO
     //생성 시 답, 조회 시 단건 답
-    public static CommentReplyResponseDTO.CommentResponseDTO toCommentResponseDTO(Comment comment) {
+    public static CommentReplyResponseDTO.CommentResponseDTO toCommentResponseDTO(Comment comment, Member member) {
 
         List<CommentReplyResponseDTO.ReplyResponseDTO> replies = comment.getReplyList().stream()
-                .map(CommentConverter::toReplyResponseDTO).collect(Collectors.toList());
+                .map(reply->CommentConverter.toReplyResponseDTO(reply,member)).collect(Collectors.toList());
 
         String nickname = switch (comment.getAnonymousIndex()) {
             case -2 -> "삭제된 댓글";
@@ -106,10 +106,13 @@ public static CommentReplyResponseDTO.CommentResponseDTO toCommentResponseDTO(Co
             default -> "익명" + comment.getAnonymousIndex();
         };
 
+        boolean isMyComment = comment.getMember()==member;
+
         return CommentReplyResponseDTO.CommentResponseDTO.builder()
                 .commentId(comment.getId())
                 .content(comment.getContent())
                 .memberId(comment.getMember().getId())
+                .isMyComment(isMyComment)
                 .nickname(nickname)
                 .replies(replies)
                 .createdAt(comment.getCreatedAt())
@@ -117,9 +120,9 @@ public static CommentReplyResponseDTO.CommentResponseDTO toCommentResponseDTO(Co
     }
 
     //조회 시 리스트 형식 답
-    public static CommentReplyResponseDTO.CommentListResponseDTO toCommentListResponseDTO(Page<Comment> commentPage) {
+    public static CommentReplyResponseDTO.CommentListResponseDTO toCommentListResponseDTO(Page<Comment> commentPage, Member member) {
         List<CommentReplyResponseDTO.CommentResponseDTO> commentResultListDTO = commentPage.stream()
-                .map(CommentConverter::toCommentResponseDTO).collect(Collectors.toList());
+                .map(comment->CommentConverter.toCommentResponseDTO(comment, member)).collect(Collectors.toList());
 
         return CommentReplyResponseDTO.CommentListResponseDTO.builder()
                 .comments(commentResultListDTO)
@@ -132,7 +135,7 @@ public static CommentReplyResponseDTO.CommentListResponseDTO toCommentListRespon
 
 
     //생성 시 단건 답, 댓글 DTO 내부 대댓글 형식
-    public static CommentReplyResponseDTO.ReplyResponseDTO toReplyResponseDTO(Reply reply) {
+    public static CommentReplyResponseDTO.ReplyResponseDTO toReplyResponseDTO(Reply reply,Member member) {
 
         String nickname = switch (reply.getAnonymousIndex()) {
             case -1 -> reply.getMember().getName();
@@ -140,10 +143,12 @@ public static CommentReplyResponseDTO.ReplyResponseDTO toReplyResponseDTO(Reply
             default -> "익명" + reply.getAnonymousIndex();
         };
 
+        boolean isMyComment = reply.getMember()==member;
         return CommentReplyResponseDTO.ReplyResponseDTO.builder()
                 .commentId(reply.getComment().getId())
                 .replyId(reply.getId())
                 .memberId(reply.getMember().getId())
+                .isMyComment(isMyComment)
                 .nickname(nickname)
                 .content(reply.getContent())
                 .createdAt(reply.getCreatedAt())
diff --git a/src/main/java/muit/backend/dto/commentDTO/CommentReplyResponseDTO.java b/src/main/java/muit/backend/dto/commentDTO/CommentReplyResponseDTO.java
index 45cdc72..b1b3c97 100644
--- a/src/main/java/muit/backend/dto/commentDTO/CommentReplyResponseDTO.java
+++ b/src/main/java/muit/backend/dto/commentDTO/CommentReplyResponseDTO.java
@@ -19,6 +19,7 @@ public static class CommentResponseDTO{
         private String nickname;
         private String content;
         private Long memberId;
+        private Boolean isMyComment;
         private LocalDateTime createdAt;
         private List<ReplyResponseDTO> replies;
     }
@@ -33,6 +34,7 @@ public static class ReplyResponseDTO{
         private Long replyId;
         private String content;
         private Long memberId;
+        private Boolean isMyComment;
         private LocalDateTime createdAt;
     }
 
diff --git a/src/main/java/muit/backend/service/CommentService.java b/src/main/java/muit/backend/service/CommentService.java
index 1f2b83e..af4f738 100644
--- a/src/main/java/muit/backend/service/CommentService.java
+++ b/src/main/java/muit/backend/service/CommentService.java
@@ -9,7 +9,7 @@
 
 @Service
 public interface CommentService {
-    CommentReplyResponseDTO.CommentListResponseDTO getCommentList(Long postId, Integer page, Integer size);
+    CommentReplyResponseDTO.CommentListResponseDTO getCommentList(Long postId,Member member, Integer page, Integer size);
 
     CommentReplyResponseDTO.CommentResponseDTO writeComment(CommentReplyRequestDTO.CommentRequestDTO requestDTO, Long postId, Member member);
 
diff --git a/src/main/java/muit/backend/service/CommentServiceImpl.java b/src/main/java/muit/backend/service/CommentServiceImpl.java
index c7aae0b..5c4dfb4 100644
--- a/src/main/java/muit/backend/service/CommentServiceImpl.java
+++ b/src/main/java/muit/backend/service/CommentServiceImpl.java
@@ -34,10 +34,10 @@ public class CommentServiceImpl implements CommentService {
     //특정 게시물의 모든 댓글 조회
     @Transactional(readOnly = true)
     @Override
-    public CommentReplyResponseDTO.CommentListResponseDTO getCommentList(Long postId, Integer page, Integer size) {
+    public CommentReplyResponseDTO.CommentListResponseDTO getCommentList(Long postId, Member member, Integer page, Integer size) {
         Post post = postRepository.findById(postId).orElseThrow(()->new GeneralException(ErrorStatus.POST_NOT_FOUND));
         Page<Comment> commentPage = commentRepository.findAllByPost(post, PageRequest.of(page, size));
-        return CommentConverter.toCommentListResponseDTO(commentPage);
+        return CommentConverter.toCommentListResponseDTO(commentPage,member);
     }
 
     //특정 게시물에 댓글 생성
@@ -51,7 +51,7 @@ public CommentReplyResponseDTO.CommentResponseDTO writeComment(CommentReplyReque
         post.changeCommentCount(true);
 
         //comment -> responseDTO
-        return CommentConverter.toCommentResponseDTO(comment);
+        return CommentConverter.toCommentResponseDTO(comment,member);
     }
 
     //특정 댓글에 대댓글 생성
@@ -65,7 +65,7 @@ public CommentReplyResponseDTO.ReplyResponseDTO writeReply(CommentReplyRequestDT
         replyRepository.save(reply);
         comment.getPost().changeCommentCount(true);
 
-        return CommentConverter.toReplyResponseDTO(reply);
+        return CommentConverter.toReplyResponseDTO(reply,member);
     }
 
     //특정 댓글 삭제