Skip to content

Commit

Permalink
Merge pull request #85 from Leets-Official/refactor-#84
Browse files Browse the repository at this point in the history
#84 Refactor: 엔티티 참조 관계 수정
  • Loading branch information
KoungQ authored Aug 1, 2024
2 parents 9853f4f + d7d6829 commit 206742f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 25 deletions.
9 changes: 7 additions & 2 deletions src/main/java/leets/weeth/domain/post/entity/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,22 @@ public class Comment extends BaseEntity {
@Column(nullable = false)
private Boolean isDeleted;

@OneToMany(orphanRemoval = true)
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private Comment parent;

@OneToMany(mappedBy = "parent", cascade = CascadeType.REMOVE, fetch = FetchType.LAZY)
private List<Comment> children = new ArrayList<>();

LocalDateTime time;

public static Comment createComment(RequestCommentDTO dto, Post post, User user){
public static Comment createComment(RequestCommentDTO dto, Post post, User user, Comment parent){
return Comment.builder()
.post(post)
.user(user)
.content(dto.getContent())
.isDeleted(false)
.parent(parent)
.build();
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/leets/weeth/domain/post/entity/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class Post extends BaseEntity {
@Column(columnDefinition = "TEXT")
private String content;

@OneToMany(orphanRemoval = true)
@OneToMany(mappedBy = "post", orphanRemoval = true)
@JsonManagedReference
private List<Comment> parentComments = new ArrayList<>();

Expand Down
43 changes: 21 additions & 22 deletions src/main/java/leets/weeth/domain/post/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
import leets.weeth.domain.post.repository.PostRepository;
import leets.weeth.domain.user.entity.User;
import leets.weeth.domain.user.repository.UserRepository;
import leets.weeth.global.common.error.exception.custom.*;
import leets.weeth.global.common.error.exception.custom.CommentNotFoundException;
import leets.weeth.global.common.error.exception.custom.PostNotFoundException;
import leets.weeth.global.common.error.exception.custom.UserMismatchException;
import leets.weeth.global.common.error.exception.custom.UserNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
Expand All @@ -20,30 +24,25 @@ public class CommentService {
private final UserRepository userRepository;
private final CommentRepository commentRepository;

@Transactional
public void createComment(Long userId, Long postId, RequestCommentDTO requestCommentDTO) {
User user = userRepository.findById(userId)
.orElseThrow(UserNotFoundException::new);
// 해당 유저가 없는 경우
Post currentPost = postRepository.findById(postId).orElseThrow(PostNotFoundException::new);
// 해당 게시물이 없는 경우

Comment newComment = Comment.createComment(requestCommentDTO, currentPost, user);
Comment parentComment;
commentRepository.save(newComment);
// 댓글이 child인 경우(부모가 있는 경우)
if(requestCommentDTO.getParentCommentId()!=null){
parentComment = commentRepository.findById(requestCommentDTO.getParentCommentId())
.orElseThrow(CommentNotFoundException::new);
parentComment.addChild(newComment);
// 자식 추가
commentRepository.save(parentComment);
}
else{
// 댓글이 부모인 경우
currentPost.addComment(newComment);
postRepository.save(currentPost);
}
postRepository.save(currentPost);

Post post = postRepository.findById(postId).orElseThrow(PostNotFoundException::new);

Comment parent = null;
if(requestCommentDTO.getParentCommentId() != null)
parent = commentRepository.findById(requestCommentDTO.getParentCommentId())
.orElseThrow(CommentNotFoundException::new);

Comment comment = Comment.createComment(requestCommentDTO, post, user, parent);
commentRepository.save(comment);

if(parent == null)
post.addComment(comment);
else
parent.addChild(comment);
}

@Transactional
Expand Down

0 comments on commit 206742f

Please sign in to comment.