api.go.tpl 4.8 KB

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