mirror of
https://gitee.com/XM-GO/PandaX.git
synced 2026-04-23 02:48:34 +08:00
[优化]restful
This commit is contained in:
@@ -78,4 +78,6 @@ func InitRoleRouter(container *restful.Container) {
|
||||
}).
|
||||
Doc("导出角色信息").
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags))
|
||||
|
||||
container.Add(ws)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -6,41 +6,63 @@
|
||||
package router
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"pandax/apps/{{.PackageName}}/api"
|
||||
"pandax/apps/{{.PackageName}}/services"
|
||||
"github.com/XM-GO/PandaKit/restfulx"
|
||||
|
||||
restfulspec "github.com/emicklei/go-restful-openapi/v2"
|
||||
"github.com/emicklei/go-restful/v3"
|
||||
)
|
||||
|
||||
func Init{{.ClassName}}Router(router *gin.RouterGroup) {
|
||||
func Init{{.ClassName}}Router(container *restful.Container) {
|
||||
s := &api.{{.ClassName}}Api{
|
||||
{{.ClassName}}App: services.{{.ClassName}}ModelDao,
|
||||
}
|
||||
routerGroup := router.Group("{{.BusinessName}}")
|
||||
|
||||
{{.ClassName}}ListLog := restfulx.NewLogInfo("获取{{.FunctionName}}分页列表")
|
||||
routerGroup.GET("list", func(c *gin.Context) {
|
||||
restfulx.NewReqCtx(c).WithLog({{.ClassName}}ListLog).Handle(s.Get{{.ClassName}}List)
|
||||
})
|
||||
ws := new(restful.WebService)
|
||||
ws.Path("/{{.PackageName}}/{{.BusinessName}}").Produces(restful.MIME_JSON)
|
||||
tags := []string{"{{.BusinessName}}"}
|
||||
|
||||
{{.ClassName}}Log := restfulx.NewLogInfo("获取{{.FunctionName}}信息")
|
||||
routerGroup.GET(":{{.PkJsonField}}", func(c *gin.Context) {
|
||||
restfulx.NewReqCtx(c).WithLog({{.ClassName}}Log).Handle(s.Get{{.ClassName}})
|
||||
})
|
||||
|
||||
insert{{.ClassName}}Log := restfulx.NewLogInfo("添加{{.FunctionName}}信息")
|
||||
routerGroup.POST("", func(c *gin.Context) {
|
||||
restfulx.NewReqCtx(c).WithLog(insert{{.ClassName}}Log).Handle(s.Insert{{.ClassName}})
|
||||
})
|
||||
ws.Route(ws.GET("/list").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithLog("获取{{.FunctionName}}分页列表").Handle(s.Get{{.ClassName}}List)
|
||||
}).
|
||||
Doc("获取{{.FunctionName}}分页列表").
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Writes([]entity.{{.ClassName}}{}).
|
||||
Returns(200, "OK", []entity.{{.ClassName}}{}))
|
||||
|
||||
update{{.ClassName}}Log := restfulx.NewLogInfo("修改{{.FunctionName}}信息")
|
||||
routerGroup.PUT("", func(c *gin.Context) {
|
||||
restfulx.NewReqCtx(c).WithLog(update{{.ClassName}}Log).Handle(s.Update{{.ClassName}})
|
||||
})
|
||||
ws.Route(ws.GET("/{{"{"}}{{.PkJsonField}}{{"}"}}").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithLog("获取{{.FunctionName}}信息").Handle(s.Get{{.ClassName}})
|
||||
}).
|
||||
Doc("获取{{.FunctionName}}信息").
|
||||
Param(ws.PathParameter("{{.PkJsonField}}", "Id").DataType("int")).
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Writes(entity.{{.ClassName}}{}). // on the response
|
||||
Returns(200, "OK", entity.{{.ClassName}}{}).
|
||||
Returns(404, "Not Found", nil))
|
||||
|
||||
delete{{.ClassName}}Log := restfulx.NewLogInfo("删除{{.FunctionName}}信息")
|
||||
routerGroup.DELETE(":{{.PkJsonField}}", func(c *gin.Context) {
|
||||
restfulx.NewReqCtx(c).WithLog(delete{{.ClassName}}Log).Handle(s.Delete{{.ClassName}})
|
||||
})
|
||||
ws.Route(ws.POST("").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithLog("添加{{.FunctionName}}信息").Handle(s.Insert{{.ClassName}})
|
||||
}).
|
||||
Doc("添加{{.FunctionName}}信息").
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Reads(entity.{{.ClassName}}{}))
|
||||
|
||||
ws.Route(ws.PUT("").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithLog("修改{{.FunctionName}}信息").Handle(s.Update{{.ClassName}})
|
||||
}).
|
||||
Doc("修改{{.FunctionName}}信息").
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Reads(entity.{{.ClassName}}{}))
|
||||
|
||||
ws.Route(ws.DELETE("/{{"{"}}{{.PkJsonField}}{{"}"}}").To(func(request *restful.Request, response *restful.Response) {
|
||||
restfulx.NewReqCtx(request, response).WithLog("删除{{.FunctionName}}信息").Handle(s.Delete{{.ClassName}})
|
||||
}).
|
||||
Doc("删除{{.FunctionName}}信息").
|
||||
Metadata(restfulspec.KeyOpenAPITags, tags).
|
||||
Param(ws.PathParameter("{{.PkJsonField}}", "多id 1,2,3").DataType("string")))
|
||||
|
||||
container.Add(ws)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user