|
| 1 | +package com.dbaagent.controller; |
| 2 | + |
| 3 | +import com.dbaagent.model.DashboardAlert; |
| 4 | +import com.dbaagent.model.SavedDashboard; |
| 5 | +import com.dbaagent.service.DashboardAlertService; |
| 6 | +import com.dbaagent.service.SavedDashboardService; |
| 7 | +import com.dbaagent.service.security.AccessControlService; |
| 8 | +import lombok.RequiredArgsConstructor; |
| 9 | +import lombok.extern.slf4j.Slf4j; |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.web.bind.annotation.*; |
| 13 | + |
| 14 | +import java.util.List; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.UUID; |
| 17 | + |
| 18 | +/** CRUD for per-dashboard natural-language alerts — see DashboardAlertService for evaluation. */ |
| 19 | +@RestController |
| 20 | +@RequestMapping("/saved-dashboards/{dashboardId}/alerts") |
| 21 | +@RequiredArgsConstructor |
| 22 | +@Slf4j |
| 23 | +public class DashboardAlertController { |
| 24 | + |
| 25 | + private final DashboardAlertService alertService; |
| 26 | + private final SavedDashboardService savedDashboardService; |
| 27 | + private final AccessControlService accessControlService; |
| 28 | + |
| 29 | + @PostMapping |
| 30 | + public ResponseEntity<Map<String, Object>> create(@PathVariable UUID dashboardId, @RequestBody DashboardAlert draft) { |
| 31 | + try { |
| 32 | + SavedDashboard dashboard = requireDashboard(dashboardId); |
| 33 | + accessControlService.assertCanManageConnectionContent(dashboard.getConnectionId()); |
| 34 | + String username = accessControlService.requireCurrentUsername(); |
| 35 | + DashboardAlert created = alertService.createAlert(dashboardId, username, draft); |
| 36 | + return ResponseEntity.status(HttpStatus.CREATED).body(Map.of("success", true, "alert", created)); |
| 37 | + } catch (IllegalArgumentException e) { |
| 38 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of("success", false, "message", e.getMessage())); |
| 39 | + } catch (org.springframework.web.server.ResponseStatusException e) { |
| 40 | + throw e; |
| 41 | + } catch (Exception e) { |
| 42 | + log.error("Error creating dashboard alert", e); |
| 43 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("success", false, "message", "Failed to create alert")); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @GetMapping |
| 48 | + public ResponseEntity<Map<String, Object>> list(@PathVariable UUID dashboardId) { |
| 49 | + try { |
| 50 | + SavedDashboard dashboard = requireDashboard(dashboardId); |
| 51 | + accessControlService.assertCanReadConnectionContent(dashboard.getConnectionId()); |
| 52 | + List<DashboardAlert> alerts = alertService.getAlertsForDashboard(dashboardId); |
| 53 | + return ResponseEntity.ok(Map.of("success", true, "alerts", alerts)); |
| 54 | + } catch (IllegalArgumentException e) { |
| 55 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of("success", false, "message", e.getMessage())); |
| 56 | + } catch (org.springframework.web.server.ResponseStatusException e) { |
| 57 | + throw e; |
| 58 | + } catch (Exception e) { |
| 59 | + log.error("Error listing dashboard alerts", e); |
| 60 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("success", false, "message", "Failed to fetch alerts")); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @PutMapping("/{alertId}") |
| 65 | + public ResponseEntity<Map<String, Object>> update(@PathVariable UUID dashboardId, @PathVariable UUID alertId, @RequestBody DashboardAlert updates) { |
| 66 | + try { |
| 67 | + SavedDashboard dashboard = requireDashboard(dashboardId); |
| 68 | + accessControlService.assertCanManageConnectionContent(dashboard.getConnectionId()); |
| 69 | + DashboardAlert updated = alertService.updateAlert(alertId, updates); |
| 70 | + return ResponseEntity.ok(Map.of("success", true, "alert", updated)); |
| 71 | + } catch (IllegalArgumentException e) { |
| 72 | + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(Map.of("success", false, "message", e.getMessage())); |
| 73 | + } catch (org.springframework.web.server.ResponseStatusException e) { |
| 74 | + throw e; |
| 75 | + } catch (Exception e) { |
| 76 | + log.error("Error updating dashboard alert", e); |
| 77 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("success", false, "message", "Failed to update alert")); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @DeleteMapping("/{alertId}") |
| 82 | + public ResponseEntity<Map<String, Object>> delete(@PathVariable UUID dashboardId, @PathVariable UUID alertId) { |
| 83 | + try { |
| 84 | + SavedDashboard dashboard = requireDashboard(dashboardId); |
| 85 | + accessControlService.assertCanManageConnectionContent(dashboard.getConnectionId()); |
| 86 | + alertService.deleteAlert(alertId); |
| 87 | + return ResponseEntity.ok(Map.of("success", true)); |
| 88 | + } catch (org.springframework.web.server.ResponseStatusException e) { |
| 89 | + throw e; |
| 90 | + } catch (Exception e) { |
| 91 | + log.error("Error deleting dashboard alert", e); |
| 92 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of("success", false, "message", "Failed to delete alert")); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + private SavedDashboard requireDashboard(UUID dashboardId) { |
| 97 | + return savedDashboardService.getDashboardById(dashboardId) |
| 98 | + .orElseThrow(() -> new IllegalArgumentException("Dashboard not found")); |
| 99 | + } |
| 100 | +} |
0 commit comments