提交 e1e3a31e 编写于 作者: S songzhibin97

feat:回滚删除对应api

上级 dac4e733
......@@ -72,15 +72,18 @@ func CreateTemp(c *gin.Context) {
response.FailWithMessage(err.Error(), c)
return
}
var apiIds []uint
if a.AutoCreateApiToSql {
if err := service.AutoCreateApi(&a); err != nil {
if ids, err := service.AutoCreateApi(&a); err != nil {
global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
c.Writer.Header().Add("success", "false")
c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
return
} else {
apiIds = ids
}
}
err := service.CreateTemp(a)
err := service.CreateTemp(a, apiIds...)
if err != nil {
if errors.Is(err, model.AutoMoveErr) {
c.Writer.Header().Add("success", "false")
......
......@@ -9,5 +9,6 @@ type SysAutoCodeHistory struct {
TableName string
AutoCodeMeta string `gorm:"type:text"` // 其他meta信息 path;path
InjectionMeta string `gorm:"type:text"` // 注入的内容 RouterPath@functionName@RouterString;
ApiIDs string // api表注册内容
Flag int // 表示对应状态 0 代表创建, 1 代表回滚 ...
}
......@@ -141,3 +141,7 @@ func DeleteApisByIds(ids request.IdsReq) (err error) {
err = global.GVA_DB.Delete(&[]model.SysApi{}, "id in ?", ids.Ids).Error
return err
}
func DeleteApiByIds(ids []string) (err error) {
return global.GVA_DB.Delete(model.SysApi{}, ids).Error
}
......@@ -10,6 +10,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"text/template"
......@@ -99,7 +100,7 @@ func PreviewTemp(autoCode model.AutoCodeStruct) (map[string]string, error) {
//@param: model.AutoCodeStruct
//@return: err error
func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
func CreateTemp(autoCode model.AutoCodeStruct, ids ...uint) (err error) {
dataList, fileList, needMkdir, err := getNeedList(&autoCode)
if err != nil {
return err
......@@ -160,27 +161,33 @@ func CreateTemp(autoCode model.AutoCodeStruct) (err error) {
bf.WriteString(";")
}
}
idBf := strings.Builder{}
for _, id := range ids {
idBf.WriteString(strconv.Itoa(int(id)))
idBf.WriteString(";")
}
if autoCode.TableName != "" {
err = CreateAutoCodeHistory(bf.String(),
injectionCodeMeta.String(),
autoCode.TableName,
idBf.String(),
)
} else {
err = CreateAutoCodeHistory(bf.String(),
injectionCodeMeta.String(),
autoCode.StructName,
idBf.String(),
)
}
if err != nil {
return err
}
if global.GVA_CONFIG.AutoCode.TransferRestart {
go func() {
_ = utils.Reload()
}()
}
//if global.GVA_CONFIG.AutoCode.TransferRestart {
// go func() {
// _ = utils.Reload()
// }()
//}
return errors.New("创建代码成功并移动文件成功")
} else { // 打包
if err := utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil {
......@@ -301,7 +308,7 @@ func addAutoMoveFile(data *tplData) {
//@param: a *model.AutoCodeStruct
//@return: err error
func AutoCreateApi(a *model.AutoCodeStruct) (err error) {
func AutoCreateApi(a *model.AutoCodeStruct) (ids []uint, err error) {
var apiList = []model.SysApi{
{
Path: "/" + a.Abbreviation + "/" + "create" + a.StructName,
......@@ -341,17 +348,20 @@ func AutoCreateApi(a *model.AutoCodeStruct) (err error) {
},
}
err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
for _, v := range apiList {
var api model.SysApi
if errors.Is(tx.Where("path = ? AND method = ?", v.Path, v.Method).First(&api).Error, gorm.ErrRecordNotFound) {
if err := tx.Create(&v).Error; err != nil { // 遇到错误时回滚事务
if err = tx.Create(&v).Error; err != nil { // 遇到错误时回滚事务
return err
} else {
ids = append(ids, v.ID)
}
}
}
return nil
})
return err
return ids, err
}
func getNeedList(autoCode *model.AutoCodeStruct) (dataList []tplData, fileList []string, needMkdir []string, err error) {
......
......@@ -11,11 +11,12 @@ import (
)
// CreateAutoCodeHistory RouterPath : RouterPath@RouterString;RouterPath2@RouterString2
func CreateAutoCodeHistory(autoCodeMeta string, injectionMeta string, tableName string) error {
func CreateAutoCodeHistory(autoCodeMeta string, injectionMeta string, tableName string, apiIds string) error {
return global.GVA_DB.Create(&model.SysAutoCodeHistory{
AutoCodeMeta: autoCodeMeta,
InjectionMeta: injectionMeta,
TableName: tableName,
ApiIDs: apiIds,
}).Error
}
......@@ -24,10 +25,15 @@ func RollBack(id uint) error {
if err := global.GVA_DB.First(&md, id).Error; err != nil {
return err
}
// 切分数据
// 清除API表
err := DeleteApiByIds(strings.Split(md.ApiIDs, ";"))
if err != nil {
global.GVA_LOG.Error("ClearTag DeleteApiByIds:", zap.Error(err))
}
// 获取全部表名
err, dbNames := GetTables(global.GVA_CONFIG.Mysql.Dbname)
if err != nil {
return err
global.GVA_LOG.Error("ClearTag GetTables:", zap.Error(err))
}
// 删除表
for _, name := range dbNames {
......@@ -40,7 +46,6 @@ func RollBack(id uint) error {
}
}
// 删除文件
for _, path := range strings.Split(md.AutoCodeMeta, ";") {
_ = utils.DeLFile(path)
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册