package resource import ( "github.com/veypi/OneBD/rest" "github.com/veypi/OneAuth/cfg" "github.com/veypi/OneAuth/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 }