From e60ee9799ff1cd43107fd75e542241dcdc87bf63 Mon Sep 17 00:00:00 2001 From: zhanghan Date: Fri, 12 Sep 2025 16:32:07 +0800 Subject: [PATCH] add func for update script --- .../module/script_library/controller/script.go | 14 ++++++++++++++ .../internal/module/script_library/dao/script.go | 9 +++++++++ .../internal/module/script_library/model/script.go | 2 +- .../internal/module/script_library/router.go | 1 + .../module/script_library/service/script.go | 11 +++++++++++ 5 files changed, 36 insertions(+), 1 deletion(-) diff --git a/automation/server/internal/module/script_library/controller/script.go b/automation/server/internal/module/script_library/controller/script.go index 4afe5177..ff48493f 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 0c0b690e..0d9a1574 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 9a212765..871eec7a 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 bcbb9d75..929e14c3 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 cdfddb33..7da0cc50 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) +} -- Gitee