From 4164f72a5d0e9bf45f3f7590fd067ce21e334aad Mon Sep 17 00:00:00 2001
From: sleeg00 <slee000220@gmail.com>
Date: Mon, 30 Oct 2023 19:04:35 +0900
Subject: [PATCH] Refector: commentAPI

---
 .../just/Controller/CommentController.java     |  9 +++++++++
 .../java/com/example/just/Dao/Comment.java     | 17 ++++++++++++++++-
 src/main/java/com/example/just/Dao/Member.java |  9 ++++++++-
 .../example/just/Service/CommentService.java   | 18 ++++++++++++++++++
 4 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/src/main/java/com/example/just/Controller/CommentController.java b/src/main/java/com/example/just/Controller/CommentController.java
index 81502bfb..67eb0d3d 100644
--- a/src/main/java/com/example/just/Controller/CommentController.java
+++ b/src/main/java/com/example/just/Controller/CommentController.java
@@ -69,4 +69,13 @@ public ResponseEntity<String> blameComment(@PathVariable Long postId, @PathVaria
     public int blameGetComment(@PathVariable Long postId, @PathVariable Long commentId) {
         return commentService.blameGetComment(postId, commentId);
     }
+
+    @ApiOperation(value = "댓글 좋아요")
+    @PostMapping("/post/like/comment/{postId}/{commentId}")
+    public void likeComment(@PathVariable Long postId, @PathVariable Long commentId,
+                            HttpServletRequest req) {
+        String token = jwtProvider.getAccessToken(req);
+        Long member_id = Long.valueOf(jwtProvider.getIdFromToken(token));
+        commentService.likeComment(postId, commentId, member_id);
+    }
 }
diff --git a/src/main/java/com/example/just/Dao/Comment.java b/src/main/java/com/example/just/Dao/Comment.java
index dfd5919c..0eaa443c 100644
--- a/src/main/java/com/example/just/Dao/Comment.java
+++ b/src/main/java/com/example/just/Dao/Comment.java
@@ -54,9 +54,24 @@ public class Comment {
 
     @Column(name = "blamed_count")
     private int blamedCount;
-
+    @ManyToMany(mappedBy = "likedComments")
+    @JsonIgnore
+    private List<Member> likedMembers = new ArrayList<>();
     public void addBlamed(){
         blamedCount++;
     }
+    public void addLike(Member member) {
+        if (!likedMembers.contains(member)) {
+            System.out.println("멤버가 존재하지 않음 ");
+            member.getLikedComments().add(this);//좋아한 글 List에 해당 글의 객체 추가
+            comment_like++;
+        }
+    }
 
+    public void removeLike(Member member) {
+        if (likedMembers.contains(member)) {
+            member.getLikedComments().remove(this);
+            comment_like--;
+        }
+    }
 }
diff --git a/src/main/java/com/example/just/Dao/Member.java b/src/main/java/com/example/just/Dao/Member.java
index f1fb00ef..afb4db2f 100644
--- a/src/main/java/com/example/just/Dao/Member.java
+++ b/src/main/java/com/example/just/Dao/Member.java
@@ -55,7 +55,14 @@ public class Member {
             inverseJoinColumns = @JoinColumn(name = "post_id")
       )
       private List<Post> likedPosts = new ArrayList<>();
-
+      @Builder.Default
+      @ManyToMany(cascade = CascadeType.ALL)
+      @JoinTable(
+              name = "comment_like",
+              joinColumns = @JoinColumn(name = "member_id"),
+              inverseJoinColumns = @JoinColumn(name = "comment_id")
+      )
+      private List<Comment> likedComments = new ArrayList<>();
       public void addBlamed(){
           blamedCount++;
       }
diff --git a/src/main/java/com/example/just/Service/CommentService.java b/src/main/java/com/example/just/Service/CommentService.java
index a42dead4..b3d9c3c3 100644
--- a/src/main/java/com/example/just/Service/CommentService.java
+++ b/src/main/java/com/example/just/Service/CommentService.java
@@ -10,6 +10,7 @@
 import com.example.just.Repository.PostRepository;
 import java.util.NoSuchElementException;
 import java.util.Optional;
+import javax.transaction.Transactional;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
@@ -116,5 +117,22 @@ public int blameGetComment(Long postId, Long commentId) {
                 .orElseThrow(() -> new RuntimeException("게시물이 존재하지 않습니다."));
         return comment.getBlamedCount();
     }
+
+    @Transactional
+    public void likeComment(Long postId, Long commentId, Long member_id) {
+        Comment comment = commentRepository.findById(commentId)
+                .orElseThrow(() -> new NoSuchElementException("댓글이 존재하지 않습니다: " + commentId));
+        Member member = memberRepository.findById(member_id).orElseGet(() -> new Member());
+        Post post = postRepository.findById(postId)
+                .orElseThrow(() -> new RuntimeException("게시물이 존재하지 않습니다."));
+        if (comment.getLikedMembers().contains(member)) {
+            comment.removeLike(member);
+        } else {
+            comment.addLike(member);
+        }
+
+       commentRepository.save(comment);
+
+    }
 }