catbase/plugins/roles/role.go

63 lines
1.1 KiB
Go

package roles
import (
"github.com/rs/zerolog/log"
bh "github.com/timshannon/bolthold"
)
type Role struct {
ID int64 `json:"id" boltholdIndex:"ID"`
Name string `json:"name"`
Offering string `json:"offering"`
*bh.Store
}
func NewRole(store *bh.Store, name, offering string) *Role {
return &Role{
Name: name,
Offering: offering,
Store: store,
}
}
func (r *Role) Save() error {
if r.ID == -1 {
r.Store.Upsert(bh.NextSequence(), r)
} else {
r.Store.Upsert(r.ID, r)
}
return nil
}
func getRolesByOffering(store *bh.Store, offering string) ([]*Role, error) {
roles := []*Role{}
err := store.Find(&roles, bh.Where("offering").Eq(offering))
if checkErr(err) {
return nil, err
}
for _, r := range roles {
r.Store = store
r.Store = store
}
return roles, nil
}
func getRole(store *bh.Store, name, offering string) (*Role, error) {
role := &Role{}
err := store.Find(&role, bh.Where("role").Eq(role).And("offering").Eq(offering))
if checkErr(err) {
return nil, err
}
role.Store = store
return role, nil
}
func checkErr(err error) bool {
if err != nil {
log.Error().Err(err).Msg("")
return true
}
return false
}