Skip to content

Commit 70b2ee3

Browse files
SliverHornSliverHorn
SliverHorn
authored and
SliverHorn
committed
更新自动化代码api层代码混入service层代码
1 parent 6779054 commit 70b2ee3

File tree

3 files changed

+68
-67
lines changed

3 files changed

+68
-67
lines changed

server/api/v1/sys_auto_code.go

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,51 +30,11 @@ func CreateTemp(c *gin.Context) {
3030
return
3131
}
3232
if a.AutoCreateApiToSql {
33-
apiList := [6]model.SysApi{
34-
{
35-
Path: "/" + a.Abbreviation + "/" + "create" + a.StructName,
36-
Description: "新增" + a.Description,
37-
ApiGroup: a.Abbreviation,
38-
Method: "POST",
39-
},
40-
{
41-
Path: "/" + a.Abbreviation + "/" + "delete" + a.StructName,
42-
Description: "删除" + a.Description,
43-
ApiGroup: a.Abbreviation,
44-
Method: "DELETE",
45-
},
46-
{
47-
Path: "/" + a.Abbreviation + "/" + "delete" + a.StructName + "ByIds",
48-
Description: "批量删除" + a.Description,
49-
ApiGroup: a.Abbreviation,
50-
Method: "DELETE",
51-
},
52-
{
53-
Path: "/" + a.Abbreviation + "/" + "update" + a.StructName,
54-
Description: "更新" + a.Description,
55-
ApiGroup: a.Abbreviation,
56-
Method: "PUT",
57-
},
58-
{
59-
Path: "/" + a.Abbreviation + "/" + "find" + a.StructName,
60-
Description: "根据ID获取" + a.Description,
61-
ApiGroup: a.Abbreviation,
62-
Method: "GET",
63-
},
64-
{
65-
Path: "/" + a.Abbreviation + "/" + "get" + a.StructName + "List",
66-
Description: "获取" + a.Description + "列表",
67-
ApiGroup: a.Abbreviation,
68-
Method: "GET",
69-
},
70-
}
71-
for _, v := range apiList {
72-
if err := service.AutoCreateApi(v); err != nil {
73-
global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
74-
c.Writer.Header().Add("success", "false")
75-
c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
76-
return
77-
}
33+
if err := service.AutoCreateApi(&a); err != nil {
34+
global.GVA_LOG.Error("自动化创建失败!请自行清空垃圾数据!", zap.Any("err", err))
35+
c.Writer.Header().Add("success", "false")
36+
c.Writer.Header().Add("msg", url.QueryEscape("自动化创建失败!请自行清空垃圾数据!"))
37+
return
7838
}
7939
}
8040
err := service.CreateTemp(a)

server/service/sys_api.go

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,28 +33,6 @@ func DeleteApi(api model.SysApi) (err error) {
3333
return err
3434
}
3535

36-
//@author: [piexlmax](https://github.com/piexlmax)
37-
//@function: CreateApi
38-
//@description: 自动创建api数据,
39-
//@param: api model.SysApi
40-
//@return: err error
41-
42-
func AutoCreateApi(api model.SysApi) (err error) {
43-
err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
44-
var fApi model.SysApi
45-
var txErr error
46-
fxErr := tx.Where("path = ? AND method = ?", api.Path, api.Method).First(&fApi).Error
47-
if errors.Is(fxErr, gorm.ErrRecordNotFound) {
48-
txErr = tx.Create(&api).Error
49-
if txErr != nil {
50-
return txErr
51-
}
52-
}
53-
return nil
54-
})
55-
return err
56-
}
57-
5836
//@author: [piexlmax](https://github.com/piexlmax)
5937
//@function: GetAPIInfoList
6038
//@description: 分页获取数据,

server/service/sys_auto_code.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"gin-vue-admin/model"
88
"gin-vue-admin/model/request"
99
"gin-vue-admin/utils"
10+
"gorm.io/gorm"
1011
"io/ioutil"
1112
"os"
1213
"path/filepath"
@@ -207,3 +208,65 @@ func addAutoMoveFile(data *tplData) {
207208
}
208209
}
209210
}
211+
212+
213+
//@author: [piexlmax](https://github.com/piexlmax)
214+
//@author: [SliverHorn](https://github.com/SliverHorn)
215+
//@function: CreateApi
216+
//@description: 自动创建api数据,
217+
//@param: a *model.AutoCodeStruct
218+
//@return: error
219+
220+
func AutoCreateApi(a *model.AutoCodeStruct) (err error) {
221+
var apiList = []model.SysApi{
222+
{
223+
Path: "/" + a.Abbreviation + "/" + "create" + a.StructName,
224+
Description: "新增" + a.Description,
225+
ApiGroup: a.Abbreviation,
226+
Method: "POST",
227+
},
228+
{
229+
Path: "/" + a.Abbreviation + "/" + "delete" + a.StructName,
230+
Description: "删除" + a.Description,
231+
ApiGroup: a.Abbreviation,
232+
Method: "DELETE",
233+
},
234+
{
235+
Path: "/" + a.Abbreviation + "/" + "delete" + a.StructName + "ByIds",
236+
Description: "批量删除" + a.Description,
237+
ApiGroup: a.Abbreviation,
238+
Method: "DELETE",
239+
},
240+
{
241+
Path: "/" + a.Abbreviation + "/" + "update" + a.StructName,
242+
Description: "更新" + a.Description,
243+
ApiGroup: a.Abbreviation,
244+
Method: "PUT",
245+
},
246+
{
247+
Path: "/" + a.Abbreviation + "/" + "find" + a.StructName,
248+
Description: "根据ID获取" + a.Description,
249+
ApiGroup: a.Abbreviation,
250+
Method: "GET",
251+
},
252+
{
253+
Path: "/" + a.Abbreviation + "/" + "get" + a.StructName + "List",
254+
Description: "获取" + a.Description + "列表",
255+
ApiGroup: a.Abbreviation,
256+
Method: "GET",
257+
},
258+
}
259+
err = global.GVA_DB.Transaction(func(tx *gorm.DB) error {
260+
for _, v := range apiList {
261+
var api model.SysApi
262+
if err := tx.Where("path = ? AND method = ?", v.Path, v.Method).First(&api).Error; err != nil {
263+
return err
264+
}
265+
if err := tx.Create(&v).Error; err != nil { // 遇到错误时回滚事务
266+
return err
267+
}
268+
}
269+
return nil
270+
})
271+
return err
272+
}

0 commit comments

Comments
 (0)