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/user/role/get.go

72 lines
1.7 KiB
Go

//
// get.go
// Copyright (C) 2025 veypi <i@veypi.com>
// 2025-05-06 15:21
// Distributed under terms of the MIT license.
//
package role
import (
6 months ago
"github.com/veypi/OneAuth/cfg"
"github.com/veypi/OneAuth/models"
"github.com/veypi/OneBD/rest"
)
var _ = Router.Get("/:id", `
get user role
`, userRoleGet)
func userRoleGet(x *rest.X) (any, error) {
6 months ago
data := &models.UserRole{}
err := cfg.DB().Where("id = ?", x.Params.Get("id")).First(data).Error
return data, err
}
var _ = Router.Get("/", `
list user roles
`, userRoleList)
type listOpts struct {
UserID *string `json:"user_id" parse:"path"`
RoleID *string `json:"role_id" parse:"query"`
AppID *string `json:"app_id" parse:"query"`
Status *string `json:"status" parse:"query"`
}
func userRoleList(x *rest.X) (any, error) {
opts := &listOpts{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := make([]*struct {
6 months ago
models.UserRole
Username string `json:"username"`
Nickname string `json:"nickname"`
Icon string `json:"icon"`
RoleName string `json:"role_name"`
}, 0, 10)
// data := make([]*M.UserRole, 0, 10)
query := cfg.DB().Debug().Table("user_roles").Select("user_roles.*,users.username,users.nickname,users.icon,roles.name as role_name").
Joins("JOIN users ON users.id = user_roles.user_id").
Joins("JOIN roles ON roles.id = user_roles.role_id")
if opts.UserID != nil && *opts.UserID != "-" {
query = query.Where("user_id LIKE ?", opts.UserID)
}
if opts.RoleID != nil {
query = query.Where("role_id LIKE ?", opts.RoleID)
}
if opts.AppID != nil {
query = query.Where("app_id LIKE ?", opts.AppID)
}
if opts.Status != nil {
query = query.Where("status LIKE ?", opts.Status)
}
err = query.Scan(&data).Error
return data, err
}