[优化]restful

This commit is contained in:
PandaGoAdmin
2022-08-03 22:29:46 +08:00
parent 0f08bac600
commit 78b93cb9af
3 changed files with 62 additions and 75 deletions

View File

@@ -5,10 +5,10 @@ package api
// 生成人:{{.FunctionAuthor}}
// ==========================================================================
import (
"github.com/XM-GO/PandaKit/restfulx"
"pandax/apps/{{.PackageName}}/entity"
"pandax/apps/{{.PackageName}}/services"
"github.com/XM-GO/PandaKit/restfulx"
"github.com/XM-GO/PandaKit/restfulx"
"github.com/XM-GO/PandaKit/utils"
)
@@ -16,23 +16,17 @@ type {{.ClassName}}Api struct {
{{.ClassName}}App services.{{.ClassName}}Model
}
// @Summary {{.FunctionName}}列表数据
// @Tags {{.FunctionName}}
// @Param pageSize query int false "页条数"
// @Param pageNum query int false "页码"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /{{.PackageName}}/{{.BusinessName}}/list [get]
// @Security
// Get{{.ClassName}}List {{.FunctionName}}列表数据
func (p *{{.ClassName}}Api) Get{{.ClassName}}List(rc *restfulx.ReqCtx) {
data := entity.{{.ClassName}}{}
pageNum := restfulx.QueryInt(rc.GinCtx, "pageNum", 1)
pageSize := restfulx.QueryInt(rc.GinCtx, "pageSize", 10)
pageNum := restfulx.QueryInt(rc, "pageNum", 1)
pageSize := restfulx.QueryInt(rc, "pageSize", 10)
{{- range $index, $column := .Columns -}}
{{- if eq $column.IsRequired "1" -}}
{{- if eq $column.GoType "string" }}
data.{{$column.GoField}} = rc.GinCtx.Query("{{$column.JsonField}}")
data.{{$column.GoField}} = restfulx.QueryParam(rc, "{{$column.JsonField}}")
{{- else if or (eq $column.GoType "int") (eq $column.GoType "int64") (eq $column.GoType "uint") (eq $column.GoType "uint64") -}}
data.{{$column.GoField}} = restfulx.QueryInt(rc.GinCtx, "{{$column.JsonField}}", 0)
data.{{$column.GoField}} = restfulx.QueryInt(rc, "{{$column.JsonField}}", 0)
{{- end -}}
{{- end -}}
{{- end }}
@@ -47,62 +41,31 @@ func (p *{{.ClassName}}Api) Get{{.ClassName}}List(rc *restfulx.ReqCtx) {
}
}
// @Summary 获取{{.FunctionName}}
// @Description 获取JSON
// @Tags {{.FunctionName}}
// @Param {{.PkJsonField}} path int true "{{.PkJsonField}}"
// @Success 200 {string} string "{"code": 200, "data": [...]}"
// @Router /{{.PackageName}}/{{.BusinessName}}/{{"{"}}{{.PkJsonField}}{{"}"}} [get]
// @Security
// Get{{.ClassName}} 获取{{.FunctionName}}
func (p *{{.ClassName}}Api) Get{{.ClassName}}(rc *restfulx.ReqCtx) {
{{.PkJsonField}} := restfulx.PathParamInt(rc.GinCtx, "{{.PkJsonField}}")
{{.PkJsonField}} := restfulx.PathParamInt(rc, "{{.PkJsonField}}")
p.{{.ClassName}}App.FindOne(int64({{.PkJsonField}}))
}
// @Summary 添加{{.FunctionName}}
// @Description 获取JSON
// @Tags {{.FunctionName}}
// @Accept application/json
// @Product application/json
// @Param data body entity.{{.ClassName}} true "data"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /{{.PackageName}}/{{.BusinessName}} [post]
// @Security X-TOKEN
// Insert{{.ClassName}} 添加{{.FunctionName}}
func (p *{{.ClassName}}Api) Insert{{.ClassName}}(rc *restfulx.ReqCtx) {
var data entity.{{.ClassName}}
restfulx.BindJsonAndValid(rc.GinCtx, &data)
restfulx.BindQuery(rc, &data)
p.{{.ClassName}}App.Insert(data)
}
// @Summary 修改{{.FunctionName}}
// @Description 获取JSON
// @Tags {{.FunctionName}}
// @Accept application/json
// @Product application/json
// @Param data body entity.{{.ClassName}} true "body"
// @Success 200 {string} string "{"code": 200, "message": "添加成功"}"
// @Success 200 {string} string "{"code": 400, "message": "添加失败"}"
// @Router /{{.PackageName}}/{{.BusinessName}} [put]
// @Security X-TOKEN
// Update{{.ClassName}} 修改{{.FunctionName}}
func (p *{{.ClassName}}Api) Update{{.ClassName}}(rc *restfulx.ReqCtx) {
var data entity.{{.ClassName}}
restfulx.BindJsonAndValid(rc.GinCtx, &data)
restfulx.BindQuery(rc, &data)
p.{{.ClassName}}App.Update(data)
}
// @Summary 删除{{.FunctionName}}
// @Description 删除数据
// @Tags {{.FunctionName}}
// @Param {{.PkJsonField}} path string true "{{.PkJsonField}}"
// @Success 200 {string} string "{"code": 200, "message": "删除成功"}"
// @Success 200 {string} string "{"code": 400, "message": "删除失败"}"
// @Router /{{.PackageName}}/{{.BusinessName}}/{{"{"}}{{.PkJsonField}}{{"}"}} [delete]
// Delete{{.ClassName}} 删除{{.FunctionName}}
func (p *{{.ClassName}}Api) Delete{{.ClassName}}(rc *restfulx.ReqCtx) {
{{.PkJsonField}} := rc.GinCtx.Param("{{.PkJsonField}}")
{{.PkJsonField}} := restfulx.PathParam("{{.PkJsonField}}")
{{.PkJsonField}}s := utils.IdsStrToIdsIntGroup({{.PkJsonField}})
p.{{.ClassName}}App.Delete({{.PkJsonField}}s)
}