Skip to content

Commit 1d7add2

Browse files
author
nstian
committed
Refactoring to gin-admin v8.0
1 parent bb08942 commit 1d7add2

31 files changed

+474
-913
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
*.out
1313
/gin-admin-cli
1414
.idea
15+
.vscode

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# gin-admin-cli - [gin-admin](https://github.com/LyricTian/gin-admin)
22

3-
> GinAdmin 辅助工具,提供创建项目、快速生成功能模块的功能
3+
> gin-admin v8.0 辅助工具,提供创建项目、快速生成功能模块的功能
44
55
## 下载并使用
66

77
```bash
8-
go get -u github.com/gin-admin/gin-admin-cli/v4
8+
go get -u github.com/gin-admin/gin-admin-cli/v5
99
```
1010

1111
### 创建项目
@@ -15,10 +15,10 @@ USAGE:
1515
gin-admin-cli new [command options] [arguments...]
1616

1717
OPTIONS:
18-
--dir value, -d value 项目生成目录(默认GOPATH)
18+
--dir value, -d value 项目生成目录 (默认GOPATH)
1919
--pkg value, -p value 项目包名
20-
--branch value, -b value 指定分支(默认master)
21-
--mirror, -m 使用国内镜像(gitee.com)
20+
--branch value, -b value 指定分支 (默认master)
21+
--mirror, -m 使用国内镜像 (gitee.com)
2222
--web, -w 包含web项目
2323
```
2424

@@ -42,8 +42,7 @@ OPTIONS:
4242
--name value, -n value 业务模块名称(结构体名称)
4343
--comment value, -c value 业务模块注释(结构体注释)
4444
--file value, -f value 指定模板文件(.yaml,模板配置可参考说明)
45-
--module value, -m value 指定生成模块(默认生成全部模块,以逗号分隔,支持:schema,model,bll,api,mock,router)
46-
--storage value, -s value 指定model的实现存储(默认gorm,支持:mongo/gorm)
45+
--module value, -m value 指定生成模块(默认生成全部模块,以逗号分隔,支持:schema,dao,service,api,mock,router)
4746
```
4847

4948
> 使用示例

cmd/cmd.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"os"
77
"path/filepath"
88

9-
"github.com/gin-admin/gin-admin-cli/v4/cmd/generate"
10-
"github.com/gin-admin/gin-admin-cli/v4/cmd/new"
9+
"github.com/gin-admin/gin-admin-cli/v5/cmd/generate"
10+
"github.com/gin-admin/gin-admin-cli/v5/cmd/new"
1111
"github.com/urfave/cli"
1212
)
1313

@@ -95,11 +95,7 @@ func GenerateCommand() cli.Command {
9595
},
9696
&cli.StringFlag{
9797
Name: "module, m",
98-
Usage: "指定生成模块(默认生成全部模块,以逗号分隔,支持:schema,model,bll,api,mock,router)",
99-
},
100-
&cli.StringFlag{
101-
Name: "storage, s",
102-
Usage: "指定model的实现存储(默认gorm,支持:mongo/gorm)",
98+
Usage: "指定生成模块(默认生成全部模块,以逗号分隔,支持:schema,dao,service,api,mock,router)",
10399
},
104100
},
105101
Action: func(c *cli.Context) error {
@@ -110,7 +106,6 @@ func GenerateCommand() cli.Command {
110106
Comment: c.String("comment"),
111107
File: c.String("file"),
112108
Modules: c.String("module"),
113-
Storage: c.String("storage"),
114109
}
115110

116111
if cfg.Dir == "" {

cmd/generate/api.go

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ package generate
33
import (
44
"context"
55
"fmt"
6-
"strings"
76

8-
"github.com/gin-admin/gin-admin-cli/v4/util"
7+
"github.com/gin-admin/gin-admin-cli/v5/util"
98
)
109

1110
func getAPIFileName(dir, name string) string {
@@ -14,9 +13,6 @@ func getAPIFileName(dir, name string) string {
1413
}
1514

1615
func genAPI(ctx context.Context, pkgName, dir, name, comment string) error {
17-
pname := util.ToPlural(util.ToLowerUnderlinedNamer(name))
18-
pname = strings.Replace(pname, "_", "-", -1)
19-
2016
data := map[string]interface{}{
2117
"PkgName": pkgName,
2218
"Name": name,
@@ -34,7 +30,7 @@ func genAPI(ctx context.Context, pkgName, dir, name, comment string) error {
3430
return err
3531
}
3632

37-
fmt.Printf("文件[%s]写入成功\n", fullname)
33+
fmt.Printf("File write success: %s\n", fullname)
3834

3935
return execGoFmt(fullname)
4036
}
@@ -43,23 +39,22 @@ const apiTpl = `
4339
package api
4440
4541
import (
46-
"{{.PkgName}}/internal/app/service"
47-
"{{.PkgName}}/internal/app/ginx"
48-
"{{.PkgName}}/internal/app/schema"
4942
"github.com/gin-gonic/gin"
5043
"github.com/google/wire"
44+
45+
"{{.PkgName}}/internal/app/contextx"
46+
"{{.PkgName}}/internal/app/ginx"
47+
"{{.PkgName}}/internal/app/schema"
48+
"{{.PkgName}}/internal/app/service"
5149
)
5250
53-
// {{.Name}}Set 注入{{.Name}}
54-
var {{.Name}}Set = wire.NewSet(wire.Struct(new({{.Name}}), "*"))
51+
var {{.Name}}Set = wire.NewSet(wire.Struct(new({{.Name}}API), "*"))
5552
56-
// {{.Name}} {{.Comment}}
57-
type {{.Name}} struct {
58-
{{.Name}}Srv *service.{{.Name}}
53+
type {{.Name}}API struct {
54+
{{.Name}}Srv *service.{{.Name}}Srv
5955
}
6056
61-
// Query 查询数据
62-
func (a *{{.Name}}) Query(c *gin.Context) {
57+
func (a *{{.Name}}API) Query(c *gin.Context) {
6358
ctx := c.Request.Context()
6459
var params schema.{{.Name}}QueryParam
6560
if err := ginx.ParseQuery(c, &params); err != nil {
@@ -68,36 +63,35 @@ func (a *{{.Name}}) Query(c *gin.Context) {
6863
}
6964
7065
params.Pagination = true
71-
result, err := a.{{.Name}}Srv.Query(ctx, params)
66+
result, err := a.{{.Name}}Srv.Query(ctx, params, schema.{{.Name}}QueryOptions{
67+
OrderFields: schema.NewOrderFields(schema.NewOrderField("sequence", schema.OrderByDESC)),
68+
})
7269
if err != nil {
7370
ginx.ResError(c, err)
7471
return
7572
}
76-
7773
ginx.ResPage(c, result.Data, result.PageResult)
7874
}
7975
80-
// Get 查询指定数据
81-
func (a *{{.Name}}) Get(c *gin.Context) {
76+
func (a *{{.Name}}API) Get(c *gin.Context) {
8277
ctx := c.Request.Context()
83-
item, err := a.{{.Name}}Srv.Get(ctx, c.Param("id"))
78+
item, err := a.{{.Name}}Srv.Get(ctx, ginx.ParseParamID(c, "id"))
8479
if err != nil {
8580
ginx.ResError(c, err)
8681
return
8782
}
8883
ginx.ResSuccess(c, item)
8984
}
9085
91-
// Create 创建数据
92-
func (a *{{.Name}}) Create(c *gin.Context) {
86+
func (a *{{.Name}}API) Create(c *gin.Context) {
9387
ctx := c.Request.Context()
9488
var item schema.{{.Name}}
9589
if err := ginx.ParseJSON(c, &item); err != nil {
9690
ginx.ResError(c, err)
9791
return
9892
}
9993
100-
item.Creator = ginx.GetUserID(c)
94+
item.Creator = contextx.FromUserID(ctx)
10195
result, err := a.{{.Name}}Srv.Create(ctx, item)
10296
if err != nil {
10397
ginx.ResError(c, err)
@@ -106,27 +100,45 @@ func (a *{{.Name}}) Create(c *gin.Context) {
106100
ginx.ResSuccess(c, result)
107101
}
108102
109-
// Update 更新数据
110-
func (a *{{.Name}}) Update(c *gin.Context) {
103+
func (a *{{.Name}}API) Update(c *gin.Context) {
111104
ctx := c.Request.Context()
112105
var item schema.{{.Name}}
113106
if err := ginx.ParseJSON(c, &item); err != nil {
114107
ginx.ResError(c, err)
115108
return
116109
}
117110
118-
err := a.{{.Name}}Srv.Update(ctx, c.Param("id"), item)
111+
err := a.{{.Name}}Srv.Update(ctx, ginx.ParseParamID(c, "id"), item)
112+
if err != nil {
113+
ginx.ResError(c, err)
114+
return
115+
}
116+
ginx.ResOK(c)
117+
}
118+
119+
func (a *{{.Name}}API) Delete(c *gin.Context) {
120+
ctx := c.Request.Context()
121+
err := a.{{.Name}}Srv.Delete(ctx, ginx.ParseParamID(c, "id"))
122+
if err != nil {
123+
ginx.ResError(c, err)
124+
return
125+
}
126+
ginx.ResOK(c)
127+
}
128+
129+
func (a *{{.Name}}API) Enable(c *gin.Context) {
130+
ctx := c.Request.Context()
131+
err := a.{{.Name}}Srv.UpdateStatus(ctx, ginx.ParseParamID(c, "id"), 1)
119132
if err != nil {
120133
ginx.ResError(c, err)
121134
return
122135
}
123136
ginx.ResOK(c)
124137
}
125138
126-
// Delete 删除数据
127-
func (a *{{.Name}}) Delete(c *gin.Context) {
139+
func (a *{{.Name}}API) Disable(c *gin.Context) {
128140
ctx := c.Request.Context()
129-
err := a.{{.Name}}Srv.Delete(ctx, c.Param("id"))
141+
err := a.{{.Name}}Srv.UpdateStatus(ctx, ginx.ParseParamID(c, "id"), 2)
130142
if err != nil {
131143
ginx.ResError(c, err)
132144
return

cmd/generate/api_inject.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func insertAPIInject(ctx context.Context, dir, name string) error {
2020
return
2121
}
2222

23-
if injectStart == 1 && strings.Contains(line, ")") {
23+
if injectStart == 1 && strings.Contains(line, ") // end") {
2424
injectStart = -1
2525
data = injectContent
2626
flag = -1
@@ -37,7 +37,7 @@ func insertAPIInject(ctx context.Context, dir, name string) error {
3737
return err
3838
}
3939

40-
fmt.Printf("文件[%s]写入成功\n", filename)
40+
fmt.Printf("File write success: %s\n", filename)
4141

4242
return execGoFmt(filename)
4343
}

cmd/generate/api_mock.go

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"strings"
77

8-
"github.com/gin-admin/gin-admin-cli/v4/util"
8+
"github.com/gin-admin/gin-admin-cli/v5/util"
99
)
1010

1111
func getAPIMockFileName(dir, name string) string {
@@ -35,7 +35,7 @@ func genAPIMock(ctx context.Context, pkgName, dir, name, comment string) error {
3535
return err
3636
}
3737

38-
fmt.Printf("文件[%s]写入成功\n", fullname)
38+
fmt.Printf("File write success: %s\n", fullname)
3939

4040
return execGoFmt(fullname)
4141
}
@@ -48,77 +48,100 @@ import (
4848
"github.com/google/wire"
4949
)
5050
51-
// {{.Name}}Set 注入{{.Name}}
52-
var {{.Name}}Set = wire.NewSet(wire.Struct(new({{.Name}}), "*"))
51+
var {{.Name}}Set = wire.NewSet(wire.Struct(new({{.Name}}Mock), "*"))
5352
54-
// {{.Name}} {{.Comment}}
55-
type {{.Name}} struct {
56-
}
53+
// {{.Name}}Mock {{.Comment}}
54+
type {{.Name}}Mock struct{}
5755
5856
// Query 查询数据
5957
// @Tags {{.Comment}}
6058
// @Summary 查询数据
61-
// @Param Authorization header string false "Bearer 用户令牌"
59+
// @Security ApiKeyAuth
6260
// @Param current query int true "分页索引" default(1)
6361
// @Param pageSize query int true "分页大小" default(10)
6462
// @Param queryValue query string false "查询值"
65-
// @Success 200 {array} schema.{{.Name}} "查询结果:{list:列表数据,pagination:{current:页索引,pageSize:页大小,total:总数量}}"
63+
// @Param status query int false "状态(1:启用 2:禁用)"
64+
// @Success 200 {object} schema.ListResult{list=[]schema.{{.Name}}} "查询结果"
6665
// @Failure 401 {object} schema.ErrorResult "{error:{code:0,message:未授权}}"
6766
// @Failure 500 {object} schema.ErrorResult "{error:{code:0,message:服务器错误}}"
6867
// @Router /api/v1/{{.PluralName}} [get]
69-
func (a *{{.Name}}) Query(c *gin.Context) {
68+
func (a *{{.Name}}Mock) Query(c *gin.Context) {
7069
}
7170
7271
// Get 查询指定数据
7372
// @Tags {{.Comment}}
7473
// @Summary 查询指定数据
75-
// @Param Authorization header string false "Bearer 用户令牌"
76-
// @Param id path string true "唯一标识"
74+
// @Security ApiKeyAuth
75+
// @Param id path int true "唯一标识"
7776
// @Success 200 {object} schema.{{.Name}}
7877
// @Failure 401 {object} schema.ErrorResult "{error:{code:0,message:未授权}}"
7978
// @Failure 404 {object} schema.ErrorResult "{error:{code:0,message:资源不存在}}"
8079
// @Failure 500 {object} schema.ErrorResult "{error:{code:0,message:服务器错误}}"
8180
// @Router /api/v1/{{.PluralName}}/{id} [get]
82-
func (a *{{.Name}}) Get(c *gin.Context) {
81+
func (a *{{.Name}}Mock) Get(c *gin.Context) {
8382
}
8483
8584
// Create 创建数据
8685
// @Tags {{.Comment}}
8786
// @Summary 创建数据
88-
// @Param Authorization header string false "Bearer 用户令牌"
87+
// @Security ApiKeyAuth
8988
// @Param body body schema.{{.Name}} true "创建数据"
9089
// @Success 200 {object} schema.IDResult
9190
// @Failure 400 {object} schema.ErrorResult "{error:{code:0,message:无效的请求参数}}"
9291
// @Failure 401 {object} schema.ErrorResult "{error:{code:0,message:未授权}}"
9392
// @Failure 500 {object} schema.ErrorResult "{error:{code:0,message:服务器错误}}"
9493
// @Router /api/v1/{{.PluralName}} [post]
95-
func (a *{{.Name}}) Create(c *gin.Context) {
94+
func (a *{{.Name}}Mock) Create(c *gin.Context) {
9695
}
9796
9897
// Update 更新数据
9998
// @Tags {{.Comment}}
10099
// @Summary 更新数据
101-
// @Param Authorization header string false "Bearer 用户令牌"
102-
// @Param id path string true "唯一标识"
100+
// @Security ApiKeyAuth
101+
// @Param id path int true "唯一标识"
103102
// @Param body body schema.{{.Name}} true "更新数据"
104103
// @Success 200 {object} schema.StatusResult "{status:OK}"
105104
// @Failure 400 {object} schema.ErrorResult "{error:{code:0,message:无效的请求参数}}"
106105
// @Failure 401 {object} schema.ErrorResult "{error:{code:0,message:未授权}}"
107106
// @Failure 500 {object} schema.ErrorResult "{error:{code:0,message:服务器错误}}"
108107
// @Router /api/v1/{{.PluralName}}/{id} [put]
109-
func (a *{{.Name}}) Update(c *gin.Context) {
108+
func (a *{{.Name}}Mock) Update(c *gin.Context) {
110109
}
111110
112111
// Delete 删除数据
113112
// @Tags {{.Comment}}
114113
// @Summary 删除数据
115-
// @Param Authorization header string false "Bearer 用户令牌"
116-
// @Param id path string true "唯一标识"
114+
// @Security ApiKeyAuth
115+
// @Param id path int true "唯一标识"
117116
// @Success 200 {object} schema.StatusResult "{status:OK}"
118117
// @Failure 401 {object} schema.ErrorResult "{error:{code:0,message:未授权}}"
119118
// @Failure 500 {object} schema.ErrorResult "{error:{code:0,message:服务器错误}}"
120119
// @Router /api/v1/{{.PluralName}}/{id} [delete]
121-
func (a *{{.Name}}) Delete(c *gin.Context) {
120+
func (a *{{.Name}}Mock) Delete(c *gin.Context) {
121+
}
122+
123+
// Enable 启用数据
124+
// @Tags {{.Comment}}
125+
// @Summary 启用数据
126+
// @Security ApiKeyAuth
127+
// @Param id path int true "唯一标识"
128+
// @Success 200 {object} schema.StatusResult "{status:OK}"
129+
// @Failure 401 {object} schema.ErrorResult "{error:{code:0,message:未授权}}"
130+
// @Failure 500 {object} schema.ErrorResult "{error:{code:0,message:服务器错误}}"
131+
// @Router /api/v1/{{.PluralName}}/{id}/enable [patch]
132+
func (a *{{.Name}}Mock) Enable(c *gin.Context) {
133+
}
134+
135+
// Disable 禁用数据
136+
// @Tags {{.Comment}}
137+
// @Summary 禁用数据
138+
// @Security ApiKeyAuth
139+
// @Param id path int true "唯一标识"
140+
// @Success 200 {object} schema.StatusResult "{status:OK}"
141+
// @Failure 401 {object} schema.ErrorResult "{error:{code:0,message:未授权}}"
142+
// @Failure 500 {object} schema.ErrorResult "{error:{code:0,message:服务器错误}}"
143+
// @Router /api/v1/{{.PluralName}}/{id}/disable [patch]
144+
func (a *{{.Name}}Mock) Disable(c *gin.Context) {
122145
}
123146
124147
`

0 commit comments

Comments
 (0)