statping/types/notifications/database.go

49 lines
833 B
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package notifications
import (
"errors"
"github.com/hunterlong/statping/database"
)
func DB() database.Database {
return database.DB().Model(&Notification{})
}
2020-03-06 03:03:18 +00:00
func Find(name string) (Notifier, error) {
2020-03-04 10:29:00 +00:00
for _, n := range AllCommunications {
notif := n.Select()
2020-03-06 03:03:18 +00:00
if notif.Name() == name || notif.Method == name {
2020-03-04 10:29:00 +00:00
return n, nil
}
}
return nil, errors.New("notifier not found")
}
func All() []*Notification {
var notifiers []*Notification
DB().Find(&notifiers)
return notifiers
}
func (n *Notification) Create() error {
2020-03-06 03:03:18 +00:00
db := DB().Create(n)
2020-03-04 10:29:00 +00:00
return db.Error()
}
2020-03-06 03:03:18 +00:00
func (n *Notification) Update() error {
2020-03-04 10:29:00 +00:00
n.ResetQueue()
if n.Enabled.Bool {
n.Close()
n.Start()
2020-03-06 03:03:18 +00:00
go Queue(Notifier(n))
2020-03-04 10:29:00 +00:00
} else {
n.Close()
}
2020-03-06 03:03:18 +00:00
err := DB().Update(n)
return err.Error()
2020-03-04 10:29:00 +00:00
}
func (n *Notification) Delete() error {
return nil
}