apis.go.template 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package apis
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/go-admin-team/go-admin-core/sdk/api"
  6. "github.com/go-admin-team/go-admin-core/sdk/pkg/jwtauth/user"
  7. _ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
  8. "go-admin/app/{{.PackageName}}/models"
  9. "go-admin/app/{{.PackageName}}/service"
  10. "go-admin/app/{{.PackageName}}/service/dto"
  11. "go-admin/common/actions"
  12. )
  13. type {{.ClassName}} struct {
  14. api.Api
  15. }
  16. // GetPage 获取{{.TableComment}}列表
  17. // @Summary 获取{{.TableComment}}列表
  18. // @Description 获取{{.TableComment}}列表
  19. // @Tags {{.TableComment}}
  20. {{- $tablename := .TBName -}}
  21. {{- range .Columns -}}
  22. {{$z := .IsQuery}}
  23. {{- if ($z) }}
  24. // @Param {{.JsonField}} query {{.GoType}} false "{{.ColumnComment}}"
  25. {{- end -}}
  26. {{- end }}
  27. // @Param pageSize query int false "页条数"
  28. // @Param pageIndex query int false "页码"
  29. // @Success 200 {object} response.Response{data=response.Page{list=[]models.{{.ClassName}}}} "{"code": 200, "data": [...]}"
  30. // @Router /api/v1/{{.ModuleName}} [get]
  31. // @Security Bearer
  32. func (e {{.ClassName}}) GetPage(c *gin.Context) {
  33. req := dto.{{.ClassName}}GetPageReq{}
  34. s := service.{{.ClassName}}{}
  35. err := e.MakeContext(c).
  36. MakeOrm().
  37. Bind(&req).
  38. MakeService(&s.Service).
  39. Errors
  40. if err != nil {
  41. e.Logger.Error(err)
  42. e.Error(500, err, err.Error())
  43. return
  44. }
  45. p := actions.GetPermissionFromContext(c)
  46. list := make([]models.{{.ClassName}}, 0)
  47. var count int64
  48. err = s.GetPage(&req, p, &list, &count)
  49. if err != nil {
  50. e.Error(500, err, fmt.Sprintf("获取{{.TableComment}}失败,\r\n失败信息 %s", err.Error()))
  51. return
  52. }
  53. e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
  54. }
  55. // Get 获取{{.TableComment}}
  56. // @Summary 获取{{.TableComment}}
  57. // @Description 获取{{.TableComment}}
  58. // @Tags {{.TableComment}}
  59. // @Param id path int false "id"
  60. // @Success 200 {object} response.Response{data=models.{{.ClassName}}} "{"code": 200, "data": [...]}"
  61. // @Router /api/v1/{{.ModuleName}}/{id} [get]
  62. // @Security Bearer
  63. func (e {{.ClassName}}) Get(c *gin.Context) {
  64. req := dto.{{.ClassName}}GetReq{}
  65. s := service.{{.ClassName}}{}
  66. err := e.MakeContext(c).
  67. MakeOrm().
  68. Bind(&req).
  69. MakeService(&s.Service).
  70. Errors
  71. if err != nil {
  72. e.Logger.Error(err)
  73. e.Error(500, err, err.Error())
  74. return
  75. }
  76. var object models.{{.ClassName}}
  77. p := actions.GetPermissionFromContext(c)
  78. err = s.Get(&req, p, &object)
  79. if err != nil {
  80. e.Error(500, err, fmt.Sprintf("获取{{.TableComment}}失败,\r\n失败信息 %s", err.Error()))
  81. return
  82. }
  83. e.OK( object, "查询成功")
  84. }
  85. // Insert 创建{{.TableComment}}
  86. // @Summary 创建{{.TableComment}}
  87. // @Description 创建{{.TableComment}}
  88. // @Tags {{.TableComment}}
  89. // @Accept application/json
  90. // @Product application/json
  91. // @Param data body dto.{{.ClassName}}InsertReq true "data"
  92. // @Success 200 {object} response.Response "{"code": 200, "message": "添加成功"}"
  93. // @Router /api/v1/{{.ModuleName}} [post]
  94. // @Security Bearer
  95. func (e {{.ClassName}}) Insert(c *gin.Context) {
  96. req := dto.{{.ClassName}}InsertReq{}
  97. s := service.{{.ClassName}}{}
  98. err := e.MakeContext(c).
  99. MakeOrm().
  100. Bind(&req).
  101. MakeService(&s.Service).
  102. Errors
  103. if err != nil {
  104. e.Logger.Error(err)
  105. e.Error(500, err, err.Error())
  106. return
  107. }
  108. // 设置创建人
  109. req.SetCreateBy(user.GetUserId(c))
  110. err = s.Insert(&req)
  111. if err != nil {
  112. e.Error(500, err, fmt.Sprintf("创建{{.TableComment}}失败,\r\n失败信息 %s", err.Error()))
  113. return
  114. }
  115. e.OK(req.GetId(), "创建成功")
  116. }
  117. // Update 修改{{.TableComment}}
  118. // @Summary 修改{{.TableComment}}
  119. // @Description 修改{{.TableComment}}
  120. // @Tags {{.TableComment}}
  121. // @Accept application/json
  122. // @Product application/json
  123. // @Param id path int true "id"
  124. // @Param data body dto.{{.ClassName}}UpdateReq true "body"
  125. // @Success 200 {object} response.Response "{"code": 200, "message": "修改成功"}"
  126. // @Router /api/v1/{{.ModuleName}}/{id} [put]
  127. // @Security Bearer
  128. func (e {{.ClassName}}) Update(c *gin.Context) {
  129. req := dto.{{.ClassName}}UpdateReq{}
  130. s := service.{{.ClassName}}{}
  131. err := e.MakeContext(c).
  132. MakeOrm().
  133. Bind(&req).
  134. MakeService(&s.Service).
  135. Errors
  136. if err != nil {
  137. e.Logger.Error(err)
  138. e.Error(500, err, err.Error())
  139. return
  140. }
  141. req.SetUpdateBy(user.GetUserId(c))
  142. p := actions.GetPermissionFromContext(c)
  143. err = s.Update(&req, p)
  144. if err != nil {
  145. e.Error(500, err, fmt.Sprintf("修改{{.TableComment}}失败,\r\n失败信息 %s", err.Error()))
  146. return
  147. }
  148. e.OK( req.GetId(), "修改成功")
  149. }
  150. // Delete 删除{{.TableComment}}
  151. // @Summary 删除{{.TableComment}}
  152. // @Description 删除{{.TableComment}}
  153. // @Tags {{.TableComment}}
  154. // @Param data body dto.{{.ClassName}}DeleteReq true "body"
  155. // @Success 200 {object} response.Response "{"code": 200, "message": "删除成功"}"
  156. // @Router /api/v1/{{.ModuleName}} [delete]
  157. // @Security Bearer
  158. func (e {{.ClassName}}) Delete(c *gin.Context) {
  159. s := service.{{.ClassName}}{}
  160. req := dto.{{.ClassName}}DeleteReq{}
  161. err := e.MakeContext(c).
  162. MakeOrm().
  163. Bind(&req).
  164. MakeService(&s.Service).
  165. Errors
  166. if err != nil {
  167. e.Logger.Error(err)
  168. e.Error(500, err, err.Error())
  169. return
  170. }
  171. // req.SetUpdateBy(user.GetUserId(c))
  172. p := actions.GetPermissionFromContext(c)
  173. err = s.Remove(&req, p)
  174. if err != nil {
  175. e.Error(500, err, fmt.Sprintf("删除{{.TableComment}}失败,\r\n失败信息 %s", err.Error()))
  176. return
  177. }
  178. e.OK( req.GetId(), "删除成功")
  179. }