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/app/resource/patch.go

48 lines
1.0 KiB
Go

package resource
import (
"github.com/veypi/OneBD/rest"
"oa/cfg"
"oa/models"
)
var _ = Router.Patch("/:resource_id", updateResource)
type updateOpts struct {
ResourceID string `parse:"path"`
Name *string `json:"name" parse:"json"`
Des *string `json:"des" parse:"json"`
}
func updateResource(x *rest.X) (any, error) {
// 解析参数
opts := &updateOpts{}
if err := x.Parse(opts); err != nil {
return nil, err
}
// 查找资源
resource := &models.Resource{}
if err := cfg.DB().Where("id = ?", opts.ResourceID).First(resource).Error; err != nil {
return nil, rest.NewError("resource 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 {
if err := cfg.DB().Model(resource).Updates(updates).Error; err != nil {
return nil, rest.NewError("failed to update resource").WithCode(500)
}
}
return resource, nil
}