swirl/api/registry.go

89 lines
2.1 KiB
Go
Raw Normal View History

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
)
// RegistryHandler encapsulates registry related handlers.
type RegistryHandler struct {
Search web.HandlerFunc `path:"/search" auth:"registry.view" desc:"search registries"`
Find web.HandlerFunc `path:"/find" auth:"registry.view" desc:"find registry by id"`
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"registry.delete" desc:"delete registry"`
Save web.HandlerFunc `path:"/save" method:"post" auth:"registry.edit" desc:"create or update registry"`
}
// NewRegistry creates an instance of RegistryHandler
func NewRegistry(b biz.RegistryBiz) *RegistryHandler {
return &RegistryHandler{
Search: registrySearch(b),
Find: registryFind(b),
Delete: registryDelete(b),
Save: registrySave(b),
}
}
func registrySearch(b biz.RegistryBiz) web.HandlerFunc {
2022-01-06 08:54:14 +00:00
return func(c web.Context) error {
ctx, cancel := misc.Context(defaultTimeout)
defer cancel()
registries, err := b.Search(ctx)
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
2022-01-06 08:54:14 +00:00
return success(c, registries)
2021-12-06 12:24:22 +00:00
}
}
func registryFind(b biz.RegistryBiz) 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")
node, 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, node)
2021-12-06 12:24:22 +00:00
}
}
func registryDelete(b biz.RegistryBiz) 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 registrySave(b biz.RegistryBiz) 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.Registry{}
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
}
}