From b907ce341c6a40f800fbdd41df32be4a4d19d964 Mon Sep 17 00:00:00 2001 From: bergquist Date: Thu, 16 Jun 2016 15:21:44 +0200 Subject: [PATCH] feat(alerting): enables deletes for alert notifications --- pkg/api/alerting.go | 13 +++++++++++++ pkg/api/api.go | 1 + pkg/models/alert_notifications.go | 5 +++++ pkg/services/sqlstore/alert_notification.go | 14 ++++++++++++++ .../features/alerting/notification_edit_ctrl.ts | 10 +++++++--- .../alerting/notifications_list_ctrl.ts | 15 ++++++++++++++- .../alerting/partials/notifications_list.html | 17 ++++++++++------- 7 files changed, 64 insertions(+), 11 deletions(-) diff --git a/pkg/api/alerting.go b/pkg/api/alerting.go index 514a8498056..869fb726410 100644 --- a/pkg/api/alerting.go +++ b/pkg/api/alerting.go @@ -201,3 +201,16 @@ func UpdateAlertNotification(c *middleware.Context, cmd models.UpdateAlertNotifi return Json(200, cmd.Result) } + +func DeleteAlertNotification(c *middleware.Context) Response { + cmd := models.DeleteAlertNotificationCommand{ + OrgId: c.OrgId, + Id: c.ParamsInt64("notificationId"), + } + + if err := bus.Dispatch(&cmd); err != nil { + return ApiError(500, "Failed to delete alert notification", err) + } + + return Json(200, map[string]interface{}{"notificationId": cmd.Id}) +} diff --git a/pkg/api/api.go b/pkg/api/api.go index 706a46c0ec7..022edb9c74c 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -257,6 +257,7 @@ func Register(r *macaron.Macaron) { r.Post("/", bind(m.CreateAlertNotificationCommand{}), wrap(CreateAlertNotification)) r.Put("/:notificationId", bind(m.UpdateAlertNotificationCommand{}), wrap(UpdateAlertNotification)) r.Get("/:notificationId", wrap(GetAlertNotificationById)) + r.Delete("/:notificationId", wrap(DeleteAlertNotification)) }) r.Get("/changes", wrap(GetAlertChanges)) diff --git a/pkg/models/alert_notifications.go b/pkg/models/alert_notifications.go index 55318b9b285..85fdbafe9c4 100644 --- a/pkg/models/alert_notifications.go +++ b/pkg/models/alert_notifications.go @@ -35,6 +35,11 @@ type UpdateAlertNotificationCommand struct { Result *AlertNotification } +type DeleteAlertNotificationCommand struct { + Id int64 + OrgId int64 +} + type GetAlertNotificationQuery struct { Name string Id int64 diff --git a/pkg/services/sqlstore/alert_notification.go b/pkg/services/sqlstore/alert_notification.go index 7694c7bf0e0..4c4bdb6e4c4 100644 --- a/pkg/services/sqlstore/alert_notification.go +++ b/pkg/services/sqlstore/alert_notification.go @@ -15,6 +15,20 @@ func init() { bus.AddHandler("sql", AlertNotificationQuery) bus.AddHandler("sql", CreateAlertNotificationCommand) bus.AddHandler("sql", UpdateAlertNotification) + bus.AddHandler("sql", DeleteAlertNotification) +} + +func DeleteAlertNotification(cmd *m.DeleteAlertNotificationCommand) error { + return inTransaction(func(sess *xorm.Session) error { + sql := "DELETE FROM alert_notification WHERE alert_notification.org_id = ? AND alert_notification.id = ?" + _, err := sess.Exec(sql, cmd.OrgId, cmd.Id) + + if err != nil { + return err + } + + return nil + }) } func AlertNotificationQuery(query *m.GetAlertNotificationQuery) error { diff --git a/public/app/features/alerting/notification_edit_ctrl.ts b/public/app/features/alerting/notification_edit_ctrl.ts index 796d57aa2f8..08b4c41b361 100644 --- a/public/app/features/alerting/notification_edit_ctrl.ts +++ b/public/app/features/alerting/notification_edit_ctrl.ts @@ -10,7 +10,7 @@ export class AlertNotificationEditCtrl { notification: any; /** @ngInject */ - constructor(private $routeParams, private backendSrv) { + constructor(private $routeParams, private backendSrv, private $scope) { if ($routeParams.notificationId) { this.loadNotification($routeParams.notificationId); } @@ -33,13 +33,17 @@ export class AlertNotificationEditCtrl { this.backendSrv.put(`/api/alerts/notification/${this.notification.id}`, this.notification) .then(result => { this.notification = result; - console.log('updated notification', result); + this.$scope.appEvent('alert-success', ['Notification created!', '']); + }, () => { + this.$scope.appEvent('alert-error', ['Unable to create notification.', '']); }); } else { this.backendSrv.post(`/api/alerts/notification`, this.notification) .then(result => { this.notification = result; - console.log('created new notification', result); + this.$scope.appEvent('alert-success', ['Notification updated!', '']); + }, () => { + this.$scope.appEvent('alert-error', ['Unable to update notification.', '']); }); } } diff --git a/public/app/features/alerting/notifications_list_ctrl.ts b/public/app/features/alerting/notifications_list_ctrl.ts index 41458a08577..54362104b31 100644 --- a/public/app/features/alerting/notifications_list_ctrl.ts +++ b/public/app/features/alerting/notifications_list_ctrl.ts @@ -10,7 +10,7 @@ export class AlertNotificationsListCtrl { notifications: any; /** @ngInject */ - constructor(private backendSrv) { + constructor(private backendSrv, private $scope) { this.loadNotifications(); } @@ -19,7 +19,20 @@ export class AlertNotificationsListCtrl { this.notifications = result; }); } + + deleteNotification(notificationId) { + this.backendSrv.delete(`/api/alerts/notification/${notificationId}`) + .then(() => { + this.notifications = this.notifications.filter(notification => { + return notification.id !== notificationId; + }); + this.$scope.appEvent('alert-success', ['Notification deleted', '']); + }, () => { + this.$scope.appEvent('alert-error', ['Unable to delete notification', '']); + }); + } } coreModule.controller('AlertNotificationsListCtrl', AlertNotificationsListCtrl); + diff --git a/public/app/features/alerting/partials/notifications_list.html b/public/app/features/alerting/partials/notifications_list.html index c3347018cd4..be0a44fa0e0 100644 --- a/public/app/features/alerting/partials/notifications_list.html +++ b/public/app/features/alerting/partials/notifications_list.html @@ -4,32 +4,35 @@
- +
- + - -
NameTypeType
- {{alert.name}} + {{notification.name}} + {{notification.type}} + edit + + +