NotificationController.java
7.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package com.ecommerce.notification.controller;
import com.ecommerce.notification.model.dto.NotificationRequest;
import com.ecommerce.notification.model.dto.NotificationResponse;
import com.ecommerce.notification.service.NotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/api/notifications")
@RequiredArgsConstructor
public class NotificationController {
private final NotificationService notificationService;
@PostMapping
public ResponseEntity<NotificationResponse> sendNotification(@Valid @RequestBody NotificationRequest request) {
NotificationResponse response = notificationService.sendNotification(request);
return ResponseEntity.ok(response);
}
@GetMapping("/{notificationId}")
public ResponseEntity<NotificationResponse> getNotification(@PathVariable String notificationId) {
NotificationResponse response = notificationService.getNotification(notificationId);
return ResponseEntity.ok(response);
}
@GetMapping
public ResponseEntity<Map<String, Object>> getAllNotifications(
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size,
@RequestParam(defaultValue = "createdAt") String sortBy,
@RequestParam(defaultValue = "desc") String sortDirection) {
Sort sort = sortDirection.equalsIgnoreCase("desc") ?
Sort.by(sortBy).descending() : Sort.by(sortBy).ascending();
Pageable pageable = PageRequest.of(page, size, sort);
Page<NotificationResponse> notifications = notificationService.getAllNotifications(pageable);
Map<String, Object> response = new HashMap<>();
response.put("notifications", notifications.getContent());
response.put("currentPage", notifications.getNumber());
response.put("totalItems", notifications.getTotalElements());
response.put("totalPages", notifications.getTotalPages());
return ResponseEntity.ok(response);
}
@GetMapping("/user/{userId}")
public ResponseEntity<Map<String, Object>> getNotificationsByUser(
@PathVariable Long userId,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by("createdAt").descending());
Page<NotificationResponse> notifications = notificationService.getNotificationsByUser(userId, pageable);
Map<String, Object> response = new HashMap<>();
response.put("notifications", notifications.getContent());
response.put("currentPage", notifications.getNumber());
response.put("totalItems", notifications.getTotalElements());
response.put("totalPages", notifications.getTotalPages());
return ResponseEntity.ok(response);
}
@GetMapping("/email/{email}")
public ResponseEntity<Map<String, Object>> getNotificationsByEmail(
@PathVariable String email,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by("createdAt").descending());
Page<NotificationResponse> notifications = notificationService.getNotificationsByEmail(email, pageable);
Map<String, Object> response = new HashMap<>();
response.put("notifications", notifications.getContent());
response.put("currentPage", notifications.getNumber());
response.put("totalItems", notifications.getTotalElements());
response.put("totalPages", notifications.getTotalPages());
return ResponseEntity.ok(response);
}
@GetMapping("/type/{type}")
public ResponseEntity<Map<String, Object>> getNotificationsByType(
@PathVariable String type,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by("createdAt").descending());
Page<NotificationResponse> notifications = notificationService.getNotificationsByType(type, pageable);
Map<String, Object> response = new HashMap<>();
response.put("notifications", notifications.getContent());
response.put("currentPage", notifications.getNumber());
response.put("totalItems", notifications.getTotalElements());
response.put("totalPages", notifications.getTotalPages());
return ResponseEntity.ok(response);
}
@GetMapping("/channel/{channel}")
public ResponseEntity<Map<String, Object>> getNotificationsByChannel(
@PathVariable String channel,
@RequestParam(defaultValue = "0") int page,
@RequestParam(defaultValue = "20") int size) {
Pageable pageable = PageRequest.of(page, size, Sort.by("createdAt").descending());
Page<NotificationResponse> notifications = notificationService.getNotificationsByChannel(channel, pageable);
Map<String, Object> response = new HashMap<>();
response.put("notifications", notifications.getContent());
response.put("currentPage", notifications.getNumber());
response.put("totalItems", notifications.getTotalElements());
response.put("totalPages", notifications.getTotalPages());
return ResponseEntity.ok(response);
}
@PostMapping("/{notificationId}/read")
public ResponseEntity<NotificationResponse> markAsRead(@PathVariable String notificationId) {
NotificationResponse response = notificationService.markAsRead(notificationId);
return ResponseEntity.ok(response);
}
@PostMapping("/{notificationId}/retry")
public ResponseEntity<NotificationResponse> retryNotification(@PathVariable String notificationId) {
NotificationResponse response = notificationService.retryNotification(notificationId);
return ResponseEntity.ok(response);
}
@PostMapping("/process-pending")
public ResponseEntity<Map<String, String>> processPendingNotifications() {
notificationService.processPendingNotifications();
Map<String, String> response = new HashMap<>();
response.put("message", "Pending notifications processing started");
response.put("timestamp", LocalDateTime.now().toString());
return ResponseEntity.ok(response);
}
@GetMapping("/statistics")
public ResponseEntity<Map<String, Object>> getNotificationStatistics(
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDate,
@RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDate) {
Map<String, Object> statistics = notificationService.getNotificationStatistics(startDate, endDate);
return ResponseEntity.ok(statistics);
}
@GetMapping("/health")
public ResponseEntity<Map<String, String>> healthCheck() {
Map<String, String> response = new HashMap<>();
response.put("status", "UP");
response.put("service", "notification-service");
response.put("timestamp", LocalDateTime.now().toString());
return ResponseEntity.ok(response);
}
}