Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.back.domain.board.comment.entity.CommentLike;
import com.back.domain.board.comment.repository.CommentLikeRepository;
import com.back.domain.board.comment.repository.CommentRepository;
import com.back.domain.notification.event.community.CommentLikedEvent;
import com.back.domain.user.entity.User;
import com.back.domain.user.repository.UserRepository;
import com.back.global.exception.CustomException;
import com.back.global.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -20,13 +22,15 @@ public class CommentLikeService {
private final CommentRepository commentRepository;
private final CommentLikeRepository commentLikeRepository;
private final UserRepository userRepository;
private final ApplicationEventPublisher eventPublisher;

/**
* 댓글 좋아요 서비스
* 1. User 조회
* 2. Comment 조회
* 3. 이미 존재하는 경우 예외 처리
* 4. CommentLike 저장 및 likeCount 증가
* 5. 알림 이벤트 발행 (자기 글이 아닐 경우)
*/
public CommentLikeResponse likeComment(Long commentId, Long userId) {
// User 조회
Expand All @@ -47,6 +51,19 @@ public CommentLikeResponse likeComment(Long commentId, Long userId) {

// CommentLike 저장 및 응답 반환
commentLikeRepository.save(new CommentLike(comment, user));

if (!comment.getUser().getId().equals(userId)) {
eventPublisher.publishEvent(
new CommentLikedEvent(
userId, // 좋아요 누른 사람
comment.getUser().getId(), // 댓글 작성자
comment.getPost().getId(), // 게시글 ID
comment.getId(),
comment.getContent()
)
);
}

return CommentLikeResponse.from(comment);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import com.back.domain.board.post.entity.PostLike;
import com.back.domain.board.post.repository.PostLikeRepository;
import com.back.domain.board.post.repository.PostRepository;
import com.back.domain.notification.event.community.PostLikedEvent;
import com.back.domain.user.entity.User;
import com.back.domain.user.repository.UserRepository;
import com.back.global.exception.CustomException;
import com.back.global.exception.ErrorCode;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -20,14 +22,16 @@ public class PostLikeService {
private final PostRepository postRepository;
private final PostLikeRepository postLikeRepository;
private final UserRepository userRepository;
private final ApplicationEventPublisher eventPublisher;

/**
* 게시글 좋아요 서비스
* 1. User 조회
* 2. Post 조회
* 3. 이미 존재하는 경우 예외 처리
* 4. PostLike 저장 및 likeCount 증가
* 5. PostLikeResponse 반환
* 5. 알림 이벤트 발행 (자기 글이 아닐 경우)
* 6. PostLikeResponse 반환
*/
public PostLikeResponse likePost(Long postId, Long userId) {
// User 조회
Expand All @@ -48,6 +52,19 @@ public PostLikeResponse likePost(Long postId, Long userId) {

// PostLike 저장 및 응답 반환
postLikeRepository.save(new PostLike(post, user));

// 알림 이벤트 발행 (자기 자신의 글이 아닐 때만)
if (!post.getUser().getId().equals(userId)) {
eventPublisher.publishEvent(
new PostLikedEvent(
userId, // 좋아요 누른 사람
post.getUser().getId(), // 게시글 작성자
post.getId(),
post.getTitle()
)
);
}

return PostLikeResponse.from(post);
}

Expand Down