-
Notifications
You must be signed in to change notification settings - Fork 8
refactor: Community 연관관계 수정/삭제 #372
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
Changes from all commits
b6037ab
df10149
32cb72f
3ca8f4e
2938a79
9c59985
541fed7
5cf0660
b739ceb
805ae3d
41afcad
f24495e
2bb6e95
d3c9b0e
8257dfc
cfa18eb
13a4673
68f4bbd
af4ec96
6fcc903
5f6bcaf
786e0ae
5b58a39
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| import org.springframework.transaction.annotation.Transactional; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static com.example.solidconnection.common.exception.ErrorCode.CAN_NOT_UPDATE_DEPRECATED_COMMENT; | ||
|
|
@@ -35,14 +36,16 @@ public class CommentService { | |
|
|
||
| @Transactional(readOnly = true) | ||
| public List<PostFindCommentResponse> findCommentsByPostId(SiteUser siteUser, Long postId) { | ||
| SiteUser commentOwner = siteUserRepository.findById(siteUser.getId()) | ||
| .orElseThrow(() -> new CustomException(USER_NOT_FOUND)); | ||
| return commentRepository.findCommentTreeByPostId(postId) | ||
| .stream() | ||
| .map(comment -> PostFindCommentResponse.from(isOwner(comment, siteUser), comment)) | ||
| .map(comment -> PostFindCommentResponse.from(isOwner(comment, siteUser), comment, siteUser)) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private Boolean isOwner(Comment comment, SiteUser siteUser) { | ||
| return comment.getSiteUser().getId().equals(siteUser.getId()); | ||
| return Objects.equals(comment.getSiteUserId(), siteUser.getId()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. getSiteUserId가 long으로 바뀌어서 Objects.equals를 쓰신거군요!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 넵 맞습니다! |
||
| } | ||
|
|
||
| @Transactional | ||
|
|
@@ -54,14 +57,7 @@ public CommentCreateResponse createComment(SiteUser siteUser, CommentCreateReque | |
| parentComment = commentRepository.getById(commentCreateRequest.parentId()); | ||
| validateCommentDepth(parentComment); | ||
| } | ||
|
|
||
| /* | ||
| * todo: siteUser를 영속 상태로 만들 수 있도록 컨트롤러에서 siteUserId 를 넘겨줄 것인지, | ||
| * siteUser 에 postList 를 FetchType.EAGER 로 설정할 것인지, | ||
| * post 와 siteUser 사이의 양방향을 끊을 것인지 생각해봐야한다. | ||
| */ | ||
| SiteUser siteUser1 = siteUserRepository.findById(siteUser.getId()).orElseThrow(() -> new CustomException(USER_NOT_FOUND)); | ||
| Comment comment = commentCreateRequest.toEntity(siteUser1, post, parentComment); | ||
| Comment comment = commentCreateRequest.toEntity(siteUser, post, parentComment); | ||
| Comment createdComment = commentRepository.save(comment); | ||
|
|
||
| return CommentCreateResponse.from(createdComment); | ||
|
|
@@ -100,18 +96,18 @@ public CommentDeleteResponse deleteCommentById(SiteUser siteUser, Long commentId | |
| // 대댓글인 경우 | ||
| Comment parentComment = comment.getParentComment(); | ||
| // 대댓글을 삭제합니다. | ||
| comment.resetPostAndSiteUserAndParentComment(); | ||
| comment.resetPostAndParentComment(); | ||
| commentRepository.deleteById(commentId); | ||
| // 대댓글 삭제 이후, 부모댓글이 무의미하다면 이역시 삭제합니다. | ||
| if (parentComment.getCommentList().isEmpty() && parentComment.getContent() == null) { | ||
| parentComment.resetPostAndSiteUserAndParentComment(); | ||
| parentComment.resetPostAndParentComment(); | ||
| commentRepository.deleteById(parentComment.getId()); | ||
| } | ||
| } else { | ||
| // 댓글인 경우 | ||
| if (comment.getCommentList().isEmpty()) { | ||
| // 대댓글이 없는 경우 | ||
| comment.resetPostAndSiteUserAndParentComment(); | ||
| comment.resetPostAndParentComment(); | ||
| commentRepository.deleteById(commentId); | ||
| } else { | ||
| // 대댓글이 있는 경우 | ||
|
|
@@ -122,7 +118,7 @@ public CommentDeleteResponse deleteCommentById(SiteUser siteUser, Long commentId | |
| } | ||
|
|
||
| private void validateOwnership(Comment comment, SiteUser siteUser) { | ||
| if (!comment.getSiteUser().getId().equals(siteUser.getId())) { | ||
| if (!Objects.equals(comment.getSiteUserId(), siteUser.getId())) { | ||
| throw new CustomException(INVALID_POST_ACCESS); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,13 +8,16 @@ | |
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import static com.example.solidconnection.common.exception.ErrorCode.INVALID_POST_ID; | ||
|
|
||
| public interface PostRepository extends JpaRepository<Post, Long> { | ||
|
|
||
| @EntityGraph(attributePaths = {"postImageList", "board", "siteUser"}) | ||
| List<Post> findByBoardCode(String boardCode); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 개행!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 수정하였습니다! |
||
|
|
||
| @EntityGraph(attributePaths = {"postImageList"}) | ||
| Optional<Post> findPostById(Long id); | ||
|
|
||
| @Modifying(clearAutomatically = true, flushAutomatically = true) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import com.example.solidconnection.siteuser.domain.SiteUser;
import 제거!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
수정하였습니다!