Skip to content

Commit 36ea657

Browse files
committed
Feat: 커뮤니티 글/댓글 좋아요 기능에 이벤트 발행 로직 추가
1 parent d7fd421 commit 36ea657

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/main/java/com/back/domain/board/comment/service/CommentLikeService.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import com.back.domain.board.comment.entity.CommentLike;
66
import com.back.domain.board.comment.repository.CommentLikeRepository;
77
import com.back.domain.board.comment.repository.CommentRepository;
8+
import com.back.domain.notification.event.community.CommentLikedEvent;
89
import com.back.domain.user.entity.User;
910
import com.back.domain.user.repository.UserRepository;
1011
import com.back.global.exception.CustomException;
1112
import com.back.global.exception.ErrorCode;
1213
import lombok.RequiredArgsConstructor;
14+
import org.springframework.context.ApplicationEventPublisher;
1315
import org.springframework.stereotype.Service;
1416
import org.springframework.transaction.annotation.Transactional;
1517

@@ -20,13 +22,15 @@ public class CommentLikeService {
2022
private final CommentRepository commentRepository;
2123
private final CommentLikeRepository commentLikeRepository;
2224
private final UserRepository userRepository;
25+
private final ApplicationEventPublisher eventPublisher;
2326

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

4852
// CommentLike 저장 및 응답 반환
4953
commentLikeRepository.save(new CommentLike(comment, user));
54+
55+
if (!comment.getUser().getId().equals(userId)) {
56+
eventPublisher.publishEvent(
57+
new CommentLikedEvent(
58+
userId, // 좋아요 누른 사람
59+
comment.getUser().getId(), // 댓글 작성자
60+
comment.getPost().getId(), // 게시글 ID
61+
comment.getId(),
62+
comment.getContent()
63+
)
64+
);
65+
}
66+
5067
return CommentLikeResponse.from(comment);
5168
}
5269

src/main/java/com/back/domain/board/post/service/PostLikeService.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import com.back.domain.board.post.entity.PostLike;
66
import com.back.domain.board.post.repository.PostLikeRepository;
77
import com.back.domain.board.post.repository.PostRepository;
8+
import com.back.domain.notification.event.community.PostLikedEvent;
89
import com.back.domain.user.entity.User;
910
import com.back.domain.user.repository.UserRepository;
1011
import com.back.global.exception.CustomException;
1112
import com.back.global.exception.ErrorCode;
1213
import lombok.RequiredArgsConstructor;
14+
import org.springframework.context.ApplicationEventPublisher;
1315
import org.springframework.stereotype.Service;
1416
import org.springframework.transaction.annotation.Transactional;
1517

@@ -20,14 +22,16 @@ public class PostLikeService {
2022
private final PostRepository postRepository;
2123
private final PostLikeRepository postLikeRepository;
2224
private final UserRepository userRepository;
25+
private final ApplicationEventPublisher eventPublisher;
2326

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

4953
// PostLike 저장 및 응답 반환
5054
postLikeRepository.save(new PostLike(post, user));
55+
56+
// 알림 이벤트 발행 (자기 자신의 글이 아닐 때만)
57+
if (!post.getUser().getId().equals(userId)) {
58+
eventPublisher.publishEvent(
59+
new PostLikedEvent(
60+
userId, // 좋아요 누른 사람
61+
post.getUser().getId(), // 게시글 작성자
62+
post.getId(),
63+
post.getTitle()
64+
)
65+
);
66+
}
67+
5168
return PostLikeResponse.from(post);
5269
}
5370

0 commit comments

Comments
 (0)