Skip to content

Commit 8fa90ce

Browse files
committed
[Feat] 댓글 수정 삭제 SSE 이벤트 반영
1 parent c8131db commit 8fa90ce

5 files changed

Lines changed: 50 additions & 1 deletion

File tree

‎backend/src/main/java/com/example/Piroin/project/domain/question/dto/QuestionResDTO.java‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,17 @@ public record CommentCreatedEvent(
197197
) {
198198
}
199199

200+
// 댓글 수정/삭제 시 SSE로 내려가는 목록 갱신 이벤트 응답
201+
public record CommentUpdatedEvent(
202+
String type,
203+
Long sessionId,
204+
Long questionId,
205+
Boolean isResolved,
206+
Integer commentCount,
207+
List<PreviewCommentResponse> previewComments
208+
) {
209+
}
210+
200211
// O/X 클릭 직후 응답. selectedChoice가 null이면 같은 선택지를 다시 눌러 취소된 상태다.
201212
public record UnderstandingResponseResult(
202213
Long checkId,

‎backend/src/main/java/com/example/Piroin/project/domain/question/service/QuestionEventService.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public void publishCommentCreated(Long sessionId, QuestionResDTO.CommentCreatedE
4545
broadcast(sessionId, "comment-created", event);
4646
}
4747

48+
// 댓글 수정/삭제 이벤트를 같은 세션 질문방을 구독 중인 모든 클라이언트에게 전파한다.
49+
public void publishCommentUpdated(Long sessionId, QuestionResDTO.CommentUpdatedEvent event) {
50+
broadcast(sessionId, "comment-updated", event);
51+
}
52+
4853
// 질문 등록 이벤트를 같은 세션 질문방을 구독 중인 모든 클라이언트에게 전파한다.
4954
public void publishQuestionCreated(Long sessionId, QuestionResDTO.QuestionCreatedEvent event) {
5055
broadcast(sessionId, "question-created", event);

‎backend/src/main/java/com/example/Piroin/project/domain/question/service/QuestionService.java‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ public QuestionResDTO.CommentUpdateDeleteRes updateComment(
354354

355355
comment.updateContent(request.getContent());
356356

357+
publishCommentUpdatedEventAfterCommit(comment.getQuestion());
358+
357359
return new QuestionResDTO.CommentUpdateDeleteRes(
358360
comment.getId(), comment.getContent(),
359361
comment.getUpdatedAt(), comment.getDeletedAt()
@@ -369,6 +371,8 @@ public QuestionResDTO.CommentUpdateDeleteRes deleteComment(Long commentId, Long
369371

370372
comment.softDelete();
371373

374+
publishCommentUpdatedEventAfterCommit(comment.getQuestion());
375+
372376
return new QuestionResDTO.CommentUpdateDeleteRes(
373377
comment.getId(), comment.getContent(),
374378
comment.getUpdatedAt(), comment.getDeletedAt()
@@ -751,6 +755,32 @@ private void publishCommentCreatedEventAfterCommit(Question question) {
751755
publishAfterCommit(() -> questionEventService.publishCommentCreated(sessionId, event));
752756
}
753757

758+
private void publishCommentUpdatedEventAfterCommit(Question question) {
759+
Long sessionId = question.getSession().getId();
760+
Long questionId = question.getId();
761+
List<Long> questionIds = List.of(questionId);
762+
763+
Map<Long, Integer> commentCounts = new HashMap<>();
764+
questionCommentRepository.countByQuestionIds(questionIds)
765+
.forEach(row -> commentCounts.put(row.getQuestionId(), Math.toIntExact(row.getCommentCount())));
766+
767+
Map<Long, List<QuestionResDTO.PreviewCommentResponse>> previewComments = new HashMap<>();
768+
questionCommentRepository.findPreviewCommentsByQuestionIds(questionIds)
769+
.forEach(row -> previewComments.computeIfAbsent(row.getQuestionId(), key -> new ArrayList<>())
770+
.add(toPreviewCommentResponse(question, row)));
771+
772+
QuestionResDTO.CommentUpdatedEvent event = new QuestionResDTO.CommentUpdatedEvent(
773+
"COMMENT_UPDATED",
774+
sessionId,
775+
questionId,
776+
question.getIsResolved(),
777+
commentCounts.getOrDefault(questionId, 0),
778+
previewComments.getOrDefault(questionId, List.of())
779+
);
780+
781+
publishAfterCommit(() -> questionEventService.publishCommentUpdated(sessionId, event));
782+
}
783+
754784
private void publishQuestionCreatedEventAfterCommit(Question question) {
755785
Long sessionId = question.getSession().getId();
756786

‎frontend/src/pages/qna/QnADetailPage.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ function QnADetailPage() {
135135
return;
136136
}
137137

138-
if (message.event === 'comment-created') {
138+
if (message.event === 'comment-created' || message.event === 'comment-updated') {
139139
void fetchQuestion();
140140
return;
141141
}

‎frontend/src/pages/qna/QnAListPage.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,9 @@ function QnAListPage() {
386386
case 'comment-created':
387387
handleCommentCreatedEvent(data);
388388
break;
389+
case 'comment-updated':
390+
handleCommentCreatedEvent(data);
391+
break;
389392
case 'question-created':
390393
void handleQuestionCreatedEvent(data);
391394
break;

0 commit comments

Comments
 (0)