Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -22,6 +25,7 @@ public class NotificationService {

private final NotificationRepository notificationRepository;
private final UserRepository userRepository;
private final CommentRepository commentRepository;

public List<NotificationResponseDto> getCommentAlarms(Long userId) {
List<Notification> notifications = notificationRepository
Expand All @@ -37,7 +41,7 @@ public List<NotificationResponseDto> getLikeAlarms(Long userId) {
return toResponseDtos(notifications);
}

// 알림 목록의 senderId를 모아 유저 정보를 한 번에 조회한 뒤 DTO로 변환 (N+1 방지)
// 알림 목록의 senderId, commentId를 모아 각각 한 번에 조회한 뒤 DTO로 변환 (N+1 방지)
private List<NotificationResponseDto> toResponseDtos(List<Notification> notifications) {
List<Long> senderIds = notifications.stream()
.map(Notification::getSenderId)
Expand All @@ -47,10 +51,20 @@ private List<NotificationResponseDto> toResponseDtos(List<Notification> notifica
Map<Long, User> senderMap = userRepository.findAllById(senderIds).stream()
.collect(Collectors.toMap(User::getId, Function.identity()));

List<Long> commentIds = notifications.stream()
.map(Notification::getCommentId)
.filter(Objects::nonNull)
.distinct()
.toList();

Map<Long, Comment> 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();
}
Expand Down
Loading