statping/types/groups/database.go

61 lines
1010 B
Go
Raw Normal View History

2020-03-04 10:29:00 +00:00
package groups
import (
"github.com/hunterlong/statping/database"
"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) {
var group *Group
2020-03-04 14:20:47 +00:00
db := DB().Where("id = ?", id).Find(&group)
2020-03-04 10:29:00 +00:00
return group, db.Error()
}
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
}