From e36e644da45e88340392db495a094b6ad495f465 Mon Sep 17 00:00:00 2001 From: jaypark Date: Sun, 19 Nov 2023 00:54:35 +0900 Subject: [PATCH] =?UTF-8?q?hotfix=20:=20comment=20=EC=95=8C=EB=A6=BC=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../push/CommunityCommentNoticeFactory.java | 4 +- .../domain/post/PostQueryRepository.java | 8 +-- .../domain/post/PostServiceImpl.java | 2 +- .../domain/post/comment/Comment.java | 14 ++-- .../post/comment/CommentJpaRepository.java | 10 ++- .../post/comment/CommentRepository.java | 3 +- .../post/comment/CommentRepositoryImpl.java | 5 +- .../post/comment/CommentServiceImpl.java | 70 +++++++++++++------ .../domain/post/comment/dto/Command.java | 5 +- .../domain/post/PostServiceImplTest.java | 2 +- 10 files changed, 72 insertions(+), 51 deletions(-) diff --git a/src/main/java/com/depromeet/breadmapbackend/domain/notice/factory/push/CommunityCommentNoticeFactory.java b/src/main/java/com/depromeet/breadmapbackend/domain/notice/factory/push/CommunityCommentNoticeFactory.java index 6e01810f..f1e3e764 100644 --- a/src/main/java/com/depromeet/breadmapbackend/domain/notice/factory/push/CommunityCommentNoticeFactory.java +++ b/src/main/java/com/depromeet/breadmapbackend/domain/notice/factory/push/CommunityCommentNoticeFactory.java @@ -23,7 +23,7 @@ public class CommunityCommentNoticeFactory implements NoticeFactory { private static final String COMMENT_TITLE_FORMAT = "댓글 알림"; private static final String COMMENT_CONTENT_FORMAT = "내 게시글에 %s님이 댓글을 달았어요!"; - private static final NoticeType SUPPORT_TYPE = NoticeType.REVIEW_COMMENT; + private static final NoticeType SUPPORT_TYPE = NoticeType.COMMUNITY_COMMENT; private final CustomAWSS3Properties customAwss3Properties; private final UserRepository userRepository; private final PostRepository postRepository; @@ -44,7 +44,7 @@ public String getImage(final Notice notice) { public List createNotice(final NoticeEventDto noticeEventDto) { final Post post = postRepository.findById(noticeEventDto.contentId()) - .orElseThrow(() -> new DaedongException(DaedongStatus.REVIEW_NOT_FOUND)); + .orElseThrow(() -> new DaedongException(DaedongStatus.POST_NOT_FOUND)); final User fromUser = userRepository.findById(noticeEventDto.userId()) .orElseThrow(() -> new DaedongException(DaedongStatus.USER_NOT_FOUND)); diff --git a/src/main/java/com/depromeet/breadmapbackend/domain/post/PostQueryRepository.java b/src/main/java/com/depromeet/breadmapbackend/domain/post/PostQueryRepository.java index b40a2205..7d29b6e0 100644 --- a/src/main/java/com/depromeet/breadmapbackend/domain/post/PostQueryRepository.java +++ b/src/main/java/com/depromeet/breadmapbackend/domain/post/PostQueryRepository.java @@ -81,7 +81,7 @@ public Optional findPostDetailById(final Long postId, final Lon .where(postLike.post.id.eq(post.id)), JPAExpressions.select(comment.count().coalesce(0L)) .from(comment) - .where(comment.post.eq(post) + .where(comment.postId.eq(post.id) .and(comment.postTopic.eq(topic))), JPAExpressions.select(review.count().coalesce(0L)) .from(review) @@ -101,7 +101,7 @@ public Optional findPostDetailById(final Long postId, final Lon .otherwise(false), new CaseBuilder().when(JPAExpressions.select(comment.count()) .from(comment) - .where(comment.post.eq(post) + .where(comment.postId.eq(post.id) .and(comment.user.id.eq(post.user.id))).goe(1L)).then(true) .otherwise(false) )) @@ -296,7 +296,7 @@ private List getTopThreePostScoresWIthId(final Long userId) { .on(post.id.eq(postLike.post.id) .and(postLike.createdAt.between(LocalDateTime.now().minusDays(3), LocalDateTime.now()))) .leftJoin(comment) - .on(post.id.eq(comment.post.id) + .on(post.id.eq(comment.postId) .and(comment.createdAt.between(LocalDateTime.now().minusDays(3), LocalDateTime.now())) .and(comment.postTopic.eq(post.postTopic))) .leftJoin(blockUser).on(blockUser.toUser.id.eq(post.user.id).and(blockUser.fromUser.id.eq(userId))) @@ -316,7 +316,7 @@ private List getTopThreeReviewScoresWithId(final Long userId) { .on(review.id.eq(reviewLike.review.id) .and(reviewLike.createdAt.between(LocalDateTime.now().minusDays(3), LocalDateTime.now()))) .leftJoin(comment) - .on(review.id.eq(comment.post.id) + .on(review.id.eq(comment.postId) .and(comment.createdAt.between(LocalDateTime.now().minusDays(3), LocalDateTime.now())) .and(comment.postTopic.eq(PostTopic.REVIEW))) .leftJoin(blockUser).on(blockUser.toUser.id.eq(review.user.id).and(blockUser.fromUser.id.eq(userId))) diff --git a/src/main/java/com/depromeet/breadmapbackend/domain/post/PostServiceImpl.java b/src/main/java/com/depromeet/breadmapbackend/domain/post/PostServiceImpl.java index 1078acf9..fde3b060 100644 --- a/src/main/java/com/depromeet/breadmapbackend/domain/post/PostServiceImpl.java +++ b/src/main/java/com/depromeet/breadmapbackend/domain/post/PostServiceImpl.java @@ -95,7 +95,7 @@ public void delete( final Post post = postRepository.findByPostIdAndUserIdAndPostTopic(postId, userId, topic) .orElseThrow(() -> new DaedongException(DaedongStatus.POST_NOT_FOUND)); - final List commentIdListToDelete = commentRepository.findCommentIdListByPost(post); + final List commentIdListToDelete = commentRepository.findCommentIdListByPostId(postId); commentRepository.deleteAllByIdInBatch(commentIdListToDelete); commentLikeRepository.deleteAllByCommentIdList(commentIdListToDelete); diff --git a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/Comment.java b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/Comment.java index 7a41c1f3..511b7463 100644 --- a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/Comment.java +++ b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/Comment.java @@ -12,7 +12,6 @@ import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; -import com.depromeet.breadmapbackend.domain.post.Post; import com.depromeet.breadmapbackend.domain.post.PostTopic; import com.depromeet.breadmapbackend.domain.user.User; import com.depromeet.breadmapbackend.global.BaseEntity; @@ -44,9 +43,12 @@ public class Comment extends BaseEntity { @Column(nullable = false, length = 500) private String content; - @ManyToOne(fetch = LAZY) - @JoinColumn(name = "post_id") - private Post post; + // @ManyToOne(fetch = LAZY) + // @JoinColumn(name = "post_id", insertable = false, updatable = false) + // private Post post; + + @Column(name = "post_id") + private Long postId; @Column(nullable = false) @Enumerated(EnumType.STRING) @@ -67,7 +69,7 @@ public class Comment extends BaseEntity { public Comment( final User user, - final Post post, + final Long postId, final String content, final boolean isFirstDepth, final Long parentId, @@ -76,7 +78,7 @@ public Comment( ) { this.user = user; this.content = content; - this.post = post; + this.postId = postId; this.isFirstDepth = isFirstDepth; this.parentId = parentId; this.targetCommentUserId = targetCommentUserId; diff --git a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentJpaRepository.java b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentJpaRepository.java index 2866c27d..87b77eaa 100644 --- a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentJpaRepository.java +++ b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentJpaRepository.java @@ -8,8 +8,6 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; -import com.depromeet.breadmapbackend.domain.post.Post; - /** * CommentJpaRepository * @@ -21,11 +19,11 @@ public interface CommentJpaRepository extends JpaRepository { Optional findByIdAndUserId(Long commentId, Long userId); @Modifying - @Query("delete from Comment c where c.post = :post") - void deleteByPostId(@Param("post") Post post); + @Query("delete from Comment c where c.postId = :postId") + void deleteByPostId(@Param("postId") Long postId); - @Query("select c.id from Comment c where c.post = :post") - List findCommentIdListByPost(@Param("post") Post post); + @Query("select c.id from Comment c where c.postId = :postId") + List findCommentIdListByPostId(@Param("postId") Long postId); Optional findByIdAndPostId(Long commentId, Long postId); } diff --git a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentRepository.java b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentRepository.java index d9d134c1..51f3cc65 100644 --- a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentRepository.java +++ b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentRepository.java @@ -5,7 +5,6 @@ import org.springframework.data.domain.Page; -import com.depromeet.breadmapbackend.domain.post.Post; import com.depromeet.breadmapbackend.domain.post.PostTopic; import com.depromeet.breadmapbackend.domain.post.comment.dto.CommentInfo; @@ -30,7 +29,7 @@ Page findComment( void deleteAllByIdInBatch(List commentIdList); - List findCommentIdListByPost(Post post); + List findCommentIdListByPostId(Long postId); Optional findById(Long id); diff --git a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentRepositoryImpl.java b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentRepositoryImpl.java index ff11aa2e..8c899cd9 100644 --- a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentRepositoryImpl.java +++ b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentRepositoryImpl.java @@ -6,7 +6,6 @@ import org.springframework.data.domain.Page; import org.springframework.stereotype.Repository; -import com.depromeet.breadmapbackend.domain.post.Post; import com.depromeet.breadmapbackend.domain.post.PostTopic; import com.depromeet.breadmapbackend.domain.post.comment.dto.CommentInfo; import com.depromeet.breadmapbackend.domain.post.comment.dto.CommentQuery; @@ -55,8 +54,8 @@ public void deleteAllByIdInBatch(final List commentIdList) { } @Override - public List findCommentIdListByPost(final Post post) { - return commentJpaRepository.findCommentIdListByPost(post); + public List findCommentIdListByPostId(final Long postId) { + return commentJpaRepository.findCommentIdListByPostId(postId); } @Override diff --git a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentServiceImpl.java b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentServiceImpl.java index df2cae10..66f3bb37 100644 --- a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentServiceImpl.java +++ b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/CommentServiceImpl.java @@ -10,6 +10,7 @@ import com.depromeet.breadmapbackend.domain.notice.dto.NoticeEventDto; import com.depromeet.breadmapbackend.domain.notice.factory.NoticeType; +import com.depromeet.breadmapbackend.domain.post.Post; import com.depromeet.breadmapbackend.domain.post.PostRepository; import com.depromeet.breadmapbackend.domain.post.PostTopic; import com.depromeet.breadmapbackend.domain.post.comment.dto.Command; @@ -17,6 +18,8 @@ import com.depromeet.breadmapbackend.domain.post.comment.dto.UpdateCommand; import com.depromeet.breadmapbackend.domain.post.comment.like.CommentLike; import com.depromeet.breadmapbackend.domain.post.comment.like.CommentLikeRepository; +import com.depromeet.breadmapbackend.domain.review.Review; +import com.depromeet.breadmapbackend.domain.review.ReviewRepository; import com.depromeet.breadmapbackend.domain.user.UserRepository; import com.depromeet.breadmapbackend.global.exception.DaedongException; import com.depromeet.breadmapbackend.global.exception.DaedongStatus; @@ -39,6 +42,7 @@ public class CommentServiceImpl implements CommentService { private final UserRepository userRepository; private final CommentLikeRepository commentLikeRepository; private final PostRepository postRepository; + private final ReviewRepository reviewRepository; private final ApplicationEventPublisher eventPublisher; @Transactional @@ -47,36 +51,56 @@ public Comment register(final Command command, final Long userId) { validateCommentCommand(command); final Comment comment = command.toEntity( userRepository.findById(userId) - .orElseThrow(() -> new DaedongException(DaedongStatus.USER_NOT_FOUND)), - postRepository.findById(command.postId()) - .orElseThrow(() -> new DaedongException(DaedongStatus.POST_NOT_FOUND)) + .orElseThrow(() -> new DaedongException(DaedongStatus.USER_NOT_FOUND)) ); final Comment savedComment = commentRepository.save(comment); - if (!Objects.equals(comment.getPost().getUser().getId(), userId)) { - if (command.isFirstDepth()) { - eventPublisher.publishEvent( - NoticeEventDto.builder() - .userId(userId) - .contentId(command.postId()) - .noticeType(command.postTopic() == PostTopic.REVIEW - ? NoticeType.REVIEW_COMMENT - : NoticeType.COMMUNITY_COMMENT) - .build() - ); - } else { - eventPublisher.publishEvent( - NoticeEventDto.builder() - .userId(userId) - .contentId(command.parentId()) - .noticeType(NoticeType.RECOMMENT) - .build() - ); - } + if (isUserAuthorOfPostAndReview(command, userId, savedComment)) + return savedComment; + + if (command.isFirstDepth()) { + + eventPublisher.publishEvent( + NoticeEventDto.builder() + .userId(userId) + .contentId(command.postId()) + .noticeType(command.postTopic() == PostTopic.REVIEW + ? NoticeType.REVIEW_COMMENT + : NoticeType.COMMUNITY_COMMENT) + .build() + ); + } else { + eventPublisher.publishEvent( + NoticeEventDto.builder() + .userId(userId) + .contentId(command.parentId()) + .noticeType(NoticeType.RECOMMENT) + .build() + ); } + return savedComment; } + private boolean isUserAuthorOfPostAndReview(final Command command, final Long userId, final Comment savedComment) { + if (command.postTopic() == PostTopic.REVIEW) { + + final Review review = reviewRepository.findById(command.postId()) + .orElseThrow(() -> new DaedongException(DaedongStatus.REVIEW_NOT_FOUND)); + if (review.getUser().getId().equals(userId)) { + return true; + } + } else { + final Post post = postRepository.findById(command.postId()) + .orElseThrow(() -> new DaedongException(DaedongStatus.POST_NOT_FOUND)); + + if (post.getUser().getId().equals(userId)) { + return true; + } + } + return false; + } + @Override public Page findComment(final Long postId, final PostTopic postTopic, final Long userId, final int page) { diff --git a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/dto/Command.java b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/dto/Command.java index c9425a25..8cb1de71 100644 --- a/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/dto/Command.java +++ b/src/main/java/com/depromeet/breadmapbackend/domain/post/comment/dto/Command.java @@ -1,6 +1,5 @@ package com.depromeet.breadmapbackend.domain.post.comment.dto; -import com.depromeet.breadmapbackend.domain.post.Post; import com.depromeet.breadmapbackend.domain.post.PostTopic; import com.depromeet.breadmapbackend.domain.post.comment.Comment; import com.depromeet.breadmapbackend.domain.user.User; @@ -21,10 +20,10 @@ public record Command( Long targetCommentUserId ) { - public Comment toEntity(final User user, final Post post) { + public Comment toEntity(final User user) { return new Comment( user, - post, + postId, content, isFirstDepth, isFirstDepth ? 0 : parentId, diff --git a/src/test/java/com/depromeet/breadmapbackend/domain/post/PostServiceImplTest.java b/src/test/java/com/depromeet/breadmapbackend/domain/post/PostServiceImplTest.java index eccbd593..e4514cc5 100644 --- a/src/test/java/com/depromeet/breadmapbackend/domain/post/PostServiceImplTest.java +++ b/src/test/java/com/depromeet/breadmapbackend/domain/post/PostServiceImplTest.java @@ -325,7 +325,7 @@ void setUp() throws Exception { PostLike.class) .setParameter("postId", postId) .getResultList(); - final List commentResult = em.createQuery("select c from Comment c where c.post.id =:postId", + final List commentResult = em.createQuery("select c from Comment c where c.postId =:postId", Comment.class) .setParameter("postId", postId) .getResultList();