From 6c29f76528869d2ddb6a1806e99f5f154b229623 Mon Sep 17 00:00:00 2001 From: kimdongjun Date: Thu, 21 Nov 2024 16:22:50 +0900 Subject: [PATCH] =?UTF-8?q?:sparkle:=20feat:=20NotificationQueryService=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit NotificationQueryService 생성 --- .../NotificationQueryService.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 src/main/java/foregg/foreggserver/service/notificationService/NotificationQueryService.java diff --git a/src/main/java/foregg/foreggserver/service/notificationService/NotificationQueryService.java b/src/main/java/foregg/foreggserver/service/notificationService/NotificationQueryService.java new file mode 100644 index 0000000..79959e6 --- /dev/null +++ b/src/main/java/foregg/foreggserver/service/notificationService/NotificationQueryService.java @@ -0,0 +1,57 @@ +package foregg.foreggserver.service.notificationService; + +import foregg.foreggserver.converter.NotificationConverter; +import foregg.foreggserver.domain.Notification; +import foregg.foreggserver.domain.Reply; +import foregg.foreggserver.domain.User; +import foregg.foreggserver.dto.notificationDTO.NotificationResponseDTO; +import foregg.foreggserver.jwt.SecurityUtil; +import foregg.foreggserver.repository.NotificationRepository; +import foregg.foreggserver.repository.ReplyRepository; +import foregg.foreggserver.service.userService.UserQueryService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class NotificationQueryService { + + private final ReplyRepository replyRepository; + private final UserQueryService userQueryService; + private final NotificationRepository notificationRepository; + + public List getNotificationHistory() { + + List result = new ArrayList<>(); + + LocalDateTime thresholdDate = LocalDateTime.now().minusDays(30); + User user = userQueryService.getUser(SecurityUtil.getCurrentUser()); + + // 필터링된 Reply 리스트 + List replyList = replyRepository.findByReceiver(user).stream() + .filter(reply -> reply.getCreatedAt().isAfter(thresholdDate)) + .toList(); + + // 필터링된 Notification 리스트 + List notificationList = notificationRepository.findByReceiver(user).stream() + .filter(notification -> notification.getCreatedAt().isAfter(thresholdDate)) + .toList(); + + // 결과 생성 + result.addAll(NotificationConverter.fromReplyToNotification(replyList)); + result.addAll(NotificationConverter.toNotificationResponse(notificationList)); + + // createdAt 순서로 정렬 + result.sort(Comparator.comparing(NotificationResponseDTO::getCreatedAt).reversed()); + + return result; + } + +}