mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-22 22:21:09 +08:00
feat: show unread message count
This commit is contained in:
@@ -29,6 +29,14 @@ public class NotificationController {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@GetMapping("/unread-count")
|
||||
public UnreadCount unreadCount(Authentication auth) {
|
||||
long count = notificationService.countUnread(auth.getName());
|
||||
UnreadCount uc = new UnreadCount();
|
||||
uc.setCount(count);
|
||||
return uc;
|
||||
}
|
||||
|
||||
@PostMapping("/read")
|
||||
public void markRead(@RequestBody MarkReadRequest req, Authentication auth) {
|
||||
notificationService.markRead(auth.getName(), req.getIds());
|
||||
@@ -115,4 +123,9 @@ public class NotificationController {
|
||||
private String username;
|
||||
private String avatar;
|
||||
}
|
||||
|
||||
@Data
|
||||
private static class UnreadCount {
|
||||
private long count;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,4 +10,5 @@ import java.util.List;
|
||||
public interface NotificationRepository extends JpaRepository<Notification, Long> {
|
||||
List<Notification> findByUserOrderByCreatedAtDesc(User user);
|
||||
List<Notification> findByUserAndReadOrderByCreatedAtDesc(User user, boolean read);
|
||||
long countByUserAndRead(User user, boolean read);
|
||||
}
|
||||
|
||||
@@ -45,4 +45,10 @@ public class NotificationService {
|
||||
}
|
||||
notificationRepository.saveAll(notifs);
|
||||
}
|
||||
|
||||
public long countUnread(String username) {
|
||||
User user = userRepository.findByUsername(username)
|
||||
.orElseThrow(() -> new IllegalArgumentException("User not found"));
|
||||
return notificationRepository.countByUserAndRead(user, false);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user