diff --git a/src/main/java/com/feesh/domain/notification/controller/NotificationController.java b/src/main/java/com/feesh/domain/notification/controller/NotificationController.java index 4116410..2fb46de 100644 --- a/src/main/java/com/feesh/domain/notification/controller/NotificationController.java +++ b/src/main/java/com/feesh/domain/notification/controller/NotificationController.java @@ -10,6 +10,7 @@ import org.springframework.web.bind.annotation.RestController; import java.util.List; +import java.util.Map; @RestController @RequestMapping("/alarm") @@ -29,4 +30,11 @@ public ResponseEntity> getLikeAlarms() { Long userId = SecurityUtil.getCurrentUserId(); return ResponseEntity.ok(notificationService.getLikeAlarms(userId)); } + + @GetMapping("/unread-count") + public ResponseEntity> getUnreadCount() { + Long userId = SecurityUtil.getCurrentUserId(); + long count = notificationService.getUnreadCount(userId); + return ResponseEntity.ok(Map.of("unreadCount", count)); + } } \ No newline at end of file diff --git a/src/main/java/com/feesh/domain/notification/repository/NotificationRepository.java b/src/main/java/com/feesh/domain/notification/repository/NotificationRepository.java index ede677a..a9914c0 100644 --- a/src/main/java/com/feesh/domain/notification/repository/NotificationRepository.java +++ b/src/main/java/com/feesh/domain/notification/repository/NotificationRepository.java @@ -13,6 +13,8 @@ List findByReceiverIdAndTypeOrderByCreatedAtDesc( NotificationType type ); + long countByReceiverIdAndIsReadFalse(Long receiverId); + void deleteAllByReceiverId(Long receiverId); void deleteAllBySenderId(Long senderId); 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 5f868b1..bef6597 100644 --- a/src/main/java/com/feesh/domain/notification/service/NotificationService.java +++ b/src/main/java/com/feesh/domain/notification/service/NotificationService.java @@ -27,20 +27,38 @@ public class NotificationService { private final UserRepository userRepository; private final CommentRepository commentRepository; + @Transactional public List getCommentAlarms(Long userId) { List notifications = notificationRepository .findByReceiverIdAndTypeOrderByCreatedAtDesc(userId, NotificationType.COMMENT); + markAsRead(notifications); + return toResponseDtos(notifications); } + @Transactional public List getLikeAlarms(Long userId) { List notifications = notificationRepository .findByReceiverIdAndTypeOrderByCreatedAtDesc(userId, NotificationType.LIKE); + markAsRead(notifications); + return toResponseDtos(notifications); } + // 헤더에 표시할 안 읽은 알림 개수 + public long getUnreadCount(Long userId) { + return notificationRepository.countByReceiverIdAndIsReadFalse(userId); + } + + // 조회한 알림들 읽음 처리 (더티체킹으로 자동 반영) + private void markAsRead(List notifications) { + notifications.stream() + .filter(notification -> !notification.isRead()) + .forEach(Notification::markAsRead); + } + // 알림 목록의 senderId, commentId를 모아 각각 한 번에 조회한 뒤 DTO로 변환 (N+1 방지) private List toResponseDtos(List notifications) { List senderIds = notifications.stream()