You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

datasets.go 3.4 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package repo
  2. import (
  3. "fmt"
  4. "strings"
  5. "code.gitea.io/gitea/modules/convert"
  6. api "code.gitea.io/gitea/modules/structs"
  7. "code.gitea.io/gitea/models"
  8. "code.gitea.io/gitea/modules/context"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. func PublicDatasetMultiple(ctx *context.APIContext) {
  13. opts := &models.SearchDatasetOptions{
  14. PublicOnly: true,
  15. NeedAttachment: true,
  16. CloudBrainType: ctx.QueryInt("type"),
  17. }
  18. datasetMultiple(ctx, opts)
  19. }
  20. func MyFavoriteDatasetMultiple(ctx *context.APIContext) {
  21. opts := &models.SearchDatasetOptions{
  22. StarByMe: true,
  23. DatasetIDs: models.GetDatasetIdsStarByUser(ctx.User.ID),
  24. NeedAttachment: true,
  25. CloudBrainType: ctx.QueryInt("type"),
  26. }
  27. datasetMultiple(ctx, opts)
  28. }
  29. func CurrentRepoDatasetMultiple(ctx *context.APIContext) {
  30. datasetIds := models.GetDatasetIdsByRepoID(ctx.Repo.Repository.ID)
  31. searchOrderBy := getSearchOrderByInValues(datasetIds)
  32. opts := &models.SearchDatasetOptions{
  33. RepoID: ctx.Repo.Repository.ID,
  34. NeedAttachment: true,
  35. CloudBrainType: ctx.QueryInt("type"),
  36. DatasetIDs: datasetIds,
  37. SearchOrderBy: searchOrderBy,
  38. }
  39. datasetMultiple(ctx, opts)
  40. }
  41. func CurrentRepoDatasetInfoWithoutAttachment(ctx *context.APIContext) {
  42. dataset, err := models.GetDatasetByRepo(ctx.Repo.Repository)
  43. if err != nil {
  44. log.Warn("can not get dataset.", err)
  45. ctx.JSON(200, map[string]interface{}{
  46. "code": 0,
  47. "message": "",
  48. "data": []*api.Dataset{},
  49. })
  50. return
  51. }
  52. dataset.Repo = ctx.Repo.Repository
  53. ctx.JSON(200, map[string]interface{}{
  54. "code": 0,
  55. "message": "",
  56. "data": []*api.Dataset{convert.ToDataset(dataset)},
  57. })
  58. }
  59. func MyDatasetsMultiple(ctx *context.APIContext) {
  60. opts := &models.SearchDatasetOptions{
  61. UploadAttachmentByMe: true,
  62. NeedAttachment: true,
  63. CloudBrainType: ctx.QueryInt("type"),
  64. }
  65. datasetMultiple(ctx, opts)
  66. }
  67. func datasetMultiple(ctx *context.APIContext, opts *models.SearchDatasetOptions) {
  68. page := ctx.QueryInt("page")
  69. if page < 1 {
  70. page = 1
  71. }
  72. pageSize := ctx.QueryInt("pageSize")
  73. if pageSize < 1 {
  74. pageSize = setting.UI.DatasetPagingNum
  75. }
  76. keyword := strings.Trim(ctx.Query("q"), " ")
  77. opts.Keyword = keyword
  78. if opts.SearchOrderBy.String() == "" {
  79. opts.SearchOrderBy = models.SearchOrderByRecentUpdated
  80. }
  81. opts.RecommendOnly = ctx.QueryBool("recommend")
  82. opts.ListOptions = models.ListOptions{
  83. Page: page,
  84. PageSize: pageSize,
  85. }
  86. opts.JustNeedZipFile = true
  87. opts.User = ctx.User
  88. datasets, count, err := models.SearchDataset(opts)
  89. if err != nil {
  90. log.Error("json.Marshal failed:", err.Error())
  91. ctx.JSON(200, map[string]interface{}{
  92. "code": 1,
  93. "message": err.Error(),
  94. "data": []*api.Dataset{},
  95. "count": 0,
  96. })
  97. return
  98. }
  99. var convertDatasets []*api.Dataset
  100. for _, dataset := range datasets {
  101. convertDatasets = append(convertDatasets, convert.ToDataset(dataset))
  102. }
  103. ctx.JSON(200, map[string]interface{}{
  104. "code": 0,
  105. "message": "",
  106. "data": convertDatasets,
  107. "count": count,
  108. })
  109. }
  110. func getSearchOrderByInValues(datasetIds []int64) models.SearchOrderBy {
  111. if len(datasetIds) == 0 {
  112. return ""
  113. }
  114. searchOrderBy := "CASE id "
  115. for i, id := range datasetIds {
  116. searchOrderBy += fmt.Sprintf(" WHEN %d THEN %d", id, i+1)
  117. }
  118. searchOrderBy += " ELSE 0 END"
  119. return models.SearchOrderBy(searchOrderBy)
  120. }