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

71 lines
1.4 KiB
Go

6 months ago
//
// list.go
// Copyright (C) 2024 veypi <i@veypi.com>
// Distributed under terms of the MIT license.
//
package access
import (
6 months ago
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
3 months ago
"github.com/vyes-ai/vigo"
6 months ago
)
type listOpts struct {
Page int `parse:"query" default:"1"`
PageSize int `parse:"query" default:"20"`
AppID string `parse:"query"`
UserID string `parse:"query"`
RoleID string `parse:"query"`
Name string `parse:"query"`
}
type listResponse struct {
Total int64 `json:"total"`
Items []models.Access `json:"items"`
}
var _ = Router.Get("/", listAccess)
func listAccess(x *vigo.X) (any, error) {
6 months ago
// 解析查询参数
opts := &listOpts{}
if err := x.Parse(opts); err != nil {
return nil, err
}
// 构建查询
db := cfg.DB().Model(&models.Access{})
if opts.AppID != "" {
db = db.Where("app_id = ?", opts.AppID)
}
if opts.UserID != "" {
db = db.Where("user_id = ?", opts.UserID)
}
if opts.RoleID != "" {
db = db.Where("role_id = ?", opts.RoleID)
}
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 items []models.Access
if err := db.Offset(offset).Limit(opts.PageSize).Find(&items).Error; err != nil {
return nil, err
}
return &listResponse{
Total: total,
Items: items,
}, nil
}