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.
63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
//
|
|
// patch.go
|
|
// Copyright (C) 2024 veypi <i@veypi.com>
|
|
// Distributed under terms of the MIT license.
|
|
//
|
|
|
|
package access
|
|
|
|
import (
|
|
"github.com/veypi/OneBD/rest"
|
|
"oa/cfg"
|
|
"oa/models"
|
|
)
|
|
|
|
type updateOpts struct {
|
|
ID string `parse:"path"`
|
|
Name *string `json:"name"`
|
|
TID *string `json:"tid"`
|
|
Level *uint `json:"level"`
|
|
}
|
|
|
|
var _ = Router.Patch("/:id", updateAccess)
|
|
|
|
func updateAccess(x *rest.X) (any, error) {
|
|
// 解析请求参数
|
|
opts := &updateOpts{}
|
|
if err := x.Parse(opts); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 查找记录
|
|
var access models.Access
|
|
if err := cfg.DB().Where("id = ?", opts.ID).First(&access).Error; err != nil {
|
|
return nil, rest.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
|
|
}
|