From e68081dd49eb65ca54c783a72c3fac8d603cff83 Mon Sep 17 00:00:00 2001 From: jinny Date: Thu, 25 Jul 2024 09:46:10 +0900 Subject: [PATCH] feat: add api to view notification individually - retrieving all unread notifications will not affect read status of notifications - retrieving individual notifications require verification process for whether the retriever is the receiver of the notification --- .../controller/NotificationController.java | 16 +++++++------- .../service/NotificationService.java | 21 ++++++++++++------- 2 files changed, 22 insertions(+), 15 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 1dc6c3b4..f267e3d4 100644 --- a/src/main/java/org/poolc/api/notification/controller/NotificationController.java +++ b/src/main/java/org/poolc/api/notification/controller/NotificationController.java @@ -10,6 +10,8 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import java.util.List; @@ -21,15 +23,15 @@ public class NotificationController { private final NotificationService notificationService; + @PostMapping("/{notificationId}") + public ResponseEntity viewNotification(@AuthenticationPrincipal Member member, @PathVariable("notificationId") Long notificationId) { + NotificationResponse response = notificationService.readNotification(member, notificationId); + return ResponseEntity.status(HttpStatus.OK).body(response); + } + @GetMapping("/unread") public ResponseEntity> getUnreadNotifications(@AuthenticationPrincipal Member member) { - List notifications = notificationService.getUnreadNotificationsForMember(member.getLoginID()); - if (notifications.isEmpty()) { - return ResponseEntity.status(HttpStatus.NO_CONTENT).build(); - } - List responses = notifications.stream() - .map(NotificationResponse::of) - .collect(Collectors.toList()); + List responses = notificationService.getUnreadNotificationsForMember(member.getLoginID()); return ResponseEntity.status(HttpStatus.OK).body(responses); } 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 8c2b6cb4..1146e27d 100644 --- a/src/main/java/org/poolc/api/notification/service/NotificationService.java +++ b/src/main/java/org/poolc/api/notification/service/NotificationService.java @@ -6,6 +6,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; + +import org.poolc.api.notification.dto.NotificationResponse; import org.springframework.transaction.annotation.Transactional; import lombok.RequiredArgsConstructor; import org.poolc.api.member.domain.Member; @@ -27,15 +29,12 @@ public class NotificationService { private final MemberRepository memberRepository; @Transactional - public List getUnreadNotificationsForMember(String receiverId) { - List notifications = notificationRepository.findByReceiverIdAndReadStatus(receiverId, false) + public List getUnreadNotificationsForMember(String receiverId) { + return notificationRepository.findByReceiverIdAndReadStatus(receiverId, false) .stream() .sorted(Comparator.comparing(Notification::getCreatedAt).reversed()) + .map(NotificationResponse::of) .collect(Collectors.toList()); - notifications.forEach(Notification::memberReads); - Member recipient = getMemberByLoginID(receiverId); - recipient.resetNotificationCount(); - return notifications; } @Transactional @@ -85,15 +84,21 @@ public void createRecommentNotification(String senderId, String receiverId, Long receiver.addNotification(); } - @Transactional(readOnly = true) - public void readNotification(Long notificationId) { + @Transactional + public NotificationResponse readNotification(Member member, Long notificationId) { Notification notification = notificationRepository.findById(notificationId) .orElseThrow(() -> new NoSuchElementException("No notification found with given id.")); + checkIfSelf(member, notification); notification.memberReads(); + return NotificationResponse.of(notification); } private Member getMemberByLoginID(String loginID) { return memberRepository.findByLoginID(loginID) .orElseThrow(() -> new NoSuchElementException("No user found with given loginID")); } + + private void checkIfSelf(Member member, Notification notification) { + if (!notification.getReceiverId().equals(member.getLoginID())) throw new NoSuchElementException("No notification found."); + } }