Skip to content

Commit

Permalink
feat: add notification summary response that includes list of notific…
Browse files Browse the repository at this point in the history
…ation responses and number of unread notifications
  • Loading branch information
becooq81 committed Jul 25, 2024
1 parent 3df7a38 commit 5c07c0c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.poolc.api.member.domain.Member;
import org.poolc.api.notification.domain.Notification;
import org.poolc.api.notification.dto.NotificationResponse;
import org.poolc.api.notification.dto.NotificationSummaryResponse;
import org.poolc.api.notification.service.NotificationService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand All @@ -30,17 +31,16 @@ public ResponseEntity<NotificationResponse> viewNotification(@AuthenticationPrin
}

@GetMapping("/unread")
public ResponseEntity<List<NotificationResponse>> getUnreadNotifications(@AuthenticationPrincipal Member member) {
List<NotificationResponse> responses = notificationService.getUnreadNotificationsForMember(member.getLoginID());
return ResponseEntity.status(HttpStatus.OK).body(responses);
public ResponseEntity<NotificationSummaryResponse> getUnreadNotifications(@AuthenticationPrincipal Member member) {
NotificationSummaryResponse summaryResponse = notificationService.getUnreadNotificationsForMember(member);
return ResponseEntity.status(HttpStatus.OK).body(summaryResponse);
}


@GetMapping("/all")
public ResponseEntity<List<NotificationResponse>> getAllNotifications(@AuthenticationPrincipal Member member) {
List<Notification> notifications = notificationService.getAllNotificationsForMember(member.getLoginID());
List<NotificationResponse> responses = notifications.stream()
.map(NotificationResponse::of)
.collect(Collectors.toList());
return ResponseEntity.status(HttpStatus.OK).body(responses);
public ResponseEntity<NotificationSummaryResponse> getAllNotifications(@AuthenticationPrincipal Member member) {
NotificationSummaryResponse summaryResponse = notificationService.getAllNotificationsForMember(member);
return ResponseEntity.status(HttpStatus.OK).body(summaryResponse);
}

}
Original file line number Diff line number Diff line change
@@ -1,2 +1,18 @@
package org.poolc.api.notification.dto;public class NotificationSummaryResponse {
package org.poolc.api.notification.dto;

import lombok.Getter;

import java.util.List;

@Getter
public class NotificationSummaryResponse {
private long unreadCount;
private List<NotificationResponse> responses;

public static NotificationSummaryResponse of(long unreadCount, List<NotificationResponse> responses) {
NotificationSummaryResponse response = new NotificationSummaryResponse();
response.responses = responses;
response.unreadCount = unreadCount;
return response;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.poolc.api.member.domain.Member;
import org.poolc.api.notification.domain.Notification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

Expand All @@ -13,4 +12,5 @@ public interface NotificationRepository extends JpaRepository<Notification, Long

List<Notification> findAllByReceiverId(String receiverId);

Long countByReceiverIdAndReadIsFalse(String receiverId);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package org.poolc.api.notification.service;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.poolc.api.notification.dto.NotificationResponse;
import org.poolc.api.notification.dto.NotificationSummaryResponse;
import org.springframework.transaction.annotation.Transactional;
import lombok.RequiredArgsConstructor;
import org.poolc.api.member.domain.Member;
Expand All @@ -29,34 +23,31 @@ public class NotificationService {
private final MemberRepository memberRepository;

@Transactional
public List<NotificationResponse> getUnreadNotificationsForMember(String receiverId) {
return notificationRepository.findByReceiverIdAndReadStatus(receiverId, false)
public NotificationSummaryResponse getUnreadNotificationsForMember(Member member) {
List<NotificationResponse> responses = notificationRepository.findByReceiverIdAndReadStatus(member.getLoginID(), false)
.stream()
.sorted(Comparator.comparing(Notification::getCreatedAt).reversed())
.map(NotificationResponse::of)
.collect(Collectors.toList());
return NotificationSummaryResponse.of(member.getNotificationCount(), responses);
}

@Transactional
public List<Notification> getAllNotificationsForMember(String receiverId) {
List<Notification> notifications = notificationRepository.findAllByReceiverId(receiverId)
public NotificationSummaryResponse getAllNotificationsForMember(Member member) {
List<NotificationResponse> responses = notificationRepository.findAllByReceiverId(member.getLoginID())
.stream()
.peek(Notification::memberReads) // Apply the memberReads method
//.peek(Notification::memberReads) // Apply the memberReads method
.sorted(Comparator.comparing(Notification::getCreatedAt).reversed())
.map(NotificationResponse::of)
.collect(Collectors.toList());

Member recipient = getMemberByLoginID(receiverId);
if (recipient == null) {
throw new IllegalArgumentException("Recipient not found for the given receiverId");
}
recipient.resetNotificationCount();
return notifications;
return NotificationSummaryResponse.of(member.getNotificationCount(), responses);
}
@Transactional
public void createBadgeNotification(Member receiver) {
Notification notification = new Notification(receiver.getLoginID(), NotificationType.BADGE);
notificationRepository.save(notification);
receiver.addNotification();
receiver.addNotificationCount();
//sendRealTimeNotification(notification);
}

Expand All @@ -65,23 +56,23 @@ public void createMessageNotification(String senderId, String receiverId, Long m
Member receiver = getMemberByLoginID(receiverId);
Notification notification = new Notification(senderId, receiverId, messageId, NotificationType.MESSAGE);
notificationRepository.save(notification);
receiver.addNotification();
receiver.addNotificationCount();
}

@Transactional
public void createCommentNotification(String senderId, String receiverId, Long postId) {
Member sender = getMemberByLoginID(senderId);
Member receiver = getMemberByLoginID(receiverId);
notificationRepository.save(new Notification(senderId, receiverId, postId, NotificationType.POST));
receiver.addNotification();
receiver.addNotificationCount();
}

@Transactional
public void createRecommentNotification(String senderId, String receiverId, Long postId, Long parentCommentId) {
Member sender = getMemberByLoginID(senderId);
Member receiver = getMemberByLoginID(receiverId);
notificationRepository.save(new Notification(senderId, receiverId, parentCommentId, NotificationType.COMMENT));
receiver.addNotification();
receiver.addNotificationCount();
}

@Transactional
Expand All @@ -90,6 +81,7 @@ public NotificationResponse readNotification(Member member, Long notificationId)
.orElseThrow(() -> new NoSuchElementException("No notification found with given id."));
checkIfSelf(member, notification);
notification.memberReads();
member.deductNotificationCount();
return NotificationResponse.of(notification);
}

Expand Down

0 comments on commit 5c07c0c

Please sign in to comment.