[fix] AiCommentServiceTest 시그니처 변경에 맞게 수정#131
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the AI reply scheduling logic to use an event-driven architecture, introducing a new event and listener to decouple the comment and AI comment services. The review feedback suggests simplifying the implementation by removing the redundant postId parameter from the service method and event record, as the post can be accessed directly through the comment entity. Additionally, it is recommended to use more explicit error handling when fetching entities and to utilize JPA relationships to improve efficiency.
| public void scheduleReplyForAiComment(Long aiCommentId, Long postId) { | ||
| Comment aiComment = commentRepository.findById(aiCommentId).orElse(null); | ||
| if (aiComment == null) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
postId 파라미터는 불필요해 보입니다. aiComment 엔티티가 이미 Post에 대한 참조를 가지고 있으므로 이를 활용할 수 있습니다. 또한, 이벤트 리스너를 통해 전달된 ID로 엔티티를 조회할 때 데이터가 없는 것은 정상적인 상황이 아닐 가능성이 높으므로, 단순히 return 하기보다 orElseThrow를 통해 명시적인 예외를 던져 관측성을 높이는 것이 좋습니다.
public void scheduleReplyForAiComment(Long aiCommentId) {
Comment aiComment = commentRepository.findById(aiCommentId)
.orElseThrow(() -> new NotFoundException("AI 댓글을 찾을 수 없습니다. id=" + aiCommentId));| Post post = postRepository.findById(postId).orElse(null); | ||
| if (post == null) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
| public record AiReplyScheduleEvent( | ||
| Long aiCommentId, | ||
| Long postId | ||
| ) { |
| @TransactionalEventListener(phase = AFTER_COMMIT) | ||
| public void handleAiReplyScheduleEvent(AiReplyScheduleEvent event) { | ||
| try { | ||
| aiCommentService.scheduleReplyForAiComment(event.aiCommentId(), event.postId()); |
|
|
||
| if (parentComment.isAi()) { | ||
| aiCommentService.scheduleReplyForAiComment(parentComment, post); | ||
| eventPublisher.publishEvent(new AiReplyScheduleEvent(parentComment.getId(), post.getId())); |
|
|
||
| // when | ||
| aiCommentService.scheduleReplyForAiComment(aiComment, post); | ||
| aiCommentService.scheduleReplyForAiComment(aiComment.getId(), post.getId()); |
|
|
||
| // when | ||
| aiCommentService.scheduleReplyForAiComment(aiComment, post); | ||
| aiCommentService.scheduleReplyForAiComment(aiComment.getId(), post.getId()); |
Summary