forked from statping/statping
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.go
72 lines (65 loc) · 1.43 KB
/
groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package core
import (
"github.com/hunterlong/statping/types"
"time"
)
type Group struct {
*types.Group
}
// Delete will remove a group
func (g *Group) Delete() error {
for _, s := range g.Services() {
s.GroupId = 0
s.Update(false)
}
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
}
// 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
}
// SelectGroups returns all groups
func SelectGroups(includeAll bool, auth bool) []*Group {
var groups []*Group
var validGroups []*Group
groupsDb().Find(&groups).Order("id desc")
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
}
// SelectGroup returns a *core.Group
func SelectGroup(id int64) *Group {
for _, g := range SelectGroups(false, false) {
if g.Id == id {
return g
}
}
return nil
}