Skip to content

[fix] AiCommentServiceTest 시그니처 변경에 맞게 수정#131

Merged
k3vin7 merged 2 commits intomainfrom
fix/ai-comment-decouple-reply-scheduling
Apr 1, 2026
Merged

[fix] AiCommentServiceTest 시그니처 변경에 맞게 수정#131
k3vin7 merged 2 commits intomainfrom
fix/ai-comment-decouple-reply-scheduling

Conversation

@k3vin7
Copy link
Copy Markdown
Contributor

@k3vin7 k3vin7 commented Apr 1, 2026

Summary

  • #130에서 scheduleReplyForAiComment() 시그니처를 ID 기반으로 변경한 것에 맞게 테스트 수정
  • aiReplyCount 검증을 DB에서 재조회하는 방식으로 변경

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +148 to +152
public void scheduleReplyForAiComment(Long aiCommentId, Long postId) {
Comment aiComment = commentRepository.findById(aiCommentId).orElse(null);
if (aiComment == null) {
return;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

postId 파라미터는 불필요해 보입니다. aiComment 엔티티가 이미 Post에 대한 참조를 가지고 있으므로 이를 활용할 수 있습니다. 또한, 이벤트 리스너를 통해 전달된 ID로 엔티티를 조회할 때 데이터가 없는 것은 정상적인 상황이 아닐 가능성이 높으므로, 단순히 return 하기보다 orElseThrow를 통해 명시적인 예외를 던져 관측성을 높이는 것이 좋습니다.

    public void scheduleReplyForAiComment(Long aiCommentId) {
        Comment aiComment = commentRepository.findById(aiCommentId)
                .orElseThrow(() -> new NotFoundException("AI 댓글을 찾을 수 없습니다. id=" + aiCommentId));

Comment on lines +168 to +171
Post post = postRepository.findById(postId).orElse(null);
if (post == null) {
return;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

앞서 언급한 대로 aiComment.getPost()를 사용하면 별도의 DB 조회 없이 게시물 엔티티에 접근할 수 있습니다. JPA 프록시를 활용하면 불필요한 SELECT 쿼리 발생도 방지할 수 있어 더 효율적입니다.

Suggested change
Post post = postRepository.findById(postId).orElse(null);
if (post == null) {
return;
}
Post post = aiComment.getPost();

Comment on lines +3 to +6
public record AiReplyScheduleEvent(
Long aiCommentId,
Long postId
) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

AiCommentService에서 aiCommentId만으로 필요한 정보를 모두 가져올 수 있게 되었으므로, 이벤트 객체에서도 불필요한 postId 필드를 제거하는 것이 좋습니다.

public record AiReplyScheduleEvent(
        Long aiCommentId
) {

@TransactionalEventListener(phase = AFTER_COMMIT)
public void handleAiReplyScheduleEvent(AiReplyScheduleEvent event) {
try {
aiCommentService.scheduleReplyForAiComment(event.aiCommentId(), event.postId());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

서비스 메서드 시그니처 변경에 맞춰 호출부를 수정해야 합니다.

Suggested change
aiCommentService.scheduleReplyForAiComment(event.aiCommentId(), event.postId());
aiCommentService.scheduleReplyForAiComment(event.aiCommentId());


if (parentComment.isAi()) {
aiCommentService.scheduleReplyForAiComment(parentComment, post);
eventPublisher.publishEvent(new AiReplyScheduleEvent(parentComment.getId(), post.getId()));
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

이벤트 생성 시 불필요한 post.getId() 전달을 제거합니다.

Suggested change
eventPublisher.publishEvent(new AiReplyScheduleEvent(parentComment.getId(), post.getId()));
eventPublisher.publishEvent(new AiReplyScheduleEvent(parentComment.getId()));


// when
aiCommentService.scheduleReplyForAiComment(aiComment, post);
aiCommentService.scheduleReplyForAiComment(aiComment.getId(), post.getId());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

서비스 메서드 시그니처 변경에 맞춰 테스트 코드를 수정합니다.

Suggested change
aiCommentService.scheduleReplyForAiComment(aiComment.getId(), post.getId());
aiCommentService.scheduleReplyForAiComment(aiComment.getId());


// when
aiCommentService.scheduleReplyForAiComment(aiComment, post);
aiCommentService.scheduleReplyForAiComment(aiComment.getId(), post.getId());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

서비스 메서드 시그니처 변경에 맞춰 테스트 코드를 수정합니다.

Suggested change
aiCommentService.scheduleReplyForAiComment(aiComment.getId(), post.getId());
aiCommentService.scheduleReplyForAiComment(aiComment.getId());

@k3vin7 k3vin7 merged commit 31c4c4d into main Apr 1, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant