-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDashboardAlertController.java
More file actions
109 lines (100 loc) · 5.52 KB
/
Copy pathDashboardAlertController.java
File metadata and controls
109 lines (100 loc) · 5.52 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
package com.dbaagent.controller;
import com.dbaagent.model.DashboardAlert;
import com.dbaagent.model.SavedDashboard;
import com.dbaagent.service.DashboardAlertService;
import com.dbaagent.service.SavedDashboardService;
import com.dbaagent.service.DashboardWorkspaceService;
import com.dbaagent.service.security.AccessControlService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
/** CRUD for per-dashboard natural-language alerts — see DashboardAlertService for evaluation. */
@RestController
@RequestMapping("/saved-dashboards/{dashboardId}/alerts")
@RequiredArgsConstructor
@Slf4j
public class DashboardAlertController {
private final DashboardAlertService alertService;
private final SavedDashboardService savedDashboardService;
private final AccessControlService accessControlService;
private final DashboardWorkspaceService dashboardWorkspaceService;
@PostMapping
public ResponseEntity<Map<String, Object>> create(@PathVariable UUID dashboardId, @RequestBody DashboardAlert draft) {
try {
SavedDashboard dashboard = requireDashboard(dashboardId);
accessControlService.assertCanManageConnectionContent(dashboard.getConnectionId());
String username = accessControlService.requireCurrentUsername();
DashboardAlert created = alertService.createAlert(dashboardId, username, draft);
return ResponseEntity.status(HttpStatus.CREATED).body(Map.of("success", true, "alert", created));
} catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of("success", false, "message", e.getMessage()));
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error creating dashboard alert", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("success", false, "message", "Failed to create alert"));
}
}
@GetMapping
public ResponseEntity<Map<String, Object>> list(@PathVariable UUID dashboardId) {
try {
SavedDashboard dashboard = requireDashboard(dashboardId);
accessControlService.assertCanReadConnectionContent(dashboard.getConnectionId());
List<DashboardAlert> alerts = alertService.getAlertsForDashboard(dashboardId);
return ResponseEntity.ok(Map.of("success", true, "alerts", alerts));
} catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of("success", false, "message", e.getMessage()));
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error listing dashboard alerts", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("success", false, "message", "Failed to fetch alerts"));
}
}
@PutMapping("/{alertId}")
public ResponseEntity<Map<String, Object>> update(@PathVariable UUID dashboardId, @PathVariable UUID alertId, @RequestBody DashboardAlert updates) {
try {
SavedDashboard dashboard = requireDashboard(dashboardId);
accessControlService.assertCanManageConnectionContent(dashboard.getConnectionId());
DashboardAlert updated = alertService.updateAlert(alertId, updates);
return ResponseEntity.ok(Map.of("success", true, "alert", updated));
} catch (IllegalArgumentException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of("success", false, "message", e.getMessage()));
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error updating dashboard alert", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("success", false, "message", "Failed to update alert"));
}
}
@DeleteMapping("/{alertId}")
public ResponseEntity<Map<String, Object>> delete(@PathVariable UUID dashboardId, @PathVariable UUID alertId) {
try {
SavedDashboard dashboard = requireDashboard(dashboardId);
accessControlService.assertCanManageConnectionContent(dashboard.getConnectionId());
alertService.deleteAlert(alertId);
return ResponseEntity.ok(Map.of("success", true));
} catch (org.springframework.web.server.ResponseStatusException e) {
throw e;
} catch (Exception e) {
log.error("Error deleting dashboard alert", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("success", false, "message", "Failed to delete alert"));
}
}
/**
* The single point every handler here resolves a dashboard through, so the workspace
* membership gate applies to all of them at once. The connection check stays with
* each caller because read and write paths need different assertions.
*/
private SavedDashboard requireDashboard(UUID dashboardId) {
SavedDashboard dashboard = savedDashboardService.getDashboardById(dashboardId)
.orElseThrow(() -> new IllegalArgumentException("Dashboard not found"));
dashboardWorkspaceService.assertCanReadDashboard(dashboard);
return dashboard;
}
}