swirl/dao/bolt/role.go

54 lines
1.1 KiB
Go
Raw Permalink Normal View History

2018-04-27 13:17:00 +00:00
package bolt
import (
2021-12-06 12:24:22 +00:00
"context"
2018-04-27 13:17:00 +00:00
2021-12-23 11:28:31 +00:00
"github.com/cuigh/swirl/dao"
2018-04-27 13:17:00 +00:00
)
2021-12-16 08:11:16 +00:00
const Role = "role"
2021-12-23 11:28:31 +00:00
func (d *Dao) RoleSearch(ctx context.Context, name string) (roles []*dao.Role, err error) {
2021-12-16 08:11:16 +00:00
err = d.each(Role, func(v []byte) error {
2021-12-23 11:28:31 +00:00
role := &dao.Role{}
2021-12-16 08:11:16 +00:00
err = decode(v, role)
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
if matchAny(name, role.Name) {
2018-04-27 13:17:00 +00:00
roles = append(roles, role)
}
2021-12-06 12:24:22 +00:00
return nil
2018-04-27 13:17:00 +00:00
})
return
}
2021-12-23 11:28:31 +00:00
func (d *Dao) RoleCreate(ctx context.Context, role *dao.Role) (err error) {
2021-12-16 08:11:16 +00:00
return d.replace(Role, role.ID, role)
2018-04-27 13:17:00 +00:00
}
2021-12-23 11:28:31 +00:00
func (d *Dao) RoleGet(ctx context.Context, id string) (role *dao.Role, err error) {
role = &dao.Role{}
2021-12-16 08:11:16 +00:00
err = d.get(Role, id, role)
if err == ErrNoRecords {
return nil, nil
} else if err != nil {
role = nil
2018-04-27 13:17:00 +00:00
}
return
}
2021-12-23 11:28:31 +00:00
func (d *Dao) RoleUpdate(ctx context.Context, role *dao.Role) (err error) {
old := &dao.Role{}
2021-12-16 08:11:16 +00:00
return d.update(Role, role.ID, old, func() interface{} {
role.CreatedAt = old.CreatedAt
role.CreatedBy = old.CreatedBy
return role
2018-04-27 13:17:00 +00:00
})
}
2021-12-06 12:24:22 +00:00
func (d *Dao) RoleDelete(ctx context.Context, id string) (err error) {
2021-12-16 08:11:16 +00:00
return d.delete(Role, id)
2018-04-27 13:17:00 +00:00
}