statping/types/notifications/database.go

52 lines
934 B
Go
Raw Normal View History

2020-03-14 03:13:20 +00:00
package notifications
2020-03-04 10:29:00 +00:00
import (
"errors"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/database"
2020-03-04 10:29:00 +00:00
)
2020-03-14 03:13:20 +00:00
var (
db database.Database
)
2020-03-10 05:24:35 +00:00
func SetDB(database database.Database) {
db = database.Model(&Notification{})
2020-03-04 10:29:00 +00:00
}
2020-03-14 03:13:20 +00:00
func Find(method string) (*Notification, error) {
var notification Notification
q := db.Where("method = ?", method).Find(&notification)
if &notification == nil {
return nil, errors.New("cannot find notifier")
}
2020-03-14 03:13:20 +00:00
return &notification, q.Error()
2020-03-04 10:29:00 +00:00
}
func (n *Notification) Create() error {
2020-03-12 04:58:56 +00:00
q := db.Where("method = ?", n.Method).Find(n)
if q.RecordNotFound() {
if err := db.Create(n).Error(); err != nil {
return err
}
2020-03-09 15:15:15 +00:00
}
return nil
2020-03-04 10:29:00 +00:00
}
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()
} else {
n.Close()
}
2020-03-10 05:24:35 +00:00
err := db.Update(n)
2020-03-06 03:03:18 +00:00
return err.Error()
2020-03-04 10:29:00 +00:00
}
2020-03-14 03:13:20 +00:00
func loadAll() []*Notification {
var notifications []*Notification
db.Find(&notifications)
return notifications
2020-03-04 10:29:00 +00:00
}