diff --git a/construct/init.go b/construct/init.go index 7bd09a6..58a1c0a 100644 --- a/construct/init.go +++ b/construct/init.go @@ -143,6 +143,19 @@ func initRouter(router *gin.Engine) { } response.ManagerAPISuccess(ctx, map[string]interface{}{}) }) + // 删除配置 + router.POST("/config/delete", func(ctx *gin.Context) { + deleteForm, err := manager.Config.ValidateDeleteConfigForm(ctx) + if nil != err { + response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()}) + return + } + if err := manager.Config.Delete(deleteForm); nil != err { + response.ManagerAPIFail(ctx, -1, map[string]interface{}{"reason": err.Error()}) + return + } + response.ManagerAPISuccess(ctx, map[string]interface{}{}) + }) // 读取配置(从内存读取) router.GET("/config/read", func(ctx *gin.Context) { diff --git a/dao/base.go b/dao/base.go index 77ccf2f..4ec046a 100644 --- a/dao/base.go +++ b/dao/base.go @@ -97,6 +97,15 @@ func (b *base) Detail(dbInstance *gorm.DB, condition interface{}, result interfa return dbInstance.First(result, condition).Error } +// Delete 删除数据 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:24 下午 2021/12/1 +func (b *base) Delete(dbInstance *gorm.DB, table string, primaryID int64, model interface{}) error { + return dbInstance.Table(table).Where("id = ?", primaryID).Delete(model).Error +} + // Count 查询数量 // // Author : go_developer@163.com<白茶清欢> diff --git a/define/form/config.go b/define/form/config.go index c9af870..ef8eace 100644 --- a/define/form/config.go +++ b/define/form/config.go @@ -20,3 +20,16 @@ type CreateOrUpdateConfig struct { OperateUserID string `json:"operate_user_id"` // 操作人ID Description string `json:"description"` // 配置描述 } + +// DeleteConfig 删除配置 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:13 下午 2021/12/1 +type DeleteConfig struct { + NamespaceID int64 `json:"namespace_id"` // 命名空间ID + Namespace string `json:"namespace"` // 命名空间 + Key string `json:"key"` // key + OperateUserID string `json:"operate_user_id"` // 操作人ID + Description string `json:"description"` // 操作描述 +} diff --git a/manager/config.go b/manager/config.go index 98ad3ee..c8781e9 100644 --- a/manager/config.go +++ b/manager/config.go @@ -6,6 +6,7 @@ package manager import ( + "encoding/json" "errors" "fmt" "time" @@ -143,6 +144,30 @@ func (c *config) Update(dataForm *form.CreateOrUpdateConfig, namespaceInfo *mode return nil } +// Delete 删除配置 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 12:16 下午 2021/12/1 +func (c *config) Delete(dataForm *form.DeleteConfig) error { + var ( + cfgInfo *model.Config + err error + ) + // 读取key的配置 + if cfgInfo, err = c.GetConfigInfo(dataForm.NamespaceID, dataForm.Key); nil != err { + return errors.New("要删除的key查询失败 : " + err.Error()) + } + if err = dao.Config.Delete(dao.DBInstance, "config", cfgInfo.ID, model.Config{}); nil != err { + return err + } + byteData, _ := json.Marshal(cfgInfo) + logData := c.GetLogData(cfgInfo.NamespaceID, cfgInfo.ID, dataForm.OperateUserID, model.LogTypeDeleteConfig, "删除配置-"+dataForm.Description) + logData.BeforeValue = string(byteData) + _ = dao.Log.CreateNewData(dao.DBInstance, logData, &dao.Option{DBTable: "log"}) + return nil +} + // ValidateCreateOrUpdateConfigForm ... // // Author : go_developer@163.com<白茶清欢> @@ -173,6 +198,33 @@ func (c *config) ValidateCreateOrUpdateConfigForm(ctx *gin.Context) (*form.Creat return &dataForm, nil } +// ValidateDeleteConfigForm 删除配置表单 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2:12 下午 2021/12/1 +func (c *config) ValidateDeleteConfigForm(ctx *gin.Context) (*form.DeleteConfig, error) { + var ( + dataForm form.DeleteConfig + err error + ) + + if err = c.ParsePostForm(ctx, &dataForm); nil != err { + return nil, err + } + + if dataForm.NamespaceID == 0 { + dataForm.NamespaceID = Namespace.GetNamespaceIDByNamespace(dataForm.Namespace) + } + if dataForm.NamespaceID == 0 { + return nil, errors.New("namespace or namespace_id must exist") + } + if len(dataForm.Key) == 0 { + return nil, errors.New("delete is not allowed empty") + } + return &dataForm, nil +} + // GetConfigInfo 读取配置 // // Author : go_developer@163.com<白茶清欢>