Files
go-gin-api/internal/api/controller/menu_handler/func_createaction.go
T
新亮 62367a041a feature(参数验证): 优化 gin 参数验证的错误信息
- 新增 internal/pkg/validation 包,详情:https://github.com/xinliangnote/go-gin-api/issues/44
- 调整 code 包位置,移动至 internal/pkg/code 目录。

Closes #44
2021-07-10 22:38:30 +08:00

90 lines
2.3 KiB
Go
Executable File

package menu_handler
import (
"net/http"
"github.com/xinliangnote/go-gin-api/internal/api/service/menu_service"
"github.com/xinliangnote/go-gin-api/internal/pkg/code"
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
"github.com/xinliangnote/go-gin-api/pkg/errno"
)
type createActionRequest struct {
Id string `form:"id"` // HashID
Method string `form:"method"` // 请求方法
API string `form:"api"` // 请求地址
}
type createActionResponse struct {
Id int32 `json:"id"` // 主键ID
}
// CreateAction 创建功能权限
// @Summary 创建功能权限
// @Description 创建功能权限
// @Tags API.menu
// @Accept multipart/form-data
// @Produce json
// @Param id formData string true "HashID"
// @Param method formData string true "请求方法"
// @Param api formData string true "请求地址"
// @Success 200 {object} createActionResponse
// @Failure 400 {object} code.Failure
// @Router /api/menu_action [post]
func (h *handler) CreateAction() core.HandlerFunc {
return func(c core.Context) {
req := new(createActionRequest)
res := new(createActionResponse)
if err := c.ShouldBindForm(req); err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.ParamBindError,
code.Text(code.ParamBindError)).WithErr(err),
)
return
}
ids, err := h.hashids.HashidsDecode(req.Id)
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.HashIdsDecodeError,
code.Text(code.HashIdsDecodeError)).WithErr(err),
)
return
}
id := int32(ids[0])
searchOneData := new(menu_service.SearchOneData)
searchOneData.Id = id
menuInfo, err := h.menuService.Detail(c, searchOneData)
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.MenuDetailError,
code.Text(code.MenuDetailError)).WithErr(err),
)
return
}
createActionData := new(menu_service.CreateMenuActionData)
createActionData.MenuId = menuInfo.Id
createActionData.Method = req.Method
createActionData.API = req.API
createId, err := h.menuService.CreateAction(c, createActionData)
if err != nil {
c.AbortWithError(errno.NewError(
http.StatusBadRequest,
code.MenuCreateActionError,
code.Text(code.MenuCreateActionError)).WithErr(err),
)
return
}
res.Id = createId
c.Payload(res)
}
}