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.
OneAuth/api/role/patch.go

45 lines
1.1 KiB
Go

package role
import (
"github.com/veypi/vbase/cfg"
"github.com/veypi/vbase/models"
"github.com/veypi/vigo"
)
type PatchReq struct {
ID string `src:"path@id" desc:"Role ID"`
Name *string `json:"name" src:"json" desc:"Role Name"`
Description *string `json:"description" src:"json" desc:"Role Description"`
Status *int `json:"status" src:"json" desc:"Status"`
}
func patch(x *vigo.X, req *PatchReq) (*models.Role, error) {
var role models.Role
if err := cfg.DB().First(&role, "id = ?", req.ID).Error; err != nil {
return nil, vigo.ErrNotFound
}
if role.IsSystem {
return nil, vigo.NewError("cannot modify system role").WithCode(40300)
}
updates := map[string]interface{}{}
if req.Name != nil {
updates["name"] = *req.Name
}
if req.Description != nil {
updates["description"] = *req.Description
}
if req.Status != nil {
updates["status"] = *req.Status
}
if len(updates) > 0 {
if err := cfg.DB().Model(&role).Updates(updates).Error; err != nil {
return nil, vigo.ErrDatabase.WithError(err)
}
}
return &role, nil
}