swirl/dao/mongo/role.go

58 lines
1.2 KiB
Go
Raw Permalink Normal View History

2017-09-26 12:50:09 +00:00
package mongo
import (
2021-12-06 12:24:22 +00:00
"context"
2021-12-23 11:28:31 +00:00
"github.com/cuigh/swirl/dao"
2021-12-06 12:24:22 +00:00
"go.mongodb.org/mongo-driver/bson"
2017-09-26 12:50:09 +00:00
)
2021-12-06 12:24:22 +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-06 12:24:22 +00:00
filter := bson.M{}
if name != "" {
filter["name"] = name
}
2021-12-23 11:28:31 +00:00
roles = []*dao.Role{}
2021-12-06 12:24:22 +00:00
err = d.fetch(ctx, Role, filter, &roles)
2017-09-26 12:50:09 +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-06 12:24:22 +00:00
return d.create(ctx, Role, role)
2017-09-26 12:50:09 +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-06 12:24:22 +00:00
found, err := d.find(ctx, Role, id, role)
if !found {
return nil, err
}
2017-09-26 12:50:09 +00:00
return
}
2021-12-23 11:28:31 +00:00
func (d *Dao) RoleUpdate(ctx context.Context, role *dao.Role) (err error) {
2021-12-06 12:24:22 +00:00
update := bson.M{
"$set": bson.M{
"name": role.Name,
"desc": role.Description,
"perms": role.Perms,
"updated_at": role.UpdatedAt,
2021-12-16 08:11:16 +00:00
"updated_by": role.UpdatedBy,
2021-12-06 12:24:22 +00:00
},
}
return d.update(ctx, Role, role.ID, update)
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
func (d *Dao) RoleDelete(ctx context.Context, id string) (err error) {
err = d.delete(ctx, Role, id)
if err == nil {
update := bson.M{
"$pull": bson.M{"roles": id},
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
_, err = d.db.Collection(User).UpdateMany(ctx, bson.M{}, update)
}
2017-09-26 12:50:09 +00:00
return
}