mirror of https://github.com/1Panel-dev/1Panel
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
99 lines
2.9 KiB
99 lines
2.9 KiB
2 years ago
|
package v1
|
||
|
|
||
|
import (
|
||
|
"github.com/1Panel-dev/1Panel/app/api/v1/helper"
|
||
|
"github.com/1Panel-dev/1Panel/app/dto"
|
||
|
"github.com/1Panel-dev/1Panel/constant"
|
||
|
"github.com/1Panel-dev/1Panel/global"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
func (b *BaseApi) CreateCommand(c *gin.Context) {
|
||
|
var req dto.CommandCreate
|
||
|
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) {
|
||
|
list, err := commandService.Search()
|
||
|
if err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
helper.SuccessWithData(c, list)
|
||
|
}
|
||
|
|
||
|
func (b *BaseApi) DeleteCommand(c *gin.Context) {
|
||
|
var req dto.DeleteByName
|
||
|
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.Delete(req.Name); err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||
|
return
|
||
|
}
|
||
|
helper.SuccessWithData(c, nil)
|
||
|
}
|
||
|
|
||
|
func (b *BaseApi) UpdateCommand(c *gin.Context) {
|
||
|
var req dto.CommandUpdate
|
||
|
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
|
||
|
if err := commandService.Update(id, upMap); err != nil {
|
||
|
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
|
||
|
return
|
||
|
}
|
||
|
helper.SuccessWithData(c, nil)
|
||
|
}
|