Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refector: commentAPI #57

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

}
}

Loading