statping/types/groups/database.go

61 lines
1008 B
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package groups
import (
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/database"
2020-03-04 10:29:00 +00:00
"sort"
)
2020-03-04 14:20:47 +00:00
func DB() database.Database {
return database.DB().Model(&Group{})
}
2020-03-04 10:29:00 +00:00
func Find(id int64) (*Group, error) {
2020-03-08 18:13:27 +00:00
var group Group
2020-03-04 14:20:47 +00:00
db := DB().Where("id = ?", id).Find(&group)
2020-03-08 18:13:27 +00:00
return &group, db.Error()
2020-03-04 10:29:00 +00:00
}
func All() []*Group {
var groups []*Group
2020-03-04 14:20:47 +00:00
DB().Find(&groups)
2020-03-04 10:29:00 +00:00
return groups
}
func (g *Group) Create() error {
2020-03-06 03:03:18 +00:00
db := DB().Create(g)
2020-03-04 10:29:00 +00:00
return db.Error()
}
func (g *Group) Update() error {
2020-03-06 03:03:18 +00:00
db := DB().Update(g)
2020-03-04 10:29:00 +00:00
return db.Error()
}
func (g *Group) Delete() error {
2020-03-06 03:03:18 +00:00
db := DB().Delete(g)
2020-03-04 10:29:00 +00:00
return db.Error()
}
// SelectGroups returns all groups
func SelectGroups(includeAll bool, auth bool) []*Group {
var validGroups []*Group
all := All()
if includeAll {
sort.Sort(GroupOrder(all))
return all
}
for _, g := range all {
if !g.Public.Bool {
if auth {
validGroups = append(validGroups, g)
}
} else {
validGroups = append(validGroups, g)
}
}
sort.Sort(GroupOrder(validGroups))
return validGroups
}