Skip to content

Commit

Permalink
hotfix : comment 알림 오류 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
JayPark7821 committed Nov 18, 2023
1 parent 38f3dde commit e36e644
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,7 +44,7 @@ public String getImage(final Notice notice) {
public List<Notice> 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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Optional<PostDetailQuery> 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)
Expand All @@ -101,7 +101,7 @@ public Optional<PostDetailQuery> 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)
))
Expand Down Expand Up @@ -296,7 +296,7 @@ private List<Tuple> 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)))
Expand All @@ -316,7 +316,7 @@ private List<Tuple> 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)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public void delete(
final Post post = postRepository.findByPostIdAndUserIdAndPostTopic(postId, userId, topic)
.orElseThrow(() -> new DaedongException(DaedongStatus.POST_NOT_FOUND));

final List<Long> commentIdListToDelete = commentRepository.findCommentIdListByPost(post);
final List<Long> commentIdListToDelete = commentRepository.findCommentIdListByPostId(postId);

commentRepository.deleteAllByIdInBatch(commentIdListToDelete);
commentLikeRepository.deleteAllByCommentIdList(commentIdListToDelete);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -21,11 +19,11 @@ public interface CommentJpaRepository extends JpaRepository<Comment, Long> {
Optional<Comment> 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<Long> findCommentIdListByPost(@Param("post") Post post);
@Query("select c.id from Comment c where c.postId = :postId")
List<Long> findCommentIdListByPostId(@Param("postId") Long postId);

Optional<Comment> findByIdAndPostId(Long commentId, Long postId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,7 +29,7 @@ Page<CommentInfo> findComment(

void deleteAllByIdInBatch(List<Long> commentIdList);

List<Long> findCommentIdListByPost(Post post);
List<Long> findCommentIdListByPostId(Long postId);

Optional<Comment> findById(Long id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -55,8 +54,8 @@ public void deleteAllByIdInBatch(final List<Long> commentIdList) {
}

@Override
public List<Long> findCommentIdListByPost(final Post post) {
return commentJpaRepository.findCommentIdListByPost(post);
public List<Long> findCommentIdListByPostId(final Long postId) {
return commentJpaRepository.findCommentIdListByPostId(postId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@

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;
import com.depromeet.breadmapbackend.domain.post.comment.dto.CommentInfo;
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;
Expand All @@ -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
Expand All @@ -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<CommentInfo.Response> findComment(final Long postId, final PostTopic postTopic, final Long userId,
final int page) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ void setUp() throws Exception {
PostLike.class)
.setParameter("postId", postId)
.getResultList();
final List<Comment> commentResult = em.createQuery("select c from Comment c where c.post.id =:postId",
final List<Comment> commentResult = em.createQuery("select c from Comment c where c.postId =:postId",
Comment.class)
.setParameter("postId", postId)
.getResultList();
Expand Down

0 comments on commit e36e644

Please sign in to comment.