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/oa/api/app/role.go

97 lines
1.8 KiB
Go

11 months ago
package app
import (
"github.com/veypi/OneBD/rest"
"oa/cfg"
M "oa/models"
)
func useRole(r rest.Router) {
11 months ago
r.Delete("/:role_id", roleDelete)
r.Get("/", roleList)
r.Post("/", rolePost)
11 months ago
r.Get("/:role_id", roleGet)
r.Patch("/:role_id", rolePatch)
}
11 months ago
func roleDelete(x *rest.X) (any, error) {
opts := &M.RoleDelete{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Role{}
11 months ago
err = cfg.DB().Where("id = ?", opts.ID).Delete(data).Error
return data, err
}
func roleList(x *rest.X) (any, error) {
opts := &M.RoleList{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := make([]*M.Role, 0, 10)
query := cfg.DB()
11 months ago
if opts.AppID != nil {
query = query.Where("app_id LIKE ?", opts.AppID)
}
if opts.Name != nil {
query = query.Where("name LIKE ?", opts.Name)
}
err = query.Find(&data).Error
return data, err
}
11 months ago
func rolePost(x *rest.X) (any, error) {
opts := &M.RolePost{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Role{}
11 months ago
data.AppID = opts.AppID
data.Name = opts.Name
data.Des = opts.Des
err = cfg.DB().Create(data).Error
return data, err
}
11 months ago
func roleGet(x *rest.X) (any, error) {
opts := &M.RoleGet{}
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Role{}
11 months ago
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
return data, err
}
11 months ago
func rolePatch(x *rest.X) (any, error) {
opts := &M.RolePatch{}
1 year ago
err := x.Parse(opts)
if err != nil {
return nil, err
}
data := &M.Role{}
11 months ago
err = cfg.DB().Where("id = ?", opts.ID).First(data).Error
if err != nil {
return nil, err
}
optsMap := make(map[string]interface{})
if opts.Name != nil {
optsMap["name"] = opts.Name
}
if opts.Des != nil {
optsMap["des"] = opts.Des
}
err = cfg.DB().Model(data).Updates(optsMap).Error
1 year ago
return data, err
}