2021-12-06 12:24:22 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cuigh/auxo/net/web"
|
|
|
|
"github.com/cuigh/swirl/biz"
|
2021-12-23 11:28:31 +00:00
|
|
|
"github.com/cuigh/swirl/dao"
|
2022-01-06 08:54:14 +00:00
|
|
|
"github.com/cuigh/swirl/misc"
|
2021-12-06 12:24:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// RoleHandler encapsulates role related handlers.
|
|
|
|
type RoleHandler struct {
|
|
|
|
Find web.HandlerFunc `path:"/find" auth:"role.view" desc:"find role by id"`
|
|
|
|
Search web.HandlerFunc `path:"/search" auth:"role.view" desc:"search roles"`
|
|
|
|
Save web.HandlerFunc `path:"/save" method:"post" auth:"role.edit" desc:"create or update role"`
|
|
|
|
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"role.delete" desc:"delete role"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewRole creates an instance of RoleHandler
|
|
|
|
func NewRole(b biz.RoleBiz) *RoleHandler {
|
|
|
|
return &RoleHandler{
|
|
|
|
Search: roleSearch(b),
|
|
|
|
Find: roleFind(b),
|
|
|
|
Delete: roleDelete(b),
|
|
|
|
Save: roleSave(b),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func roleSearch(b biz.RoleBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
name := c.Query("name")
|
|
|
|
roles, err := b.Search(ctx, name)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, roles)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func roleFind(b biz.RoleBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
id := c.Query("id")
|
|
|
|
role, err := b.Find(ctx, id)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, role)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func roleDelete(b biz.RoleBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
args := &Args{}
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = b.Delete(ctx, args.ID, args.Name, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func roleSave(b biz.RoleBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
2021-12-23 11:28:31 +00:00
|
|
|
r := &dao.Role{}
|
2022-01-06 08:54:14 +00:00
|
|
|
err := c.Bind(r, true)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
2022-01-06 08:54:14 +00:00
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
if r.ID == "" {
|
2022-01-06 08:54:14 +00:00
|
|
|
err = b.Create(ctx, r, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
} else {
|
2022-01-06 08:54:14 +00:00
|
|
|
err = b.Update(ctx, r, c.User())
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return ajax(c, err)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|