Skip to content

Commit

Permalink
refactor: 에러 컨벤션 적용, setter 제거 (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonbinn committed Dec 16, 2023
1 parent e034bdd commit 9ab5049
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,5 @@ public class Comment {
public void update(String comment) {
this.comment = comment;
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.api.TaveShot.domain.Comment.dto;
package com.api.TaveShot.domain.Comment.dto.request;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.api.TaveShot.domain.Comment.dto;
package com.api.TaveShot.domain.Comment.dto.request;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.api.TaveShot.domain.Comment.dto;
package com.api.TaveShot.domain.Comment.dto.response;

import com.api.TaveShot.domain.Comment.domain.Comment;
import com.querydsl.core.annotations.QueryProjection;
import lombok.Getter;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -23,7 +24,7 @@ public CommentResponse(Long id, String comment, String memberId, Long postId, Co
this.memberId = memberId;
this.postId = postId;
this.parentComment = parentComment;
this.replies = replies;
this.replies = Collections.emptyList();
}

public static CommentResponse fromEntity(Comment commentEntity) {
Expand All @@ -34,7 +35,7 @@ public static CommentResponse fromEntity(Comment commentEntity) {
return new CommentResponse(
commentEntity.getId(),
commentEntity.getComment(),
commentEntity.getMember().getGitName(),
commentEntity.getMember().getGitLoginId(),
commentEntity.getPost().getId(),
commentEntity.getParentComment() != null ? fromEntity(commentEntity.getParentComment()) : null,
replies
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.api.TaveShot.domain.Comment.service;

import com.api.TaveShot.domain.Comment.domain.Comment;
import com.api.TaveShot.domain.Comment.domain.CommentRepository;
import com.api.TaveShot.domain.Comment.dto.CommentDto;
import com.api.TaveShot.domain.Comment.dto.request.CommentCreateRequest;
import com.api.TaveShot.domain.Comment.dto.request.CommentUpdateRequest;
import com.api.TaveShot.domain.Comment.dto.response.CommentResponse;
import com.api.TaveShot.domain.Comment.repository.CommentRepository;
import com.api.TaveShot.domain.Member.domain.Member;
import com.api.TaveShot.domain.Post.domain.Post;
import com.api.TaveShot.domain.Post.domain.PostRepository;
import com.api.TaveShot.domain.Post.repository.PostRepository;
import com.api.TaveShot.global.exception.ApiException;
import com.api.TaveShot.global.exception.ErrorType;
import com.api.TaveShot.global.util.SecurityUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -14,88 +18,85 @@
import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class CommentService {

private final CommentRepository commentRepository;
private final PostRepository postRepository;

/* CREATE */
@Transactional
public Long save(Long id, Long gitLoginId, CommentDto.Request dto) {
@Transactional // 데이터 변경하는 메서드에만 명시적으로 적용
public Long save(Long postId, CommentCreateRequest dto) {
Member currentMember = SecurityUtil.getCurrentMember();
Post post = postRepository.findById(id).orElseThrow(() ->
new IllegalArgumentException("댓글 쓰기 실패: 해당 게시글이 존재하지 않습니다. " + id));

dto.setMember(currentMember);
dto.setPost(post);

Comment comment = dto.toEntity();
Post post = postRepository.findById(postId)
.orElseThrow(() -> new ApiException(ErrorType._POST_NOT_FOUND));

Comment comment = Comment.builder()
.comment(dto.getComment())
.member(currentMember)
.post(post)
.parentComment(null) // 대댓글이 아닌 경우 parentComment는 null
.build();
commentRepository.save(comment);

return comment.getId();
}

/* READ */
@Transactional(readOnly = true)
public List<CommentDto.Response> findAll(Long id) {
Post post = postRepository.findById(id).orElseThrow(() ->
new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id: " + id));
List<Comment> comments = post.getComments();
return comments.stream().map(CommentDto.Response::new).collect(Collectors.toList());
public List<CommentResponse> findAll(Long postId) {
Post post = postRepository.findById(postId)
.orElseThrow(() -> new ApiException(ErrorType._POST_NOT_FOUND));
List<Comment> comments = commentRepository.findByPostAndParentCommentIsNullOrderById(post);
return comments.stream()
.map(comment -> CommentResponse.fromEntity(comment))
.toList();
}

/* UPDATE */
@Transactional
public void update(Long postId, Long id, CommentDto.Request dto) {
Comment comment = commentRepository.findByPostIdAndId(postId, id).orElseThrow(() ->
new IllegalArgumentException("해당 댓글이 존재하지 않습니다. " + id));
public void update(Long postId, Long commentId, CommentUpdateRequest dto) {
Comment comment = commentRepository.findByPostIdAndId(postId, commentId).orElseThrow(() ->
new IllegalArgumentException("해당 댓글이 존재하지 않습니다. " + commentId));

comment.update(dto.getComment());
}

/* DELETE */
@Transactional
public void delete(Long postId, Long id) {
Comment comment = commentRepository.findByPostIdAndId(postId, id).orElseThrow(() ->
new IllegalArgumentException("해당 댓글이 존재하지 않습니다. id=" + id));
public void delete(Long postId, Long commentId) {
Comment comment = commentRepository.findByPostIdAndId(postId, commentId)
.orElseThrow(() -> new ApiException(ErrorType._POST_NOT_FOUND));

commentRepository.delete(comment);
}

@Transactional
public Long saveReply(Long postId, Long parentId, Long gitLoginId, CommentDto.Request dto) {
public Long saveReply(Long postId, Long parentCommentId, CommentCreateRequest dto) {
Member currentMember = SecurityUtil.getCurrentMember();

// 부모 댓글 가져오기
Comment parentComment = commentRepository.findByPostIdAndId(postId, parentId).orElseThrow(() ->
new IllegalArgumentException("부모 댓글이 존재하지 않습니다. id=" + parentId));
Post post = postRepository.findById(postId)
.orElseThrow(() -> new ApiException(ErrorType._POST_NOT_FOUND));

Post post = postRepository.findById(postId).orElseThrow(() ->
new IllegalArgumentException("댓글 쓰기 실패: 해당 게시글이 존재하지 않습니다. " + postId));
Comment parentComment = commentRepository.findById(parentCommentId).orElseThrow(() ->
new IllegalArgumentException("부모 댓글이 존재하지 않습니다. id=" + parentCommentId));

dto.setMember(currentMember);
dto.setPost(post);
dto.setParentComment(parentComment);
Comment replyComment = Comment.builder()
.comment(dto.getComment())
.member(currentMember)
.post(post)
.parentComment(parentComment)
.build();
commentRepository.save(replyComment);

Comment comment = dto.toEntity();
commentRepository.save(comment);

return comment.getId();
return replyComment.getId();
}
@Transactional(readOnly = true)
public List<CommentDto.ResponseWithReplies> findAllWithReplies(Long postId) {
Post post = postRepository.findById(postId).orElseThrow(() ->
new IllegalArgumentException("해당 게시글이 존재하지 않습니다. id: " + postId));

public List<CommentResponse> findAllWithReplies(Long postId) {
Post post = postRepository.findById(postId)
.orElseThrow(() -> new ApiException(ErrorType._POST_NOT_FOUND));

List<Comment> topLevelComments = commentRepository.findByPostAndParentCommentIsNullOrderById(post);

return topLevelComments.stream()
.map(topLevelComment -> {
List<Comment> replies = commentRepository.findByParentCommentOrderById(topLevelComment);
return new CommentDto.ResponseWithReplies(topLevelComment, replies);
})
.map(comment -> CommentResponse.fromEntity(comment))
.collect(Collectors.toList());
}
}
}

0 comments on commit 9ab5049

Please sign in to comment.