作者 郑周

角色删除

... ... @@ -92,47 +92,53 @@ func NewRoleService() *RoleService {
// return role, nil
//}
//
//func (rs *RoleService) Remove(in *command.DeleteRoleCommand) (interface{}, error) {
// transactionContext, err := factory.ValidateStartTransaction(in)
// if err != nil {
// return nil, err
// }
// defer func() {
// transactionContext.RollbackTransaction()
// }()
//
// roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
// roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
//
// role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
// if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// }
// if _, err := roleRepository.Remove(role); err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// }
//
// // 获取角色所有关联的用户,并删除
// _, roleUsers, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.Id, "companyId": in.CompanyId})
// if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// }
// ids := make([]int64, 0)
// for i := range roleUsers {
// ids = append(ids, roleUsers[i].Id)
// }
// if len(ids) > 0 {
// err := roleUserRepository.BatchDeleteById(ids)
// if err != nil {
// return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
// }
// }
//
// if err := transactionContext.CommitTransaction(); err != nil {
// return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
// }
// return role, nil
//}
func (rs *RoleService) Remove(in *command.DeleteRoleCommand) (interface{}, error) {
transactionContext, err := factory.ValidateStartTransaction(in)
if err != nil {
return nil, err
}
defer func() {
transactionContext.RollbackTransaction()
}()
roleRepository := factory.CreateRoleRepository(map[string]interface{}{"transactionContext": transactionContext})
roleUserRepository := factory.CreateRoleUserRepository(map[string]interface{}{"transactionContext": transactionContext})
role, err := roleRepository.FindOne(map[string]interface{}{"id": in.Id})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
if role.Type == domain.RoleTypeSystem {
return nil, application.ThrowError(application.BUSINESS_ERROR, "系统预制角色不可删除")
}
if _, err := roleRepository.Remove(role); err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
// 获取角色所有关联的用户,并删除
_, roleUsers, err := roleUserRepository.Find(map[string]interface{}{"roleId": in.Id, "companyId": in.CompanyId})
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
ids := make([]int64, 0)
for i := range roleUsers {
ids = append(ids, roleUsers[i].Id)
}
if len(ids) > 0 {
err := roleUserRepository.BatchDeleteById(ids)
if err != nil {
return nil, application.ThrowError(application.INTERNAL_SERVER_ERROR, err.Error())
}
}
if err := transactionContext.CommitTransaction(); err != nil {
return nil, application.ThrowError(application.TRANSACTION_ERROR, err.Error())
}
return role, nil
}
func (rs *RoleService) ListForUser(in *command.QueryRoleUserCommand) (interface{}, error) {
transactionContext, err := factory.StartTransaction()
... ...
... ... @@ -12,38 +12,38 @@ type RoleController struct {
beego.BaseController
}
//func (controller *RoleController) CreateRole() {
// ruService := service.NewRoleService()
// in := &command.CreateRoleCommand{}
// if err := controller.Unmarshal(in); err != nil {
// controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
// } else {
// in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
// controller.Response(ruService.Create(in))
// func (controller *RoleController) CreateRole() {
// ruService := service.NewRoleService()
// in := &command.CreateRoleCommand{}
// if err := controller.Unmarshal(in); err != nil {
// controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
// } else {
// in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
// controller.Response(ruService.Create(in))
// }
// }
//}
//
//func (controller *RoleController) UpdateRole() {
// ruService := service.NewRoleService()
// in := &command.UpdateRoleCommand{}
// if err := controller.Unmarshal(in); err != nil {
// controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
// } else {
// in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
// controller.Response(ruService.Update(in))
// func (controller *RoleController) UpdateRole() {
// ruService := service.NewRoleService()
// in := &command.UpdateRoleCommand{}
// if err := controller.Unmarshal(in); err != nil {
// controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
// } else {
// in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
// controller.Response(ruService.Update(in))
// }
// }
//}
//
//func (controller *RoleController) RemoveRole() {
// ruService := service.NewRoleService()
// in := &command.DeleteRoleCommand{}
// if err := controller.Unmarshal(in); err != nil {
// controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
// } else {
// in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
// controller.Response(ruService.Remove(in))
// }
//}
func (controller *RoleController) RemoveRole() {
ruService := service.NewRoleService()
in := &command.DeleteRoleCommand{}
if err := controller.Unmarshal(in); err != nil {
controller.Response(nil, application.ThrowError(application.ARG_ERROR, err.Error()))
} else {
ua := middlewares.GetUser(controller.Ctx)
in.CompanyId = ua.CompanyId
controller.Response(ruService.Remove(in))
}
}
func (controller *RoleController) ListForUserRole() {
ruService := service.NewRoleService()
... ... @@ -53,7 +53,6 @@ func (controller *RoleController) ListForUserRole() {
} else {
ua := middlewares.GetUser(controller.Ctx)
in.CompanyId = ua.CompanyId
//in.CompanyId = middlewares.GetCompanyId(controller.Ctx)
controller.Response(ruService.ListForUser(in))
}
}
... ...
... ... @@ -12,7 +12,7 @@ func init() {
web.NSBefore(filters.AllowCors(), middlewares.CheckAdminToken()),
//web.NSRouter("/", &controllers.RoleController{}, "Post:CreateRole"),
//web.NSRouter("/", &controllers.RoleController{}, "Put:UpdateRole"),
//web.NSRouter("/", &controllers.RoleController{}, "Delete:RemoveRole"),
web.NSRouter("/", &controllers.RoleController{}, "Delete:RemoveRole"),
//web.NSRouter("/:Id", &controllers.RoleController{}, "Get:GetRole"),
web.NSRouter("/all", &controllers.RoleController{}, "Post:ListForUserRole"),
)
... ...