Skip to content

Commit

Permalink
feat: add api to view notification individually
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
becooq81 committed Jul 25, 2024
1 parent 68dd271 commit e68081d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,15 +23,15 @@
public class NotificationController {
private final NotificationService notificationService;

@PostMapping("/{notificationId}")
public ResponseEntity<NotificationResponse> 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<List<NotificationResponse>> getUnreadNotifications(@AuthenticationPrincipal Member member) {
List<Notification> notifications = notificationService.getUnreadNotificationsForMember(member.getLoginID());
if (notifications.isEmpty()) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}
List<NotificationResponse> responses = notifications.stream()
.map(NotificationResponse::of)
.collect(Collectors.toList());
List<NotificationResponse> responses = notificationService.getUnreadNotificationsForMember(member.getLoginID());
return ResponseEntity.status(HttpStatus.OK).body(responses);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,15 +29,12 @@ public class NotificationService {
private final MemberRepository memberRepository;

@Transactional
public List<Notification> getUnreadNotificationsForMember(String receiverId) {
List<Notification> notifications = notificationRepository.findByReceiverIdAndReadStatus(receiverId, false)
public List<NotificationResponse> 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
Expand Down Expand Up @@ -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.");
}
}

0 comments on commit e68081d

Please sign in to comment.