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

57 lines
1.3 KiB
Go

//
// patch.go
// Copyright (C) 2024 veypi <i@veypi.com>
// Distributed under terms of the MIT license.
//
package access
import (
"github.com/veypi/vbase/cfg"
"github.com/veypi/vbase/models"
"github.com/veypi/vigo"
)
type updateOpts struct {
ID string `src:"path@id" desc:"记录ID"`
Name *string `json:"name" src:"json" desc:"名称"`
TID *string `json:"tid" src:"json" desc:"资源ID"`
Level *uint `json:"level" src:"json" desc:"级别"`
}
var _ = Router.Patch("/{id}", "更新访问权限", updateAccess)
func updateAccess(x *vigo.X, opts *updateOpts) (*models.Access, error) {
// 查找记录
var access models.Access
if err := cfg.DB().Where("id = ?", opts.ID).First(&access).Error; err != nil {
return nil, vigo.NewError("未找到资源").WithCode(404)
}
// 更新字段
updates := map[string]interface{}{}
if opts.Name != nil {
updates["name"] = *opts.Name
}
if opts.TID != nil {
updates["tid"] = *opts.TID
}
if opts.Level != nil {
updates["level"] = *opts.Level
}
// 执行更新
if len(updates) > 0 {
if err := cfg.DB().Model(&access).Updates(updates).Error; err != nil {
return nil, err
}
}
// 返回更新后的记录
if err := cfg.DB().Where("id = ?", opts.ID).First(&access).Error; err != nil {
return nil, err
}
return &access, nil
}