Repository navigation
[fix] AiCommentServiceTest 시그니처 변경에 맞게 수정 #131
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
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 |
|---|---|---|
|
|
@@ -145,7 +145,12 @@ public void scheduleForPost(Post post) { | |
| } | ||
|
|
||
| @Transactional | ||
| public void scheduleReplyForAiComment(Comment aiComment, Post post) { | ||
| public void scheduleReplyForAiComment(Long aiCommentId, Long postId) { | ||
| Comment aiComment = commentRepository.findById(aiCommentId).orElse(null); | ||
| if (aiComment == null) { | ||
| return; | ||
| } | ||
|
|
||
| if (aiComment.getAiReplyCount() >= MAX_AI_REPLY_COUNT) { | ||
| return; | ||
| } | ||
|
|
@@ -160,6 +165,11 @@ public void scheduleReplyForAiComment(Comment aiComment, Post post) { | |
| return; | ||
| } | ||
|
|
||
| Post post = postRepository.findById(postId).orElse(null); | ||
| if (post == null) { | ||
| return; | ||
| } | ||
|
Comment on lines
+168
to
+171
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. |
||
|
|
||
| aiComment.incrementAiReplyCount(); | ||
|
|
||
| Instant scheduledAt = Instant.now().plusSeconds(replyDelayMinutes * 60L); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.daramg.server.aicomment.event; | ||
|
|
||
| public record AiReplyScheduleEvent( | ||
| Long aiCommentId, | ||
| Long postId | ||
| ) { | ||
|
Comment on lines
+3
to
+6
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. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.daramg.server.aicomment.event; | ||
|
|
||
| import com.daramg.server.aicomment.application.AiCommentService; | ||
| import lombok.RequiredArgsConstructor; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.event.TransactionalEventListener; | ||
|
|
||
| import static org.springframework.transaction.event.TransactionPhase.AFTER_COMMIT; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| @RequiredArgsConstructor | ||
| public class AiReplyScheduleEventListener { | ||
|
|
||
| private final AiCommentService aiCommentService; | ||
|
|
||
| @TransactionalEventListener(phase = AFTER_COMMIT) | ||
| public void handleAiReplyScheduleEvent(AiReplyScheduleEvent event) { | ||
| try { | ||
| aiCommentService.scheduleReplyForAiComment(event.aiCommentId(), event.postId()); | ||
|
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. |
||
| } catch (Exception e) { | ||
| log.warn("AI 답글 잡 등록 실패 - aiCommentId={}, postId={}", event.aiCommentId(), event.postId(), e); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| package com.daramg.server.comment.application; | ||
|
|
||
| import com.daramg.server.aicomment.application.AiCommentService; | ||
| import com.daramg.server.aicomment.event.AiReplyScheduleEvent; | ||
| import com.daramg.server.comment.domain.Comment; | ||
| import com.daramg.server.comment.domain.CommentLike; | ||
| import com.daramg.server.comment.dto.CommentLikeResponseDto; | ||
|
|
@@ -30,7 +30,6 @@ public class CommentService { | |
| private final CommentRepository commentRepository; | ||
| private final CommentLikeRepository commentLikeRepository; | ||
| private final ApplicationEventPublisher eventPublisher; | ||
| private final AiCommentService aiCommentService; | ||
|
|
||
| public void createComment(Long postId, CommentCreateDto request, User user){ | ||
| Post post = entityUtils.getEntity(postId, Post.class); | ||
|
|
@@ -80,7 +79,7 @@ public void createReply(Long commentId, CommentReplyCreateDto request, User user | |
| } | ||
|
|
||
| if (parentComment.isAi()) { | ||
| aiCommentService.scheduleReplyForAiComment(parentComment, post); | ||
| eventPublisher.publishEvent(new AiReplyScheduleEvent(parentComment.getId(), post.getId())); | ||
|
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. |
||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -220,14 +220,14 @@ class ScheduleReplyTest { | |
| commentRepository.save(aiComment); | ||
|
|
||
| // when | ||
| aiCommentService.scheduleReplyForAiComment(aiComment, post); | ||
| aiCommentService.scheduleReplyForAiComment(aiComment.getId(), post.getId()); | ||
|
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. |
||
|
|
||
| // then | ||
| List<AiCommentJob> jobs = aiCommentJobRepository.findAll(); | ||
| assertThat(jobs).hasSize(1); | ||
| assertThat(jobs.get(0).getTriggerType()).isEqualTo(AiCommentJobTriggerType.USER_REPLY); | ||
| assertThat(jobs.get(0).getParentComment().getId()).isEqualTo(aiComment.getId()); | ||
| assertThat(aiComment.getAiReplyCount()).isEqualTo((byte) 1); | ||
| assertThat(commentRepository.findById(aiComment.getId()).get().getAiReplyCount()).isEqualTo((byte) 1); | ||
| } | ||
|
|
||
| @Test | ||
|
|
@@ -240,9 +240,10 @@ class ScheduleReplyTest { | |
| Comment aiComment = Comment.ofAi(post, botUser, "AI 댓글", null, composer); | ||
| commentRepository.save(aiComment); | ||
| ReflectionTestUtils.setField(aiComment, "aiReplyCount", (byte) 2); | ||
| commentRepository.save(aiComment); | ||
|
|
||
| // when | ||
| aiCommentService.scheduleReplyForAiComment(aiComment, post); | ||
| aiCommentService.scheduleReplyForAiComment(aiComment.getId(), post.getId()); | ||
|
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. |
||
|
|
||
| // then | ||
| assertThat(aiCommentJobRepository.findAll()).isEmpty(); | ||
|
|
||
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.
postId파라미터는 불필요해 보입니다.aiComment엔티티가 이미Post에 대한 참조를 가지고 있으므로 이를 활용할 수 있습니다. 또한, 이벤트 리스너를 통해 전달된 ID로 엔티티를 조회할 때 데이터가 없는 것은 정상적인 상황이 아닐 가능성이 높으므로, 단순히return하기보다orElseThrow를 통해 명시적인 예외를 던져 관측성을 높이는 것이 좋습니다.