diff --git a/src/main/java/com/feesh/domain/notification/dto/NotificationResponseDto.java b/src/main/java/com/feesh/domain/notification/dto/NotificationResponseDto.java index a3be251..4f267a6 100644 --- a/src/main/java/com/feesh/domain/notification/dto/NotificationResponseDto.java +++ b/src/main/java/com/feesh/domain/notification/dto/NotificationResponseDto.java @@ -1,5 +1,6 @@ package com.feesh.domain.notification.dto; +import com.feesh.domain.comment.entity.Comment; import com.feesh.domain.notification.entity.Notification; import com.feesh.domain.user.entity.User; import lombok.Builder; @@ -16,16 +17,18 @@ public class NotificationResponseDto { private String senderNickname; private Long postId; private Long commentId; + private Long parentCommentId; private boolean read; private LocalDateTime createdAt; - public static NotificationResponseDto from(Notification notification, User sender) { + public static NotificationResponseDto from(Notification notification, User sender, Comment comment) { return NotificationResponseDto.builder() .id(notification.getId()) .senderId(notification.getSenderId()) .senderNickname(sender != null ? sender.getNickname() : null) .postId(notification.getPostId()) .commentId(notification.getCommentId()) + .parentCommentId(comment != null && comment.getParent() != null ? comment.getParent().getId() : null) .read(notification.isRead()) .createdAt(notification.getCreatedAt()) .build(); diff --git a/src/main/java/com/feesh/domain/notification/service/NotificationService.java b/src/main/java/com/feesh/domain/notification/service/NotificationService.java index 225139a..5f868b1 100644 --- a/src/main/java/com/feesh/domain/notification/service/NotificationService.java +++ b/src/main/java/com/feesh/domain/notification/service/NotificationService.java @@ -1,5 +1,7 @@ package com.feesh.domain.notification.service; +import com.feesh.domain.comment.entity.Comment; +import com.feesh.domain.comment.repository.CommentRepository; import com.feesh.domain.notification.dto.NotificationResponseDto; import com.feesh.domain.notification.entity.Notification; import com.feesh.domain.notification.entity.NotificationType; @@ -12,6 +14,7 @@ import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.function.Function; import java.util.stream.Collectors; @@ -22,6 +25,7 @@ public class NotificationService { private final NotificationRepository notificationRepository; private final UserRepository userRepository; + private final CommentRepository commentRepository; public List getCommentAlarms(Long userId) { List notifications = notificationRepository @@ -37,7 +41,7 @@ public List getLikeAlarms(Long userId) { return toResponseDtos(notifications); } - // 알림 목록의 senderId를 모아 유저 정보를 한 번에 조회한 뒤 DTO로 변환 (N+1 방지) + // 알림 목록의 senderId, commentId를 모아 각각 한 번에 조회한 뒤 DTO로 변환 (N+1 방지) private List toResponseDtos(List notifications) { List senderIds = notifications.stream() .map(Notification::getSenderId) @@ -47,10 +51,20 @@ private List toResponseDtos(List notifica Map senderMap = userRepository.findAllById(senderIds).stream() .collect(Collectors.toMap(User::getId, Function.identity())); + List commentIds = notifications.stream() + .map(Notification::getCommentId) + .filter(Objects::nonNull) + .distinct() + .toList(); + + Map commentMap = commentRepository.findAllById(commentIds).stream() + .collect(Collectors.toMap(Comment::getId, Function.identity())); + return notifications.stream() .map(notification -> NotificationResponseDto.from( notification, - senderMap.get(notification.getSenderId()) + senderMap.get(notification.getSenderId()), + commentMap.get(notification.getCommentId()) )) .toList(); }