Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge notification all #7

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading