From 28af44ca364475df337fdd07fe7aba06b3a8ee1b Mon Sep 17 00:00:00 2001 From: toychip Date: Sun, 24 Dec 2023 19:10:14 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20CommentService=20=EB=B3=80=EC=88=98?= =?UTF-8?q?=EB=AA=85,=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B3=B4=EC=99=84?= =?UTF-8?q?=20(#5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 변수명, 메서드명 보안, 누락 final 키워드 추가 --- .../Comment/converter/CommentConverter.java | 5 ++- .../domain/Comment/domain/Comment.java | 24 +++++++++--- .../Comment/service/CommentService.java | 39 +++++++++---------- 3 files changed, 40 insertions(+), 28 deletions(-) diff --git a/src/main/java/com/api/TaveShot/domain/Comment/converter/CommentConverter.java b/src/main/java/com/api/TaveShot/domain/Comment/converter/CommentConverter.java index b9eedb0..b943781 100644 --- a/src/main/java/com/api/TaveShot/domain/Comment/converter/CommentConverter.java +++ b/src/main/java/com/api/TaveShot/domain/Comment/converter/CommentConverter.java @@ -7,7 +7,7 @@ public class CommentConverter { - public static Comment createDtoToEntity(String comment, Member member, Post post) { + public static Comment createDtoToEntity(final String comment, final Member member, final Post post) { return Comment.builder() .comment(comment) .member(member) @@ -16,7 +16,8 @@ public static Comment createDtoToEntity(String comment, Member member, Post post .build(); } - public static Comment createDtoToEntity(String comment, Member member, Post post, Comment parentComment) { + public static Comment createDtoToEntity(final String comment, final Member member, + final Post post, final Comment parentComment) { return Comment.builder() .comment(comment) .member(member) diff --git a/src/main/java/com/api/TaveShot/domain/Comment/domain/Comment.java b/src/main/java/com/api/TaveShot/domain/Comment/domain/Comment.java index b17004b..becb3ad 100644 --- a/src/main/java/com/api/TaveShot/domain/Comment/domain/Comment.java +++ b/src/main/java/com/api/TaveShot/domain/Comment/domain/Comment.java @@ -2,12 +2,24 @@ import com.api.TaveShot.domain.Member.domain.Member; import com.api.TaveShot.domain.post.post.domain.Post; -import jakarta.persistence.*; -import lombok.*; -import org.springframework.data.annotation.CreatedDate; -import org.springframework.data.annotation.LastModifiedDate; - +import jakarta.persistence.CascadeType; +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; +import jakarta.persistence.OneToMany; +import jakarta.persistence.Table; +import java.util.ArrayList; import java.util.List; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; @Builder @@ -38,7 +50,7 @@ public class Comment { private Comment parentComment; // 부모 댓글 @OneToMany(mappedBy = "parentComment", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, orphanRemoval = true) - private List childComments; // 자식 댓글들 + private List childComments = new ArrayList<>(); // 자식 댓글들 /* 댓글 수정 */ public void update(String comment) { diff --git a/src/main/java/com/api/TaveShot/domain/Comment/service/CommentService.java b/src/main/java/com/api/TaveShot/domain/Comment/service/CommentService.java index e95fcd0..97ae114 100644 --- a/src/main/java/com/api/TaveShot/domain/Comment/service/CommentService.java +++ b/src/main/java/com/api/TaveShot/domain/Comment/service/CommentService.java @@ -30,7 +30,7 @@ public class CommentService { private final PostRepository postRepository; @Transactional // 데이터 변경하는 메서드에만 명시적으로 적용 - public Long register(Long postId, CommentCreateRequest request) { + public Long register(final Long postId, final CommentCreateRequest request) { Member currentMember = getCurrentMember(); // ToDo 어떤 티어 게시판인지 검증 @@ -39,48 +39,47 @@ public Long register(Long postId, CommentCreateRequest request) { // ---------------- 부모 댓글 유무 확인 ---------------- Long parentCommentId = request.getParentCommentId(); - Optional findParentComment = validatedParent(parentCommentId); + Optional parentCommentOptional = findParentComment(parentCommentId); - if (findParentComment.isPresent()) { - Comment findParentCommentValue = findParentComment.get(); - return createWithParent(request, currentMember, post, findParentCommentValue); + if (parentCommentOptional.isPresent()) { + Comment parentComment = parentCommentOptional.get(); + return createWithParent(request, currentMember, post, parentComment); } return createNotParent(request, currentMember, post); } - private Optional validatedParent(Long parentCommentId) { + private Member getCurrentMember() { + return SecurityUtil.getCurrentMember(); + } + + private Post getPost(final Long postId) { + return postRepository.findById(postId) + .orElseThrow(() -> new ApiException(ErrorType._POST_NOT_FOUND)); + } + + private Optional findParentComment(final Long parentCommentId) { if (parentCommentId != null) { return commentRepository.findById(parentCommentId); } - return Optional.empty(); } - private Long createWithParent(CommentCreateRequest request, Member currentMember, Post post, - Comment findParentComment) { + private Long createWithParent(final CommentCreateRequest request, final Member currentMember, + final Post post, final Comment findParentComment) { Comment comment = CommentConverter.createDtoToEntity(request.getComment(), currentMember, post, findParentComment); commentRepository.save(comment); return comment.getId(); } - private Long createNotParent(CommentCreateRequest request, Member currentMember, Post post) { + private Long createNotParent(final CommentCreateRequest request, final Member currentMember, + final Post post) { Comment comment = CommentConverter.createDtoToEntity(request.getComment(), currentMember, post); commentRepository.save(comment); return comment.getId(); } - private Post getPost(Long postId) { - Post post = postRepository.findById(postId) - .orElseThrow(() -> new ApiException(ErrorType._POST_NOT_FOUND)); - return post; - } - - private Member getCurrentMember() { - return SecurityUtil.getCurrentMember(); - } - public List findAll(Long postId, Pageable pageable) { Post post = getPost(postId); List comments = commentRepository.findByParentCommentIsNull(post);