statping/core/groups.go

53 lines
1.2 KiB
Go
Raw Normal View History

2018-12-31 11:41:19 +00:00
package core
2018-12-31 21:36:58 +00:00
import (
2020-02-25 07:41:28 +00:00
"github.com/hunterlong/statping/database"
2018-12-31 21:36:58 +00:00
"github.com/hunterlong/statping/types"
2019-02-20 02:11:40 +00:00
"sort"
2018-12-31 21:36:58 +00:00
)
2018-12-31 11:41:19 +00:00
2018-12-31 21:36:58 +00:00
type Group struct {
2020-02-25 07:41:28 +00:00
database.Grouper
2018-12-31 21:36:58 +00:00
}
2020-02-25 07:41:28 +00:00
// SelectGroups returns all groups
func SelectGroups(includeAll bool, auth bool) []database.Grouper {
var validGroups []database.Grouper
2019-01-03 19:13:48 +00:00
2020-02-25 07:41:28 +00:00
groups := database.AllGroups()
2019-01-03 21:09:11 +00:00
for _, g := range groups {
2020-02-25 07:41:28 +00:00
if !g.Model().Public.Bool {
2019-01-03 21:09:11 +00:00
if auth {
validGroups = append(validGroups, g)
}
} else {
validGroups = append(validGroups, g)
}
}
2019-02-20 02:11:40 +00:00
sort.Sort(GroupOrder(validGroups))
if includeAll {
2020-02-25 07:41:28 +00:00
emptyGroup := &Group{}
validGroups = append(validGroups, emptyGroup)
}
2019-01-03 21:09:11 +00:00
return validGroups
2018-12-31 21:36:58 +00:00
}
// SelectGroup returns a *core.Group
2020-02-25 07:41:28 +00:00
func SelectGroup(id int64) *types.Group {
2019-02-20 02:11:40 +00:00
for _, g := range SelectGroups(true, true) {
2020-02-25 07:41:28 +00:00
if g.Model().Id == id {
return g.Model()
2018-12-31 21:36:58 +00:00
}
}
return nil
2018-12-31 11:41:19 +00:00
}
2019-02-20 02:11:40 +00:00
// GroupOrder will reorder the groups based on 'order_id' (Order)
2020-02-25 07:41:28 +00:00
type GroupOrder []database.Grouper
2019-02-20 02:11:40 +00:00
// Sort interface for resorting the Groups in order
func (c GroupOrder) Len() int { return len(c) }
func (c GroupOrder) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
2020-02-25 07:41:28 +00:00
func (c GroupOrder) Less(i, j int) bool { return c[i].Model().Order < c[j].Model().Order }