From 7f7588281191ea135fdaa712b03d478d4fd0dba8 Mon Sep 17 00:00:00 2001 From: zhengkunwang <31820853+zhengkunwang223@users.noreply.github.com> Date: Tue, 10 Oct 2023 05:36:29 -0500 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=8E=E7=AB=AF=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E5=8F=82=E6=95=B0=E6=A0=A1=E9=AA=8C=E6=96=B9?= =?UTF-8?q?=E6=B3=95=20(#2490)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/app/api/v1/helper/helper.go | 31 +++++++++++------------------ backend/app/api/v1/runtime.go | 29 ++++++++------------------- 2 files changed, 20 insertions(+), 40 deletions(-) diff --git a/backend/app/api/v1/helper/helper.go b/backend/app/api/v1/helper/helper.go index 831d516e6..e76fddae5 100644 --- a/backend/app/api/v1/helper/helper.go +++ b/backend/app/api/v1/helper/helper.go @@ -17,25 +17,6 @@ import ( "github.com/pkg/errors" ) -func GeneratePaginationFromReq(c *gin.Context) (*dto.PageInfo, bool) { - p, ok1 := c.GetQuery("page") - ps, ok2 := c.GetQuery("pageSize") - if !(ok1 && ok2) { - return nil, false - } - - page, err := strconv.Atoi(p) - if err != nil { - return nil, false - } - pageSize, err := strconv.Atoi(ps) - if err != nil { - return nil, false - } - - return &dto.PageInfo{Page: page, PageSize: pageSize}, true -} - func ErrorWithDetail(ctx *gin.Context, code int, msgKey string, err error) { res := dto.Response{ Code: code, @@ -132,3 +113,15 @@ func GetTxAndContext() (tx *gorm.DB, ctx context.Context) { ctx = context.WithValue(context.Background(), constant.DB, tx) return } + +func CheckBindAndValidate(req interface{}, c *gin.Context) error { + if err := c.ShouldBindJSON(&req); err != nil { + ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return err + } + if err := global.VALID.Struct(req); err != nil { + ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + return err + } + return nil +} diff --git a/backend/app/api/v1/runtime.go b/backend/app/api/v1/runtime.go index 3bafdf5af..8b4887e0c 100644 --- a/backend/app/api/v1/runtime.go +++ b/backend/app/api/v1/runtime.go @@ -5,7 +5,6 @@ import ( "github.com/1Panel-dev/1Panel/backend/app/dto" "github.com/1Panel-dev/1Panel/backend/app/dto/request" "github.com/1Panel-dev/1Panel/backend/constant" - "github.com/1Panel-dev/1Panel/backend/global" "github.com/gin-gonic/gin" ) @@ -19,8 +18,7 @@ import ( // @Router /runtimes/search [post] func (b *BaseApi) SearchRuntimes(c *gin.Context) { var req request.RuntimeSearch - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } total, items, err := runtimeService.Page(req) @@ -45,8 +43,7 @@ func (b *BaseApi) SearchRuntimes(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建运行环境 [name]","formatEN":"Create runtime [name]"} func (b *BaseApi) CreateRuntime(c *gin.Context) { var req request.RuntimeCreate - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } if err := runtimeService.Create(req); err != nil { @@ -67,8 +64,7 @@ func (b *BaseApi) CreateRuntime(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"删除网站 [name]","formatEN":"Delete website [name]"} func (b *BaseApi) DeleteRuntime(c *gin.Context) { var req request.RuntimeDelete - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } err := runtimeService.Delete(req) @@ -90,8 +86,7 @@ func (b *BaseApi) DeleteRuntime(c *gin.Context) { // @x-panel-log {"bodyKeys":["name"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新运行环境 [name]","formatEN":"Update runtime [name]"} func (b *BaseApi) UpdateRuntime(c *gin.Context) { var req request.RuntimeUpdate - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } if err := runtimeService.Update(req); err != nil { @@ -133,8 +128,7 @@ func (b *BaseApi) GetRuntime(c *gin.Context) { // @Router /runtimes/node/package [post] func (b *BaseApi) GetNodePackageRunScript(c *gin.Context) { var req request.NodePackageReq - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } res, err := runtimeService.GetNodePackageRunScript(req) @@ -156,8 +150,7 @@ func (b *BaseApi) GetNodePackageRunScript(c *gin.Context) { // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"操作运行环境 [name]","formatEN":"Operate runtime [name]"} func (b *BaseApi) OperateRuntime(c *gin.Context) { var req request.RuntimeOperate - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } err := runtimeService.OperateRuntime(req) @@ -178,8 +171,7 @@ func (b *BaseApi) OperateRuntime(c *gin.Context) { // @Router /runtimes/node/modules [post] func (b *BaseApi) GetNodeModules(c *gin.Context) { var req request.NodeModuleReq - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } res, err := runtimeService.GetNodeModules(req) @@ -200,12 +192,7 @@ func (b *BaseApi) GetNodeModules(c *gin.Context) { // @Router /runtimes/node/modules/operate [post] func (b *BaseApi) OperateNodeModules(c *gin.Context) { var req request.NodeModuleReq - if err := c.ShouldBindJSON(&req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) - return - } - if err := global.VALID.Struct(req); err != nil { - helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err) + if err := helper.CheckBindAndValidate(&req, c); err != nil { return } err := runtimeService.OperateNodeModules(req)