1 | package com.example.skychasemk.services;
|
---|
2 |
|
---|
3 | import com.example.skychasemk.model.Notification;
|
---|
4 | import com.example.skychasemk.repository.NotificationRepository;
|
---|
5 | import org.springframework.beans.factory.annotation.Autowired;
|
---|
6 | import org.springframework.stereotype.Service;
|
---|
7 |
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Optional;
|
---|
10 |
|
---|
11 | @Service
|
---|
12 | public class NotificationService {
|
---|
13 |
|
---|
14 | @Autowired
|
---|
15 | private NotificationRepository notificationRepository;
|
---|
16 |
|
---|
17 | // Get all notifications
|
---|
18 | public List<Notification> getAllNotifications() {
|
---|
19 | return notificationRepository.findAll();
|
---|
20 | }
|
---|
21 |
|
---|
22 | // Get notification by ID
|
---|
23 | public Optional<Notification> getNotificationById(Integer notificationID) {
|
---|
24 | return notificationRepository.findById(notificationID);
|
---|
25 | }
|
---|
26 |
|
---|
27 | // Save a new notification
|
---|
28 | public Notification saveNotification(Notification notification) {
|
---|
29 | return notificationRepository.save(notification);
|
---|
30 | }
|
---|
31 |
|
---|
32 | // Update an existing notification
|
---|
33 | public Notification updateNotification(Integer notificationID, Notification notification) {
|
---|
34 | if (notificationRepository.existsById(notificationID)) {
|
---|
35 | notification.setNotificationID(notificationID);
|
---|
36 | return notificationRepository.save(notification);
|
---|
37 | } else {
|
---|
38 | throw new RuntimeException("Notification not found with id " + notificationID);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | // Delete notification
|
---|
43 | public void deleteNotification(Integer notificationID) {
|
---|
44 | notificationRepository.deleteById(notificationID);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|