diff --git a/automation/server/internal/module/script_library/controller/script.go b/automation/server/internal/module/script_library/controller/script.go index 4afe517785a5fb947d7250db52eb0cf44ea5a964..ff48493f10219967193793844720da1c28c0578a 100644 --- a/automation/server/internal/module/script_library/controller/script.go +++ b/automation/server/internal/module/script_library/controller/script.go @@ -34,3 +34,17 @@ func ScriptListHandler(c *gin.Context) { } response.DataPagination(c, scripts, total, query) } + +func UpdateScriptHandler(c *gin.Context) { + var script model.Script + if err := c.ShouldBindJSON(&script); err != nil { + response.Fail(c, nil, err.Error()) + return + } + + if err := service.UpdateScript(&script); err != nil { + response.Fail(c, nil, err.Error()) + return + } + response.Success(c, nil, "success") +} diff --git a/automation/server/internal/module/script_library/dao/script.go b/automation/server/internal/module/script_library/dao/script.go index 0c0b690ebefdceefc2a49e24c4bb22608fe23a2a..0d9a1574e7114fc47b571ab8bad44fd1530ca223 100644 --- a/automation/server/internal/module/script_library/dao/script.go +++ b/automation/server/internal/module/script_library/dao/script.go @@ -67,3 +67,12 @@ func GetScripts(query *response.PaginationQ) ([]*model.ScriptResponse, int, erro return scriptResponses, int(total), nil } + +func UpdateScript(id string, s *model.Script) error { + return global.App.MySQL.Transaction(func(tx *gorm.DB) error { + if err := tx.Model(&model.Script{}).Where("id = ?", id).Updates(s).Error; err != nil { + return err + } + return nil + }) +} diff --git a/automation/server/internal/module/script_library/model/script.go b/automation/server/internal/module/script_library/model/script.go index 9a212765815d810e36e38d83fae063e56bd3629c..871eec7a8a2c06b6d361966cc5144e63c08a600d 100644 --- a/automation/server/internal/module/script_library/model/script.go +++ b/automation/server/internal/module/script_library/model/script.go @@ -2,7 +2,7 @@ package model type Script struct { ID string `json:"id" gorm:"primaryKey;type:varchar(36);not null;comment:'脚本ID'"` - Name string `json:"name" gorm:"type:varchar(255);not null;unique;comment:'脚本名称'"` + Name string `json:"name" gorm:"type:varchar(255);not null;uniqueIndex:uniq_script_name;comment:'脚本名称'"` ScriptType string `json:"script_type" gorm:"type:varchar(100);not null;comment:'脚本类型'"` Description string `json:"description" gorm:"type:varchar(500);comment:'脚本描述'"` Tags string `json:"tags" gorm:"comment:'场景标签'"` diff --git a/automation/server/internal/module/script_library/router.go b/automation/server/internal/module/script_library/router.go index bcbb9d7532ed9e43edaad5bf8f5d7fc68c1540e8..929e14c32fd4887c6e8241e760cb8f0b6ee02c8c 100644 --- a/automation/server/internal/module/script_library/router.go +++ b/automation/server/internal/module/script_library/router.go @@ -10,6 +10,7 @@ func ScriptLibraryHandler(router *gin.RouterGroup) { { api.POST("/add", controller.AddScriptHandler) api.GET("/list", controller.ScriptListHandler) + api.PUT("/update", controller.UpdateScriptHandler) versionGroup := api.Group("/:script_id/scriptVersion") { diff --git a/automation/server/internal/module/script_library/service/script.go b/automation/server/internal/module/script_library/service/script.go index cdfddb33a89f997fe21ca4add6edcef10529b58e..7da0cc500d5509311e12b71aff8970c68ec06309 100644 --- a/automation/server/internal/module/script_library/service/script.go +++ b/automation/server/internal/module/script_library/service/script.go @@ -52,3 +52,14 @@ func AddScript(s *model.ScriptWithVersion) error { func GetScripts(query *response.PaginationQ) ([]*model.ScriptResponse, int, error) { return dao.GetScripts(query) } + +func UpdateScript(s *model.Script) error { + script := &model.Script{ + Name: s.Name, + Description: s.Description, + Tags: s.Tags, + LastModifyUser: s.LastModifyUser, + LastModifyUpdatedAt: time.Now().Format("2006-01-02 15:04:05"), + } + return dao.UpdateScript(s.ID, script) +}