|
1 | 1 | package model |
2 | 2 |
|
3 | 3 | 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" |
11 | 12 | ) |
12 | 13 |
|
13 | 14 | type ApiGenerator struct { |
@@ -136,8 +137,21 @@ func (gen *ApiGenerator) GenApiCreateFile(ctx context.Context, file string) erro |
136 | 137 | tableApi := tableCamel + "Api" |
137 | 138 | tableModel := tableCamel + "Model" |
138 | 139 |
|
| 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 | + |
139 | 152 | f := jen.NewFile(gen.packageName) |
140 | 153 |
|
| 154 | + f.Comment(comment) |
141 | 155 | f.Func().Params( |
142 | 156 | jen.Id("api").Op("*").Id(tableApi), |
143 | 157 | ).Id("Create").Params( |
@@ -204,6 +218,22 @@ func (gen *ApiGenerator) GenApiDeleteFile(ctx context.Context, file string) erro |
204 | 218 |
|
205 | 219 | f := jen.NewFile(gen.packageName) |
206 | 220 |
|
| 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) |
207 | 237 | f.Func().Params( |
208 | 238 | jen.Id("api").Op("*").Id(tableApi), |
209 | 239 | ).Id("Delete").Params( |
@@ -269,6 +299,22 @@ func (gen *ApiGenerator) GenApiListFile(ctx context.Context, file string) error |
269 | 299 |
|
270 | 300 | f := jen.NewFile(gen.packageName) |
271 | 301 |
|
| 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) |
272 | 318 | 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( |
273 | 319 | jen.List(jen.Id("offset"), jen.Id("err")).Op(":=").Qual("strconv", |
274 | 320 | "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 |
332 | 378 |
|
333 | 379 | f := jen.NewFile(gen.packageName) |
334 | 380 |
|
| 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) |
335 | 397 | 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( |
336 | 398 |
|
337 | 399 | 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 |
387 | 449 | tableModel := tableCamel + "Model" |
388 | 450 |
|
389 | 451 | 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) |
391 | 469 | 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( |
392 | 470 | jen.List(jen.Id("id"), jen.Id("err")).Op(":=").Qual("strconv", "Atoi").Call(jen.Id("c").Dot("Query").Call(jen.Lit("id"))), |
393 | 471 | jen.If(jen.Err().Op("!=").Nil()).Block( |
|
0 commit comments