Skip to content

Commit

Permalink
Merge pull request #7 from PoolC/dev
Browse files Browse the repository at this point in the history
merge notification all
  • Loading branch information
jimmy0006 authored Aug 9, 2024
2 parents b35edb9 + 9bebd58 commit 3b441fc
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +27,12 @@ public ResponseEntity<NotificationResponse> viewNotification(@AuthenticationPrin
return ResponseEntity.status(HttpStatus.OK).body(response);
}

@PostMapping("/all")
public ResponseEntity<Void> viewAllNotifications(@AuthenticationPrincipal Member member) {
notificationService.readAllNotifications(member);
return ResponseEntity.status(HttpStatus.OK).build();
}

@GetMapping("/unread")
public ResponseEntity<NotificationSummaryResponse> getUnreadNotifications(@AuthenticationPrincipal Member member) {
NotificationSummaryResponse summaryResponse = notificationService.getUnreadNotificationsForMember(member);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Notification, Long> {

List<Notification> findByReceiverIdAndReadStatus(String receiverId, Boolean readStatus);
@Query("SELECT n FROM Notification n WHERE n.receiverId = :receiverId AND n.readStatus = :readStatus")
List<Notification> findByReceiverIdAndReadStatus(@Param("receiverId") String receiverId, @Param("readStatus") Boolean readStatus);

List<Notification> findAllByReceiverId(String receiverId);
@Query("SELECT n FROM Notification n WHERE n.receiverId = :receiverId ORDER BY n.createdAt DESC")
List<Notification> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,20 @@ public NotificationSummaryResponse getUnreadNotificationsForMember(Member member
public NotificationSummaryResponse getAllNotificationsForMember(Member member) {
List<NotificationResponse> 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<Notification> notifications = notificationRepository.findAllByReceiverId(member.getLoginID());
notifications.forEach(Notification::memberReads);
}

@Transactional
public void createBadgeNotification(Member receiver) {
Notification notification = new Notification(receiver.getLoginID(), NotificationType.BADGE);
Expand Down

0 comments on commit 3b441fc

Please sign in to comment.