swirl/api/image.go

80 lines
1.8 KiB
Go
Raw Normal View History

2021-12-06 12:24:22 +00:00
package api
import (
"github.com/cuigh/auxo/data"
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/biz"
)
// ImageHandler encapsulates image related handlers.
type ImageHandler struct {
2021-12-20 06:28:43 +00:00
Search web.HandlerFunc `path:"/search" auth:"image.view" desc:"search images"`
Find web.HandlerFunc `path:"/find" auth:"image.view" desc:"find image by id"`
Delete web.HandlerFunc `path:"/delete" method:"post" auth:"image.delete" desc:"delete image"`
2021-12-06 12:24:22 +00:00
}
// NewImage creates an instance of ImageHandler
func NewImage(b biz.ImageBiz) *ImageHandler {
return &ImageHandler{
2021-12-20 06:28:43 +00:00
Search: imageSearch(b),
Find: imageFind(b),
Delete: imageDelete(b),
2021-12-06 12:24:22 +00:00
}
}
func imageSearch(b biz.ImageBiz) web.HandlerFunc {
type Args struct {
2021-12-20 06:28:43 +00:00
Node string `json:"node" bind:"node"`
2021-12-06 12:24:22 +00:00
Name string `json:"name" bind:"name"`
PageIndex int `json:"pageIndex" bind:"pageIndex"`
PageSize int `json:"pageSize" bind:"pageSize"`
}
return func(ctx web.Context) (err error) {
var (
args = &Args{}
images []*biz.Image
total int
)
if err = ctx.Bind(args); err == nil {
2021-12-20 06:28:43 +00:00
images, total, err = b.Search(args.Node, args.Name, args.PageIndex, args.PageSize)
2021-12-06 12:24:22 +00:00
}
if err != nil {
return
}
return success(ctx, data.Map{
"items": images,
"total": total,
})
}
}
func imageFind(b biz.ImageBiz) web.HandlerFunc {
return func(ctx web.Context) error {
2021-12-20 06:28:43 +00:00
node := ctx.Query("node")
2021-12-06 12:24:22 +00:00
id := ctx.Query("id")
2021-12-20 06:28:43 +00:00
image, raw, err := b.Find(node, id)
2021-12-06 12:24:22 +00:00
if err != nil {
return err
}
return success(ctx, data.Map{"image": image, "raw": raw})
}
}
func imageDelete(b biz.ImageBiz) web.HandlerFunc {
type Args struct {
2021-12-20 06:28:43 +00:00
Node string `json:"node"`
2021-12-06 12:24:22 +00:00
ID string `json:"id"`
}
return func(ctx web.Context) (err error) {
args := &Args{}
if err = ctx.Bind(args); err == nil {
2021-12-20 06:28:43 +00:00
err = b.Delete(args.Node, args.ID, ctx.User())
2021-12-06 12:24:22 +00:00
}
return ajax(ctx, err)
}
}