diff --git a/dao/shell.go b/dao/shell.go index 8d1c5bd..f83722a 100644 --- a/dao/shell.go +++ b/dao/shell.go @@ -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 +} diff --git a/handler/shell.go b/handler/shell.go index 3f39d77..af01042 100644 --- a/handler/shell.go +++ b/handler/shell.go @@ -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"` } diff --git a/service/shellService.go b/service/shellService.go index 2a00982..ee38023 100644 --- a/service/shellService.go +++ b/service/shellService.go @@ -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) }