swirl/dao/mongo/role.go
cuigh ca0789ac4e Change mgo pkg
Since original pkg is unmaintained
2018-01-19 16:21:46 +08:00

64 lines
1.2 KiB
Go

package mongo
import (
"github.com/cuigh/swirl/model"
"github.com/globalsign/mgo"
"github.com/globalsign/mgo/bson"
)
func (d *Dao) RoleList() (roles []*model.Role, err error) {
d.do(func(db *database) {
roles = []*model.Role{}
err = db.C("role").Find(nil).All(&roles)
})
return
}
func (d *Dao) RoleCreate(role *model.Role) (err error) {
d.do(func(db *database) {
err = db.C("role").Insert(role)
})
return
}
func (d *Dao) RoleGet(id string) (role *model.Role, err error) {
d.do(func(db *database) {
role = &model.Role{}
err = db.C("role").FindId(id).One(role)
if err == mgo.ErrNotFound {
role, err = nil, nil
} else if err != nil {
role = nil
}
})
return
}
func (d *Dao) RoleUpdate(role *model.Role) (err error) {
d.do(func(db *database) {
update := bson.M{
"$set": bson.M{
"name": role.Name,
"desc": role.Description,
"perms": role.Perms,
"updated_at": role.UpdatedAt,
},
}
err = db.C("role").UpdateId(role.ID, update)
})
return
}
func (d *Dao) RoleDelete(id string) (err error) {
d.do(func(db *database) {
err = db.C("role").RemoveId(id)
if err == nil {
update := bson.M{
"$pull": bson.M{"roles": id},
}
_, err = db.C("user").UpdateAll(nil, update)
}
})
return
}