c_dataset.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 Dataset struct {
  10. DatasetBll bll.IDataset
  11. }
  12. func NewDataset(bDataset bll.IDataset) *Dataset {
  13. return &Dataset{
  14. DatasetBll: bDataset,
  15. }
  16. }
  17. // Query 查询数据
  18. func (a *Dataset) 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. gplus.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.Dataset "查询结果:{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/datasets?q=page
  39. func (a *Dataset) QueryPage(r *ghttp.Request) {
  40. ctx := gplus.NewContext(r)
  41. var params schema.DatasetQueryParam
  42. params.OrgId = r.Get("org_id").String()
  43. params.LikeName = r.Get("like_name").String()
  44. result, err := a.DatasetBll.Query(ctx, params, schema.DatasetQueryOptions{
  45. PageParam: &schema.PaginationParam{
  46. PageIndex: gplus.GetPageIndex(r),
  47. PageSize: gplus.GetPageSize(r),
  48. },
  49. })
  50. if err != nil {
  51. ResError(r, err)
  52. }
  53. ResPage(r, result.Data, result.PageResult)
  54. }
  55. // QueryList 查询列表数据
  56. // @Tags
  57. // @Summary 查询列表数据
  58. // @Param Authorization header string false "Bearer 用户令牌"
  59. // @Success 200 []schema.Dataset "查询结果:{list:列表数据}"
  60. // @Failure 400 schema.HTTPError "{error:{code:0,message:未知的查询类型}}"
  61. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  62. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  63. // @Router GET /web/v1/dataset?q=list
  64. func (a *Dataset) QueryList(r *ghttp.Request) {
  65. ctx := gplus.NewContext(r)
  66. var params schema.DatasetQueryParam
  67. params.OrgId = r.Get("org_id").String()
  68. params.LikeName = r.Get("like_name").String()
  69. result, err := a.DatasetBll.Query(ctx, params)
  70. if err != nil {
  71. gplus.ResError(r, err)
  72. }
  73. ResSuccess(r, result.Data)
  74. }
  75. // Get 获取数据
  76. // @Summary 获取数据
  77. // @Param id path string true "记录ID"
  78. // @Success 200 schema.Dataset
  79. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  80. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  81. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  82. // @Router /web/v1/dataset/{id}
  83. func (a *Dataset) Get(r *ghttp.Request) {
  84. ctx := gplus.NewContext(r)
  85. result, err := a.DatasetBll.Get(ctx, r.Get("id").String())
  86. if err != nil {
  87. gplus.ResError(r, err)
  88. }
  89. ResSuccess(r, result)
  90. }
  91. // Create 创建数据
  92. // @Tags API-Dataset
  93. // @Summary 创建数据
  94. // @Param body schema.Dataset true "提交的数据"
  95. // @Success 200 schema.Dataset
  96. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  97. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  98. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  99. // @Router /web/v1/dataset/ [post]
  100. func (a *Dataset) Create(r *ghttp.Request) {
  101. var data schema.Dataset
  102. if err := gplus.ParseJson(r, &data); err != nil {
  103. gplus.ResError(r, err)
  104. }
  105. ctx := gplus.NewContext(r)
  106. err := a.DatasetBll.Create(ctx, data)
  107. if err != nil {
  108. gplus.ResError(r, err)
  109. }
  110. ResSuccess(r, nil)
  111. }
  112. // Update 更新数据
  113. // @Summary 更新数据
  114. // @Param body schema.Dataset true "更新数据"
  115. // @Success 200 schema.Dataset
  116. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  117. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  118. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  119. // @Router /web/v1/datasets/{id}
  120. func (a *Dataset) Update(r *ghttp.Request) {
  121. var data schema.Dataset
  122. if err := gplus.ParseJson(r, &data); err != nil {
  123. gplus.ResError(r, err)
  124. }
  125. ctx := gplus.NewContext(r)
  126. err := a.DatasetBll.Update(ctx, r.Get("id").String(), data)
  127. if err != nil {
  128. gplus.ResError(r, err)
  129. }
  130. ResSuccess(r, nil)
  131. }
  132. // Delete 删除数据
  133. // @Summary 删除数据
  134. // @Param id path string true "记录ID"
  135. // @Success 200 schema.HTTPStatus "{status:OK}"
  136. // @Failure 400 schema.HTTPError "{error:{code:0,message:无效的请求参数}}"
  137. // @Failure 401 schema.HTTPError "{error:{code:0,message:未授权}}"
  138. // @Failure 500 schema.HTTPError "{error:{code:0,message:服务器错误}}"
  139. // @Router /web/v1/datasets/{id}
  140. func (a *Dataset) Delete(r *ghttp.Request) {
  141. ctx := gplus.NewContext(r)
  142. err := a.DatasetBll.Delete(ctx, r.Get("id").String())
  143. if err != nil {
  144. gplus.ResError(r, err)
  145. }
  146. ResSuccess(r, nil)
  147. }