From 9823eae89a460ec44391423c3fae88ed99741370 Mon Sep 17 00:00:00 2001 From: becooq81 Date: Tue, 6 Aug 2024 21:03:07 +0900 Subject: [PATCH 1/3] feat(NotificationController): added api to view all notifications at once --- .../notification/controller/NotificationController.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/poolc/api/notification/controller/NotificationController.java b/src/main/java/org/poolc/api/notification/controller/NotificationController.java index cb3f0f9b..5c23866c 100644 --- a/src/main/java/org/poolc/api/notification/controller/NotificationController.java +++ b/src/main/java/org/poolc/api/notification/controller/NotificationController.java @@ -15,9 +15,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; -import java.util.List; -import java.util.stream.Collectors; - @Controller @RequestMapping("/notification") @RequiredArgsConstructor @@ -30,6 +27,12 @@ public ResponseEntity viewNotification(@AuthenticationPrin return ResponseEntity.status(HttpStatus.OK).body(response); } + @PostMapping("/all") + public ResponseEntity viewAllNotifications(@AuthenticationPrincipal Member member) { + notificationService.readAllNotifications(member); + return ResponseEntity.status(HttpStatus.OK).build(); + } + @GetMapping("/unread") public ResponseEntity getUnreadNotifications(@AuthenticationPrincipal Member member) { NotificationSummaryResponse summaryResponse = notificationService.getUnreadNotificationsForMember(member); From 81278a1598e5b09a74213e8fc6d15570f7278c6b Mon Sep 17 00:00:00 2001 From: becooq81 Date: Tue, 6 Aug 2024 21:03:27 +0900 Subject: [PATCH 2/3] fix(NotificationRepository): added @Param for parameters for repository methods --- .../repository/NotificationRepository.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/poolc/api/notification/repository/NotificationRepository.java b/src/main/java/org/poolc/api/notification/repository/NotificationRepository.java index aefcb27c..ea83f19a 100644 --- a/src/main/java/org/poolc/api/notification/repository/NotificationRepository.java +++ b/src/main/java/org/poolc/api/notification/repository/NotificationRepository.java @@ -6,13 +6,17 @@ import org.springframework.data.jpa.repository.Query; import java.util.List; +import org.springframework.data.repository.query.Param; +import org.springframework.security.core.parameters.P; public interface NotificationRepository extends JpaRepository { - List findByReceiverIdAndReadStatus(String receiverId, Boolean readStatus); + @Query("SELECT n FROM Notification n WHERE n.receiverId = :receiverId AND n.readStatus = :readStatus") + List findByReceiverIdAndReadStatus(@Param("receiverId") String receiverId, @Param("readStatus") Boolean readStatus); - List findAllByReceiverId(String receiverId); + @Query("SELECT n FROM Notification n WHERE n.receiverId = :receiverId ORDER BY n.createdAt DESC") + List findAllByReceiverId(@Param("receiverId") String receiverId); @Query("SELECT COUNT(n) FROM Notification n WHERE n.receiverId = :receiverId AND n.readStatus = false") - Long countByReceiverIdAndReadIsFalse(String receiverId); + Long countByReceiverIdAndReadIsFalse(@Param("receiverId") String receiverId); } From 88df62be4186700721a02f2103cd69842e6a3adc Mon Sep 17 00:00:00 2001 From: becooq81 Date: Tue, 6 Aug 2024 21:03:57 +0900 Subject: [PATCH 3/3] feat(NotificationService): add transactional method to read all notifications for loginID --- .../api/notification/service/NotificationService.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/poolc/api/notification/service/NotificationService.java b/src/main/java/org/poolc/api/notification/service/NotificationService.java index 627b9c93..31bd37fa 100644 --- a/src/main/java/org/poolc/api/notification/service/NotificationService.java +++ b/src/main/java/org/poolc/api/notification/service/NotificationService.java @@ -37,13 +37,20 @@ public NotificationSummaryResponse getUnreadNotificationsForMember(Member member public NotificationSummaryResponse getAllNotificationsForMember(Member member) { List responses = notificationRepository.findAllByReceiverId(member.getLoginID()) .stream() - //.peek(Notification::memberReads) // Apply the memberReads method + //.peek(Notification::memberReads) .sorted(Comparator.comparing(Notification::getCreatedAt).reversed()) .map(NotificationResponse::of) .collect(Collectors.toList()); long unreadCount = notificationRepository.countByReceiverIdAndReadIsFalse(member.getLoginID()); return NotificationSummaryResponse.of(unreadCount, responses); } + + @Transactional + public void readAllNotifications(Member member) { + List notifications = notificationRepository.findAllByReceiverId(member.getLoginID()); + notifications.forEach(Notification::memberReads); + } + @Transactional public void createBadgeNotification(Member receiver) { Notification notification = new Notification(receiver.getLoginID(), NotificationType.BADGE);