statping/types/groups/database.go

63 lines
1020 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-10 05:24:35 +00:00
var db database.Database
func SetDB(database database.Database) {
db = database.Model(&Group{})
2020-03-04 14:20:47 +00:00
}
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-10 05:24:35 +00:00
q := db.Where("id = ?", id).Find(&group)
return &group, q.Error()
2020-03-04 10:29:00 +00:00
}
func All() []*Group {
var groups []*Group
2020-03-10 05:24:35 +00:00
db.Find(&groups)
2020-03-04 10:29:00 +00:00
return groups
}
func (g *Group) Create() error {
2020-03-10 05:24:35 +00:00
q := db.Create(g)
return q.Error()
2020-03-04 10:29:00 +00:00
}
func (g *Group) Update() error {
2020-03-10 05:24:35 +00:00
q := db.Update(g)
return q.Error()
2020-03-04 10:29:00 +00:00
}
func (g *Group) Delete() error {
2020-03-10 05:24:35 +00:00
q := db.Delete(g)
return q.Error()
2020-03-04 10:29:00 +00:00
}
// 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
}