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 2.9 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 MyDatasetsMultiple(ctx *context.APIContext) {
  42. opts := &models.SearchDatasetOptions{
  43. UploadAttachmentByMe: true,
  44. NeedAttachment: true,
  45. CloudBrainType: ctx.QueryInt("type"),
  46. }
  47. datasetMultiple(ctx, opts)
  48. }
  49. func datasetMultiple(ctx *context.APIContext, opts *models.SearchDatasetOptions) {
  50. page := ctx.QueryInt("page")
  51. if page < 1 {
  52. page = 1
  53. }
  54. pageSize := ctx.QueryInt("pageSize")
  55. if pageSize < 1 {
  56. pageSize = setting.UI.DatasetPagingNum
  57. }
  58. keyword := strings.Trim(ctx.Query("q"), " ")
  59. opts.Keyword = keyword
  60. if opts.SearchOrderBy.String() == "" {
  61. opts.SearchOrderBy = models.SearchOrderByRecentUpdated
  62. }
  63. opts.RecommendOnly = ctx.QueryBool("recommend")
  64. opts.ListOptions = models.ListOptions{
  65. Page: page,
  66. PageSize: pageSize,
  67. }
  68. opts.JustNeedZipFile = true
  69. opts.User = ctx.User
  70. datasets, count, err := models.SearchDataset(opts)
  71. if err != nil {
  72. log.Error("json.Marshal failed:", err.Error())
  73. ctx.JSON(200, map[string]interface{}{
  74. "code": 1,
  75. "message": err.Error(),
  76. "data": []*api.Dataset{},
  77. "count": 0,
  78. })
  79. return
  80. }
  81. var convertDatasets []*api.Dataset
  82. for _, dataset := range datasets {
  83. convertDatasets = append(convertDatasets, convert.ToDataset(dataset))
  84. }
  85. ctx.JSON(200, map[string]interface{}{
  86. "code": 0,
  87. "message": "",
  88. "data": convertDatasets,
  89. "count": count,
  90. })
  91. }
  92. func getSearchOrderByInValues(datasetIds []int64) models.SearchOrderBy {
  93. if len(datasetIds) == 0 {
  94. return ""
  95. }
  96. searchOrderBy := "CASE id "
  97. for i, id := range datasetIds {
  98. searchOrderBy += fmt.Sprintf(" WHEN %d THEN %d", id, i+1)
  99. }
  100. searchOrderBy += " ELSE 0 END"
  101. return models.SearchOrderBy(searchOrderBy)
  102. }