feat: add notification system

This commit is contained in:
Tim
2025-07-02 17:16:42 +08:00
parent 93b14f48e4
commit d6b33e65f4
11 changed files with 248 additions and 10 deletions

View File

@@ -0,0 +1,48 @@
package com.openisle.service;
import com.openisle.model.*;
import com.openisle.repository.NotificationRepository;
import com.openisle.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
/** Service for creating and retrieving notifications. */
@Service
@RequiredArgsConstructor
public class NotificationService {
private final NotificationRepository notificationRepository;
private final UserRepository userRepository;
public Notification createNotification(User user, NotificationType type, Post post, Comment comment, Boolean approved) {
Notification n = new Notification();
n.setUser(user);
n.setType(type);
n.setPost(post);
n.setComment(comment);
n.setApproved(approved);
return notificationRepository.save(n);
}
public List<Notification> listNotifications(String username, Boolean read) {
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
if (read == null) {
return notificationRepository.findByUserOrderByCreatedAtDesc(user);
}
return notificationRepository.findByUserAndReadOrderByCreatedAtDesc(user, read);
}
public void markRead(String username, List<Long> ids) {
User user = userRepository.findByUsername(username)
.orElseThrow(() -> new IllegalArgumentException("User not found"));
List<Notification> notifs = notificationRepository.findAllById(ids);
for (Notification n : notifs) {
if (n.getUser().getId().equals(user.getId())) {
n.setRead(true);
}
}
notificationRepository.saveAll(notifs);
}
}