2020-03-14 03:13:20 +00:00
|
|
|
package notifications
|
2020-03-04 10:29:00 +00:00
|
|
|
|
|
|
|
import (
|
2020-03-23 02:50:30 +00:00
|
|
|
"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(¬ification)
|
2020-03-23 02:50:30 +00:00
|
|
|
if ¬ification == nil {
|
|
|
|
return nil, errors.New("cannot find notifier")
|
|
|
|
}
|
2020-03-14 03:13:20 +00:00
|
|
|
return ¬ification, 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(¬ifications)
|
|
|
|
return notifications
|
2020-03-04 10:29:00 +00:00
|
|
|
}
|