2018-12-31 11:41:19 +00:00
|
|
|
package core
|
|
|
|
|
2018-12-31 21:36:58 +00:00
|
|
|
import (
|
|
|
|
"github.com/hunterlong/statping/types"
|
|
|
|
"time"
|
|
|
|
)
|
2018-12-31 11:41:19 +00:00
|
|
|
|
2018-12-31 21:36:58 +00:00
|
|
|
type Group struct {
|
|
|
|
*types.Group
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete will remove a group
|
|
|
|
func (g *Group) Delete() error {
|
2019-01-03 19:13:48 +00:00
|
|
|
for _, s := range g.Services() {
|
|
|
|
s.GroupId = 0
|
|
|
|
s.Update(false)
|
|
|
|
}
|
2018-12-31 21:36:58 +00:00
|
|
|
err := messagesDb().Delete(g)
|
|
|
|
if err.Error != nil {
|
|
|
|
return err.Error
|
|
|
|
}
|
|
|
|
return err.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create will create a group and insert it into the database
|
|
|
|
func (g *Group) Create() (int64, error) {
|
|
|
|
g.CreatedAt = time.Now()
|
|
|
|
db := groupsDb().Create(g)
|
|
|
|
return g.Id, db.Error
|
|
|
|
}
|
|
|
|
|
2019-01-03 19:13:48 +00:00
|
|
|
// Services returns all services belonging to a group
|
|
|
|
func (g *Group) Services() []*Service {
|
|
|
|
var services []*Service
|
|
|
|
for _, s := range Services() {
|
|
|
|
if s.Select().GroupId == int(g.Id) {
|
|
|
|
services = append(services, s.(*Service))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return services
|
|
|
|
}
|
|
|
|
|
2018-12-31 21:36:58 +00:00
|
|
|
// SelectGroups returns all groups
|
2019-01-03 21:09:11 +00:00
|
|
|
func SelectGroups(includeAll bool, auth bool) []*Group {
|
2018-12-31 21:36:58 +00:00
|
|
|
var groups []*Group
|
2019-01-03 21:09:11 +00:00
|
|
|
var validGroups []*Group
|
2018-12-31 21:36:58 +00:00
|
|
|
groupsDb().Find(&groups).Order("id desc")
|
2019-01-03 21:09:11 +00:00
|
|
|
if includeAll {
|
|
|
|
emptyGroup := &Group{&types.Group{Id: 0, Public: types.NewNullBool(true)}}
|
|
|
|
groups = append(groups, emptyGroup)
|
|
|
|
}
|
|
|
|
for _, g := range groups {
|
|
|
|
if !g.Public.Bool {
|
|
|
|
if auth {
|
|
|
|
validGroups = append(validGroups, g)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
validGroups = append(validGroups, g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return validGroups
|
2018-12-31 21:36:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SelectGroup returns a *core.Group
|
|
|
|
func SelectGroup(id int64) *Group {
|
2019-01-03 21:09:11 +00:00
|
|
|
for _, g := range SelectGroups(false, false) {
|
2018-12-31 21:36:58 +00:00
|
|
|
if g.Id == id {
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2018-12-31 11:41:19 +00:00
|
|
|
}
|