Skip to content

Commit 4ff1dab

Browse files
committed
增加gen model api 接口
1 parent 453dbd9 commit 4ff1dab

File tree

4 files changed

+166
-13
lines changed

4 files changed

+166
-13
lines changed

docs/guide/model.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,47 @@
22

33
## 指南
44

5-
hade 提供了自动生成数据库模型的命令行工具
5+
hade model 提供了自动生成数据库模型的命令行工具,如果你已经定义好你的model,这里的系列工具能帮助你节省不少时间。
66

7+
```shell
8+
> ./hade model
9+
数据库模型相关的命令
10+
11+
Usage:
12+
hade model [flags]
13+
hade model [command]
14+
15+
Available Commands:
16+
api 通过数据库生成api
17+
gen 生成模型
18+
test 测试数据库
19+
20+
Flags:
21+
-h, --help help for model
22+
```
23+
24+
包含三个命令:
25+
26+
* ./hade model api 通过数据表自动生成api代码
27+
* ./hade model gen 通过数据表自动生成model代码
28+
* ./hade model test 测试某个数据库是否可以连接
29+
30+
## ./hade test
31+
32+
当你想测试下你的某个配置好的数据库是否能连接上,都有哪些表的时候,这个命令能帮助你。
33+
34+
```shell
35+
> ./hade model test --database="database.default"
36+
数据库连接:database.default成功
37+
一共存在1张表
38+
student
739
```
40+
41+
## ./hade model gen
42+
43+
这个命令帮助你生成数据表对应的gorm的model
44+
45+
```shell
846
✘  ~/Documents/workspace/gohade/hade   feature/model-gen ●  ./hade model gen
947
Error: required flag(s) "output" not set
1048
Usage:
@@ -23,7 +61,7 @@ database: 这个参数可选,用来表示模型连接的数据库配置地址
2361

2462
output:这个参数必填,用来表示模型文件的输出地址,如果填写相对路径,会在前面填充当前执行路径来补充为绝对路径。
2563

26-
## 使用方式
64+
### 使用方式
2765

2866
第一步,使用 `./hade model gen --output=app/model`
2967

@@ -45,4 +83,29 @@ output:这个参数必填,用来表示模型文件的输出地址,如果
4583

4684
![image-20220215091735434](http://tuchuang.funaio.cn/img/image-20220215091735434.png)
4785

86+
## ./hade model api
87+
88+
```shell
89+
> ./hade model api --database=database.default --output=/Users/jianfengye/Documents/workspace/gohade/hade/app/http/module/student/
90+
```
91+
92+
![](http://tuchuang.funaio.cn/markdown/202303140005210.png)
93+
94+
它会在目标文件夹中生成对这个数据表的5个接口文件
95+
96+
* gen_[table]_api_create.go
97+
* gen_[table]_api_delete.go
98+
* gen_[table]_api_list.go
99+
* gen_[table]_api_show.go
100+
* gen_[table]_api_update.go
101+
102+
还有另外两个文件
103+
104+
* gen_[table]_model.go // 数据表对应的模型结构
105+
* gen_[table]_router.go // 5个接口对应的默认路由
48106

107+
> 这里的每个文件都是可以修改的。
108+
>
109+
> 但是注意,如果重新生成,会覆盖原先的文件。
110+
>
111+
> 执行命令的时候切记保存已经改动的代码文件。

framework/command/model/api.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ package model
22

33
import (
44
"fmt"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
59
"github.com/AlecAivazis/survey/v2"
610
"github.com/gohade/hade/framework/cobra"
711
"github.com/gohade/hade/framework/contract"
812
"github.com/gohade/hade/framework/provider/orm"
913
"github.com/gohade/hade/framework/util"
1014
"github.com/pkg/errors"
11-
"path/filepath"
12-
"strings"
1315
)
1416

1517
// modelApiCommand 生成api
@@ -113,6 +115,12 @@ var modelApiCommand = &cobra.Command{
113115
// get folder last string split by path separator
114116
apiGenerator.SetPackageName(strings.ToLower(filepath.Base(folder)))
115117

118+
if !util.Exists(folder) {
119+
if err := os.Mkdir(folder, 0755); err != nil {
120+
return errors.Wrap(err, "create folder error")
121+
}
122+
}
123+
116124
if err := apiGenerator.GenModelFile(ctx, modelFile); err != nil {
117125
return errors.Wrap(err, "GenModelFile error")
118126
}
@@ -149,6 +157,10 @@ var modelApiCommand = &cobra.Command{
149157
fmt.Println(getFileTip(apiUpdateFile))
150158
}
151159

160+
fmt.Println("=======================")
161+
fmt.Println("生成结束,请记得挂载路由到route.go中")
162+
fmt.Println("!!! hade代码生成器按照既定程序生成文件,请自行仔细检查代码逻辑 !!!")
163+
fmt.Println("=======================")
152164
return nil
153165
},
154166
}

framework/command/model/api_gen.go

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package model
22

33
import (
4-
"context"
5-
"github.com/dave/jennifer/jen"
6-
"github.com/gohade/hade/framework/contract"
7-
"github.com/gohade/hade/framework/util/word"
8-
"github.com/pkg/errors"
9-
"os"
10-
"strings"
4+
"context"
5+
"os"
6+
"strings"
7+
8+
"github.com/dave/jennifer/jen"
9+
"github.com/gohade/hade/framework/contract"
10+
"github.com/gohade/hade/framework/util/word"
11+
"github.com/pkg/errors"
1112
)
1213

1314
type ApiGenerator struct {
@@ -136,8 +137,21 @@ func (gen *ApiGenerator) GenApiCreateFile(ctx context.Context, file string) erro
136137
tableApi := tableCamel + "Api"
137138
tableModel := tableCamel + "Model"
138139

140+
comment := ` Create create ${table}
141+
@Summary create ${table}
142+
@Produce json
143+
@Tags ${table}
144+
@Param ${table} body ${tableModel} true "${tableModel}"
145+
@Success 200 {object} ${tableModel}
146+
@Failure 400 {object} gin.H
147+
@Failure 500 {object} gin.H
148+
@Router /${table}/create [post]`
149+
comment = strings.ReplaceAll(comment, "${tableModel}", tableModel)
150+
comment = strings.ReplaceAll(comment, "${table}", tableLower)
151+
139152
f := jen.NewFile(gen.packageName)
140153

154+
f.Comment(comment)
141155
f.Func().Params(
142156
jen.Id("api").Op("*").Id(tableApi),
143157
).Id("Create").Params(
@@ -204,6 +218,22 @@ func (gen *ApiGenerator) GenApiDeleteFile(ctx context.Context, file string) erro
204218

205219
f := jen.NewFile(gen.packageName)
206220

221+
comment := ` Delete godoc
222+
@Summary Delete a ${table} by ID
223+
@Description Delete a ${table} by ID
224+
@Tags ${table}
225+
@Accept json
226+
@Produce json
227+
@Param id query int true "ID"
228+
@Success 200 {string} string "success"
229+
@Failure 400 {object} ErrorResponse "Invalid parameter"
230+
@Failure 404 {object} ErrorResponse "Record not found"
231+
@Failure 500 {object} ErrorResponse "Server error"
232+
@Router /${table}/delete [post]`
233+
comment = strings.ReplaceAll(comment, "${tableModel}", tableModel)
234+
comment = strings.ReplaceAll(comment, "${table}", tableLower)
235+
236+
f.Comment(comment)
207237
f.Func().Params(
208238
jen.Id("api").Op("*").Id(tableApi),
209239
).Id("Delete").Params(
@@ -269,6 +299,22 @@ func (gen *ApiGenerator) GenApiListFile(ctx context.Context, file string) error
269299

270300
f := jen.NewFile(gen.packageName)
271301

302+
comment := ` List godoc
303+
@Summary Get a list of ${table}
304+
@Description Get a list of ${table} with pagination support
305+
@Tags ${table}
306+
@Accept json
307+
@Produce json
308+
@Param offset query integer true "offset"
309+
@Param size query integer true "size"
310+
@Success 200 {object} gin.H
311+
@Failure 400 {object} gin.H
312+
@Failure 500 {object} gin.H
313+
@Router /${table}/list [get]`
314+
comment = strings.ReplaceAll(comment, "${tableModel}", tableModel)
315+
comment = strings.ReplaceAll(comment, "${table}", tableLower)
316+
317+
f.Comment(comment)
272318
f.Func().Params(jen.Id("api").Op("*").Id(tableApi)).Id("List").Params(jen.Id("c").Op("*").Qual("github.com/gohade/hade/framework/gin", "Context")).Block(
273319
jen.List(jen.Id("offset"), jen.Id("err")).Op(":=").Qual("strconv",
274320
"Atoi").Call(jen.Id("c").Dot("Query").Call(jen.Lit("offset"))),
@@ -332,6 +378,22 @@ func (gen *ApiGenerator) GenApiShowFile(ctx context.Context, file string) error
332378

333379
f := jen.NewFile(gen.packageName)
334380

381+
comment := ` Show godoc
382+
@Summary Get a ${table} by ID
383+
@Description Get a ${table} by ID
384+
@Tags ${table}
385+
@Accept json
386+
@Produce json
387+
@Param id query integer true "${table} ID"
388+
@Success 200 {object} ${tableModel}
389+
@Failure 400 {object} gin.H
390+
@Failure 404 {object} gin.H
391+
@Failure 500 {object} gin.H
392+
@Router /${table}/{id} [get]`
393+
comment = strings.ReplaceAll(comment, "${tableModel}", tableModel)
394+
comment = strings.ReplaceAll(comment, "${table}", tableLower)
395+
396+
f.Comment(comment)
335397
f.Func().Params(jen.Id("api").Op("*").Id(tableApi)).Id("Show").Params(jen.Id("c").Op("*").Qual("github.com/gohade/hade/framework/gin", "Context")).Block(
336398

337399
jen.List(jen.Id("id"), jen.Id("err")).Op(":=").Qual("strconv", "Atoi").Call(jen.Id("c").Dot("Query").Call(jen.Lit("id"))),
@@ -387,7 +449,23 @@ func (gen *ApiGenerator) GenApiUpdateFile(ctx context.Context, file string) erro
387449
tableModel := tableCamel + "Model"
388450

389451
f := jen.NewFile(gen.packageName)
390-
452+
comment := ` Update godoc
453+
@Summary Update a ${table} by ID
454+
@Description Update a ${table} by ID
455+
@Tags ${table}
456+
@Accept json
457+
@Produce json
458+
@Param id query int true "ID of the ${table} to update"
459+
@Param student body ${tableModel} true "${table} information to update"
460+
@Success 200 {object} ${tableModel}
461+
@Failure 400 {object} gin.H
462+
@Failure 404 {object} gin.H
463+
@Failure 500 {object} gin.H
464+
@Router /${table}/update [post]`
465+
comment = strings.ReplaceAll(comment, "${tableModel}", tableModel)
466+
comment = strings.ReplaceAll(comment, "${table}", tableLower)
467+
468+
f.Comment(comment)
391469
f.Func().Params(jen.Id("api").Op("*").Id(tableApi)).Id("Update").Params(jen.Id("c").Op("*").Qual("github.com/gohade/hade/framework/gin", "Context")).Block(
392470
jen.List(jen.Id("id"), jen.Id("err")).Op(":=").Qual("strconv", "Atoi").Call(jen.Id("c").Dot("Query").Call(jen.Lit("id"))),
393471
jen.If(jen.Err().Op("!=").Nil()).Block(

framework/provider/app/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package app
22

33
// HadeVersion hade的版本
4-
const HadeVersion = "1.0.5"
4+
const HadeVersion = "1.0.6"

0 commit comments

Comments
 (0)