api.go.tpl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package autocode
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
  5. "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
  6. autocodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
  7. "github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
  8. "github.com/flipped-aurora/gin-vue-admin/server/service"
  9. "github.com/gin-gonic/gin"
  10. "go.uber.org/zap"
  11. )
  12. type {{.StructName}}Api struct {
  13. }
  14. var {{.Abbreviation}}Service = service.ServiceGroupApp.AutoCodeServiceGroup.{{.StructName}}Service
  15. // Create{{.StructName}} 创建{{.StructName}}
  16. // @Tags {{.StructName}}
  17. // @Summary 创建{{.StructName}}
  18. // @Security ApiKeyAuth
  19. // @accept application/json
  20. // @Produce application/json
  21. // @Param data body autocode.{{.StructName}} true "创建{{.StructName}}"
  22. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  23. // @Router /{{.Abbreviation}}/create{{.StructName}} [post]
  24. func ({{.Abbreviation}}Api *{{.StructName}}Api) Create{{.StructName}}(c *gin.Context) {
  25. var {{.Abbreviation}} autocode.{{.StructName}}
  26. _ = c.ShouldBindJSON(&{{.Abbreviation}})
  27. if err := {{.Abbreviation}}Service.Create{{.StructName}}({{.Abbreviation}}); err != nil {
  28. global.GVA_LOG.Error("创建失败!", zap.Any("err", err))
  29. response.FailWithMessage("创建失败", c)
  30. } else {
  31. response.OkWithMessage("创建成功", c)
  32. }
  33. }
  34. // Delete{{.StructName}} 删除{{.StructName}}
  35. // @Tags {{.StructName}}
  36. // @Summary 删除{{.StructName}}
  37. // @Security ApiKeyAuth
  38. // @accept application/json
  39. // @Produce application/json
  40. // @Param data body autocode.{{.StructName}} true "删除{{.StructName}}"
  41. // @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
  42. // @Router /{{.Abbreviation}}/delete{{.StructName}} [delete]
  43. func ({{.Abbreviation}}Api *{{.StructName}}Api) Delete{{.StructName}}(c *gin.Context) {
  44. var {{.Abbreviation}} autocode.{{.StructName}}
  45. _ = c.ShouldBindJSON(&{{.Abbreviation}})
  46. if err := {{.Abbreviation}}Service.Delete{{.StructName}}({{.Abbreviation}}); err != nil {
  47. global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
  48. response.FailWithMessage("删除失败", c)
  49. } else {
  50. response.OkWithMessage("删除成功", c)
  51. }
  52. }
  53. // Delete{{.StructName}}ByIds 批量删除{{.StructName}}
  54. // @Tags {{.StructName}}
  55. // @Summary 批量删除{{.StructName}}
  56. // @Security ApiKeyAuth
  57. // @accept application/json
  58. // @Produce application/json
  59. // @Param data body request.IdsReq true "批量删除{{.StructName}}"
  60. // @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
  61. // @Router /{{.Abbreviation}}/delete{{.StructName}}ByIds [delete]
  62. func ({{.Abbreviation}}Api *{{.StructName}}Api) Delete{{.StructName}}ByIds(c *gin.Context) {
  63. var IDS request.IdsReq
  64. _ = c.ShouldBindJSON(&IDS)
  65. if err := {{.Abbreviation}}Service.Delete{{.StructName}}ByIds(IDS); err != nil {
  66. global.GVA_LOG.Error("批量删除失败!", zap.Any("err", err))
  67. response.FailWithMessage("批量删除失败", c)
  68. } else {
  69. response.OkWithMessage("批量删除成功", c)
  70. }
  71. }
  72. // Update{{.StructName}} 更新{{.StructName}}
  73. // @Tags {{.StructName}}
  74. // @Summary 更新{{.StructName}}
  75. // @Security ApiKeyAuth
  76. // @accept application/json
  77. // @Produce application/json
  78. // @Param data body autocode.{{.StructName}} true "更新{{.StructName}}"
  79. // @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
  80. // @Router /{{.Abbreviation}}/update{{.StructName}} [put]
  81. func ({{.Abbreviation}}Api *{{.StructName}}Api) Update{{.StructName}}(c *gin.Context) {
  82. var {{.Abbreviation}} autocode.{{.StructName}}
  83. _ = c.ShouldBindJSON(&{{.Abbreviation}})
  84. if err := {{.Abbreviation}}Service.Update{{.StructName}}({{.Abbreviation}}); err != nil {
  85. global.GVA_LOG.Error("更新失败!", zap.Any("err", err))
  86. response.FailWithMessage("更新失败", c)
  87. } else {
  88. response.OkWithMessage("更新成功", c)
  89. }
  90. }
  91. // Find{{.StructName}} 用id查询{{.StructName}}
  92. // @Tags {{.StructName}}
  93. // @Summary 用id查询{{.StructName}}
  94. // @Security ApiKeyAuth
  95. // @accept application/json
  96. // @Produce application/json
  97. // @Param data query autocode.{{.StructName}} true "用id查询{{.StructName}}"
  98. // @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
  99. // @Router /{{.Abbreviation}}/find{{.StructName}} [get]
  100. func ({{.Abbreviation}}Api *{{.StructName}}Api) Find{{.StructName}}(c *gin.Context) {
  101. var {{.Abbreviation}} autocode.{{.StructName}}
  102. _ = c.ShouldBindQuery(&{{.Abbreviation}})
  103. if err, re{{.Abbreviation}} := {{.Abbreviation}}Service.Get{{.StructName}}({{.Abbreviation}}.ID); err != nil {
  104. global.GVA_LOG.Error("查询失败!", zap.Any("err", err))
  105. response.FailWithMessage("查询失败", c)
  106. } else {
  107. response.OkWithData(gin.H{"re{{.Abbreviation}}": re{{.Abbreviation}}}, c)
  108. }
  109. }
  110. // Get{{.StructName}}List 分页获取{{.StructName}}列表
  111. // @Tags {{.StructName}}
  112. // @Summary 分页获取{{.StructName}}列表
  113. // @Security ApiKeyAuth
  114. // @accept application/json
  115. // @Produce application/json
  116. // @Param data query autocodeReq.{{.StructName}}Search true "分页获取{{.StructName}}列表"
  117. // @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
  118. // @Router /{{.Abbreviation}}/get{{.StructName}}List [get]
  119. func ({{.Abbreviation}}Api *{{.StructName}}Api) Get{{.StructName}}List(c *gin.Context) {
  120. var pageInfo autocodeReq.{{.StructName}}Search
  121. _ = c.ShouldBindQuery(&pageInfo)
  122. if err, list, total := {{.Abbreviation}}Service.Get{{.StructName}}InfoList(pageInfo); err != nil {
  123. global.GVA_LOG.Error("获取失败!", zap.Any("err", err))
  124. response.FailWithMessage("获取失败", c)
  125. } else {
  126. response.OkWithDetailed(response.PageResult{
  127. List: list,
  128. Total: total,
  129. Page: pageInfo.Page,
  130. PageSize: pageInfo.PageSize,
  131. }, "获取成功", c)
  132. }
  133. }