Skip to content

Commit

Permalink
Merge pull request #57 from inje-megabrain/feat/comment
Browse files Browse the repository at this point in the history
Refector: commentAPI
  • Loading branch information
sleeg00 authored Oct 30, 2023
2 parents 91a29d9 + d12f1ec commit 89d166d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
17 changes: 16 additions & 1 deletion src/main/java/com/example/just/Dao/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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--;
}
}
}
9 changes: 8 additions & 1 deletion src/main/java/com/example/just/Dao/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/example/just/Service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

}
}

0 comments on commit 89d166d

Please sign in to comment.