mirror of https://github.com/veypi/OneAuth.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
package role
|
|
|
|
import (
|
|
"github.com/veypi/OneBD/rest"
|
|
"oa/models"
|
|
"oa/cfg"
|
|
)
|
|
|
|
var _ = Router.Patch("/:role_id", updateRole)
|
|
|
|
type updateOpts struct {
|
|
RoleID string `parse:"path"` // 角色 ID
|
|
Name *string `json:"name"` // 可选,角色名称
|
|
Des *string `json:"des"` // 可选,角色描述
|
|
}
|
|
|
|
func updateRole(x *rest.X) (any, error) {
|
|
// 解析参数
|
|
opts := &updateOpts{}
|
|
if err := x.Parse(opts); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 查找角色
|
|
role := &models.Role{}
|
|
if err := cfg.DB().Where("id = ?", opts.RoleID).First(role).Error; err != nil {
|
|
return nil, rest.NewError("role not found").WithCode(404)
|
|
}
|
|
|
|
// 准备更新字段
|
|
updates := map[string]interface{}{}
|
|
|
|
if opts.Name != nil {
|
|
updates["name"] = *opts.Name
|
|
}
|
|
|
|
if opts.Des != nil {
|
|
updates["des"] = *opts.Des
|
|
}
|
|
|
|
// 如果没有更新字段,直接返回
|
|
if len(updates) == 0 {
|
|
return role, nil
|
|
}
|
|
|
|
// 执行更新
|
|
if err := cfg.DB().Model(role).Updates(updates).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 重新查询以获取最新数据
|
|
if err := cfg.DB().Where("id = ?", opts.RoleID).First(role).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return role, nil
|
|
}
|