Skip to content

Commit

Permalink
refactor: CommentService 변수명, 메서드 보완 (#5)
Browse files Browse the repository at this point in the history
변수명, 메서드명 보안, 누락 final 키워드 추가
  • Loading branch information
toychip committed Dec 24, 2023
1 parent 4143ae8 commit 28af44c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
24 changes: 18 additions & 6 deletions src/main/java/com/api/TaveShot/domain/Comment/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,7 +50,7 @@ public class Comment {
private Comment parentComment; // 부모 댓글

@OneToMany(mappedBy = "parentComment", fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, orphanRemoval = true)
private List<Comment> childComments; // 자식 댓글들
private List<Comment> childComments = new ArrayList<>(); // 자식 댓글들

/* 댓글 수정 */
public void update(String comment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 어떤 티어 게시판인지 검증
Expand All @@ -39,48 +39,47 @@ public Long register(Long postId, CommentCreateRequest request) {
// ---------------- 부모 댓글 유무 확인 ----------------
Long parentCommentId = request.getParentCommentId();

Optional<Comment> findParentComment = validatedParent(parentCommentId);
Optional<Comment> 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<Comment> 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<Comment> 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<CommentResponse> findAll(Long postId, Pageable pageable) {
Post post = getPost(postId);
List<Comment> comments = commentRepository.findByParentCommentIsNull(post);
Expand Down

0 comments on commit 28af44c

Please sign in to comment.