2022-08-30 18:49:07 +08:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2022-10-17 16:32:31 +08:00
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/api/v1/helper"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/app/dto"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/constant"
|
|
|
|
"github.com/1Panel-dev/1Panel/backend/global"
|
2022-08-30 18:49:07 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (b *BaseApi) CreateCommand(c *gin.Context) {
|
2022-09-01 10:25:38 +08:00
|
|
|
var req dto.CommandOperate
|
2022-08-30 18:49:07 +08:00
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := commandService.Create(req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) SearchCommand(c *gin.Context) {
|
|
|
|
var req dto.SearchWithPage
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
total, list, err := commandService.SearchWithPage(req)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, dto.PageResult{
|
|
|
|
Items: list,
|
|
|
|
Total: total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) ListCommand(c *gin.Context) {
|
2022-09-01 10:25:38 +08:00
|
|
|
list, err := commandService.List()
|
2022-08-30 18:49:07 +08:00
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
helper.SuccessWithData(c, list)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) DeleteCommand(c *gin.Context) {
|
2022-09-01 10:25:38 +08:00
|
|
|
var req dto.BatchDeleteReq
|
2022-08-30 18:49:07 +08:00
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-01 10:25:38 +08:00
|
|
|
if err := commandService.Delete(req.Ids); err != nil {
|
2022-08-30 18:49:07 +08:00
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *BaseApi) UpdateCommand(c *gin.Context) {
|
2022-09-01 10:25:38 +08:00
|
|
|
var req dto.CommandOperate
|
2022-08-30 18:49:07 +08:00
|
|
|
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)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
id, err := helper.GetParamID(c)
|
|
|
|
if err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrBadRequest, constant.ErrTypeInvalidParams, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
upMap := make(map[string]interface{})
|
|
|
|
upMap["name"] = req.Name
|
2022-09-01 10:25:38 +08:00
|
|
|
upMap["command"] = req.Command
|
2022-08-30 18:49:07 +08:00
|
|
|
if err := commandService.Update(id, upMap); err != nil {
|
|
|
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
helper.SuccessWithData(c, nil)
|
|
|
|
}
|