c_organization.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package controllers
  2. import (
  3. "github.com/gogf/gf/v2/net/ghttp"
  4. "yx-dataset-server/app/bll"
  5. "yx-dataset-server/app/errors"
  6. "yx-dataset-server/app/schema"
  7. "yx-dataset-server/library/gplus"
  8. )
  9. type Organization struct {
  10. OrganizationBll bll.IOrganization
  11. }
  12. func NewOrganization(bOrganization bll.IOrganization) *Organization {
  13. return &Organization{
  14. OrganizationBll: bOrganization,
  15. }
  16. }
  17. // Query 查询数据
  18. func (a *Organization) Query(r *ghttp.Request) {
  19. switch r.Get("q").String() {
  20. case "page":
  21. a.QueryPage(r)
  22. case "list":
  23. a.QueryList(r)
  24. default:
  25. ResError(r, errors.ErrUnknownQuery)
  26. }
  27. }
  28. // QueryPage 查询分页数据
  29. // @Tags
  30. // @Summary 查询分页数据
  31. // @Param Authorization header string false "Bearer 用户令牌"
  32. // @Param current query int true "分页索引" 1
  33. // @Param pageSize query int true "分页大小" 10
  34. // @Success 200 []schema.Organization "查询结果:{list:列表数据,pagination:{current:页索引,pageSize:页大小,total:总数量}}"
  35. // @Failure 400 schema.HTTPError "{error:{code:0,message:未知的查询类型}}"
  36. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  37. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  38. // @Router GET /web/v1/organizations?q=page
  39. func (a *Organization) QueryPage(r *ghttp.Request) {
  40. ctx := gplus.NewContext(r)
  41. var params schema.OrganizationQueryParam
  42. params.LikeName = r.Get("like_name").String()
  43. result, err := a.OrganizationBll.Query(ctx, params, schema.OrganizationQueryOptions{
  44. PageParam: &schema.PaginationParam{
  45. PageIndex: gplus.GetPageIndex(r),
  46. PageSize: gplus.GetPageSize(r),
  47. },
  48. })
  49. if err != nil {
  50. ResError(r, err)
  51. }
  52. ResPage(r, result.Data, result.PageResult)
  53. }
  54. // QueryList 查询列表数据
  55. // @Tags
  56. // @Summary 查询列表数据
  57. // @Param Authorization header string false "Bearer 用户令牌"
  58. // @Success 200 []schema.Organization "查询结果:{list:列表数据}"
  59. // @Failure 400 schema.HTTPError "{error:{code:0,message:未知的查询类型}}"
  60. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  61. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  62. // @Router GET /web/v1/organizations?q=list
  63. func (a *Organization) QueryList(r *ghttp.Request) {
  64. ctx := gplus.NewContext(r)
  65. var params schema.OrganizationQueryParam
  66. params.LikeName = r.Get("like_name").String()
  67. result, err := a.OrganizationBll.Query(ctx, params)
  68. if err != nil {
  69. ResError(r, err)
  70. }
  71. ResSuccess(r, result.Data)
  72. }
  73. // Get 获取数据
  74. // @Summary 获取数据
  75. // @Param id path string true "记录ID"
  76. // @Success 200 schema.Organization
  77. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  78. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  79. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  80. // @Router /web/v1/organizations/{id}
  81. func (a *Organization) Get(r *ghttp.Request) {
  82. ctx := gplus.NewContext(r)
  83. result, err := a.OrganizationBll.Get(ctx, r.Get("id").String())
  84. if err != nil {
  85. ResError(r, err)
  86. }
  87. ResSuccess(r, result)
  88. }
  89. // Create 创建数据
  90. // @Tags API-Organization
  91. // @Summary 创建数据
  92. // @Param body schema.Organization true "提交的数据"
  93. // @Success 200 schema.Organization
  94. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  95. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  96. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  97. // @Router /web/v1/organizations/ [post]
  98. func (a *Organization) Create(r *ghttp.Request) {
  99. var data schema.Organization
  100. if err := gplus.ParseJson(r, &data); err != nil {
  101. gplus.ResError(r, err)
  102. }
  103. ctx := gplus.NewContext(r)
  104. err := a.OrganizationBll.Create(ctx, data)
  105. if err != nil {
  106. ResError(r, err)
  107. }
  108. ResSuccess(r, nil)
  109. }
  110. // Update 更新数据
  111. // @Summary 更新数据
  112. // @Param body schema.Organization true "更新数据"
  113. // @Success 200 schema.Organization
  114. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  115. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  116. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  117. // @Router /web/v1/organizations/{id}
  118. func (a *Organization) Update(r *ghttp.Request) {
  119. var data schema.Organization
  120. if err := gplus.ParseJson(r, &data); err != nil {
  121. gplus.ResError(r, err)
  122. }
  123. ctx := gplus.NewContext(r)
  124. err := a.OrganizationBll.Update(ctx, r.Get("id").String(), data)
  125. if err != nil {
  126. ResError(r, err)
  127. }
  128. ResSuccess(r, nil)
  129. }
  130. // Delete 删除数据
  131. // @Summary 删除数据
  132. // @Param id path string true "记录ID"
  133. // @Success 200 schema.HTTPStatus "{status:OK}"
  134. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  135. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  136. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  137. // @Router /web/v1/organizations/{id}
  138. func (a *Organization) Delete(r *ghttp.Request) {
  139. ctx := gplus.NewContext(r)
  140. err := a.OrganizationBll.Delete(ctx, r.Get("id").String())
  141. if err != nil {
  142. ResError(r, err)
  143. }
  144. ResSuccess(r, nil)
  145. }