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