添加根据shell的id删除

This commit is contained in:
junleea 2025-06-06 14:16:44 +08:00
parent ab2f5fd1b7
commit cf404a97b9
3 changed files with 64 additions and 0 deletions

View File

@ -98,3 +98,19 @@ func FindShellWillRunByServer(server string, uid uint) []Shell {
}
return shells
}
func DeleteShellByID(id, authId uint) bool {
var db2 *gorm.DB
if proto.Config.SERVER_SQL_LOG {
db2 = DB
} else {
db2 = DB.Debug()
}
result := db2.Where("id = ? and auth_id = ?", id, authId).Delete(&Shell{})
if result.Error != nil {
log.Printf("DeleteShellByID failed: %v", result.Error)
return false
}
return true
}

View File

@ -2,6 +2,7 @@ package handler
import (
"github.com/gin-gonic/gin"
"net/http"
"videoplayer/proto"
"videoplayer/service"
)
@ -32,12 +33,20 @@ type UpdateShellResp struct {
Status int `json:"status" form:"status"`
}
type DeleteShellRequestID struct {
ID uint `json:"id" form:"id" binding:"required"`
}
type DeleteShellRequest struct {
Shells []DeleteShellRequestID `json:"shells" form:"shells" binding:"required"`
}
func SetUpShellGroup(router *gin.Engine) {
shellGroup := router.Group("/shell") //持续集成、部署
shellHandler := ShellHandler{}
shellGroup.POST("/create", shellHandler.CreateShell)
shellGroup.POST("/list", shellHandler.ListShell)
shellGroup.POST("/update", shellHandler.UpdateShell)
shellGroup.POST("/delete", shellHandler.DeleteShell)
shellGroup.POST("/server_will_run_list", shellHandler.ServerWillRun)
}
@ -84,6 +93,41 @@ func (s *ShellHandler) UpdateShell(c *gin.Context) {
}
}
type DeleteShellResp struct {
Success []DeleteShellRequestID `json:"success" form:"success"`
Error []DeleteShellRequestID `json:"error" form:"error"`
}
func (s *ShellHandler) DeleteShell(c *gin.Context) {
userId, _ := c.Get("id")
id := int(userId.(float64))
var req DeleteShellRequest
var resp proto.GeneralResp
if err := c.ShouldBind(&req); err != nil {
var delSuccessIDs []DeleteShellRequestID
var delErrorIDs []DeleteShellRequestID
for _, v := range req.Shells {
delSuccess := service.DeleteShellByID(v.ID, uint(id))
if delSuccess {
delSuccessIDs = append(delSuccessIDs, v)
} else {
delErrorIDs = append(delErrorIDs, v)
}
}
var delResp DeleteShellResp
delResp.Success = delSuccessIDs
delResp.Error = delErrorIDs
resp.Code = proto.SuccessCode
resp.Data = delResp
resp.Message = "success"
} else {
resp.Code = proto.ParameterError
resp.Message = "参数解析错误:" + err.Error()
}
c.JSON(http.StatusOK, resp)
}
type ServerWillRunReq struct {
Server string `json:"server"`
}

View File

@ -20,6 +20,10 @@ func FindShellByAuthID(id int) []dao.Shell {
return dao.FindShellByAuthID(id)
}
func DeleteShellByID(id, authId uint) bool {
return dao.DeleteShellByID(id, authId)
}
func UpdateShellByID(id, authId uint, shellName, shellContent, server string, status int, shellResult string) bool {
return dao.UpdateShellByID(id, authId, shellName, shellContent, status, shellResult)
}