Browse Source

更新自动化代码api层代码混入service层代码

SliverHorn 4 years ago
parent
commit
70b2ee3f97
3 changed files with 68 additions and 67 deletions
  1. 5 45
      server/api/v1/sys_auto_code.go
  2. 0 22
      server/service/sys_api.go
  3. 63 0
      server/service/sys_auto_code.go

+ 5 - 45
server/api/v1/sys_auto_code.go

@@ -30,51 +30,11 @@ func CreateTemp(c *gin.Context) {
 		return
 	}
 	if a.AutoCreateApiToSql {
-		apiList := [6]model.SysApi{
-			{
-				Path:        "/" + a.Abbreviation + "/" + "create" + a.StructName,
-				Description: "新增" + a.Description,
-				ApiGroup:    a.Abbreviation,
-				Method:      "POST",
-			},
-			{
-				Path:        "/" + a.Abbreviation + "/" + "delete" + a.StructName,
-				Description: "删除" + a.Description,
-				ApiGroup:    a.Abbreviation,
-				Method:      "DELETE",
-			},
-			{
-				Path:        "/" + a.Abbreviation + "/" + "delete" + a.StructName + "ByIds",
-				Description: "批量删除" + a.Description,
-				ApiGroup:    a.Abbreviation,
-				Method:      "DELETE",
-			},
-			{
-				Path:        "/" + a.Abbreviation + "/" + "update" + a.StructName,
-				Description: "更新" + a.Description,
-				ApiGroup:    a.Abbreviation,
-				Method:      "PUT",
-			},
-			{
-				Path:        "/" + a.Abbreviation + "/" + "find" + a.StructName,
-				Description: "根据ID获取" + a.Description,
-				ApiGroup:    a.Abbreviation,
-				Method:      "GET",
-			},
-			{
-				Path:        "/" + a.Abbreviation + "/" + "get" + a.StructName + "List",
-				Description: "获取" + a.Description + "列表",
-				ApiGroup:    a.Abbreviation,
-				Method:      "GET",
-			},
-		}
-		for _, v := range apiList {
-			if err := service.AutoCreateApi(v); err != nil {
-				global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
-				c.Writer.Header().Add("success", "false")
-				c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
-				return
-			}
+		if 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
 		}
 	}
 	err := service.CreateTemp(a)

+ 0 - 22
server/service/sys_api.go

@@ -33,28 +33,6 @@ func DeleteApi(api model.SysApi) (err error) {
 	return err
 }
 
-//@author: [piexlmax](https://github.com/piexlmax)
-//@function: CreateApi
-//@description: 自动创建api数据,
-//@param: api model.SysApi
-//@return: err error
-
-func AutoCreateApi(api model.SysApi) (err error) {
-	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
-		var fApi model.SysApi
-		var txErr error
-		fxErr := tx.Where("path = ? AND method = ?", api.Path, api.Method).First(&fApi).Error
-		if errors.Is(fxErr, gorm.ErrRecordNotFound) {
-			txErr = tx.Create(&api).Error
-			if txErr != nil {
-				return txErr
-			}
-		}
-		return nil
-	})
-	return err
-}
-
 //@author: [piexlmax](https://github.com/piexlmax)
 //@function: GetAPIInfoList
 //@description: 分页获取数据,

+ 63 - 0
server/service/sys_auto_code.go

@@ -7,6 +7,7 @@ import (
 	"gin-vue-admin/model"
 	"gin-vue-admin/model/request"
 	"gin-vue-admin/utils"
+	"gorm.io/gorm"
 	"io/ioutil"
 	"os"
 	"path/filepath"
@@ -207,3 +208,65 @@ func addAutoMoveFile(data *tplData) {
 		}
 	}
 }
+
+
+//@author: [piexlmax](https://github.com/piexlmax)
+//@author: [SliverHorn](https://github.com/SliverHorn)
+//@function: CreateApi
+//@description: 自动创建api数据,
+//@param: a *model.AutoCodeStruct
+//@return: error
+
+func AutoCreateApi(a *model.AutoCodeStruct) (err error) {
+	var apiList = []model.SysApi{
+		{
+			Path:        "/" + a.Abbreviation + "/" + "create" + a.StructName,
+			Description: "新增" + a.Description,
+			ApiGroup:    a.Abbreviation,
+			Method:      "POST",
+		},
+		{
+			Path:        "/" + a.Abbreviation + "/" + "delete" + a.StructName,
+			Description: "删除" + a.Description,
+			ApiGroup:    a.Abbreviation,
+			Method:      "DELETE",
+		},
+		{
+			Path:        "/" + a.Abbreviation + "/" + "delete" + a.StructName + "ByIds",
+			Description: "批量删除" + a.Description,
+			ApiGroup:    a.Abbreviation,
+			Method:      "DELETE",
+		},
+		{
+			Path:        "/" + a.Abbreviation + "/" + "update" + a.StructName,
+			Description: "更新" + a.Description,
+			ApiGroup:    a.Abbreviation,
+			Method:      "PUT",
+		},
+		{
+			Path:        "/" + a.Abbreviation + "/" + "find" + a.StructName,
+			Description: "根据ID获取" + a.Description,
+			ApiGroup:    a.Abbreviation,
+			Method:      "GET",
+		},
+		{
+			Path:        "/" + a.Abbreviation + "/" + "get" + a.StructName + "List",
+			Description: "获取" + a.Description + "列表",
+			ApiGroup:    a.Abbreviation,
+			Method:      "GET",
+		},
+	}
+	err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
+		for _, v := range apiList {
+			var api model.SysApi
+			if err := tx.Where("path = ? AND method = ?", v.Path, v.Method).First(&api).Error; err != nil {
+				return err
+			}
+			if err := tx.Create(&v).Error; err != nil { // 遇到错误时回滚事务
+				return err
+			}
+		}
+		return nil
+	})
+	return err
+}