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/list.go

57 lines
1.2 KiB
Go

package resource
import (
"github.com/vyes-ai/vigo"
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
)
type listOpts struct {
Page int `parse:"query" default:"1"`
PageSize int `parse:"query" default:"20"`
AppID string `parse:"query"`
Name string `parse:"query"`
}
type listResponse struct {
Total int64 `json:"total"`
Items []*models.Resource `json:"items"`
}
var _ = Router.Get("/", listResources)
func listResources(x *vigo.X) (any, error) {
// 解析参数
opts := &listOpts{}
if err := x.Parse(opts); err != nil {
return nil, err
}
// 构建查询
db := cfg.DB().Model(&models.Resource{})
if opts.AppID != "" {
db = db.Where("app_id = ?", opts.AppID)
}
if opts.Name != "" {
db = db.Where("name LIKE ?", "%"+opts.Name+"%")
}
// 计算总数
var total int64
if err := db.Count(&total).Error; err != nil {
return nil, err
}
// 分页查询
offset := (opts.Page - 1) * opts.PageSize
var resources []*models.Resource
if err := db.Offset(offset).Limit(opts.PageSize).Find(&resources).Error; err != nil {
return nil, err
}
return &listResponse{
Total: total,
Items: resources,
}, nil
}