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.

dataset.go 2.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package models
  2. import (
  3. "fmt"
  4. "code.gitea.io/gitea/modules/timeutil"
  5. "xorm.io/builder"
  6. )
  7. const (
  8. DatasetStatusPrivate int = iota
  9. DatasetStatusPublic
  10. DatasetStatusDeleted
  11. )
  12. type DatasetList []*Dataset
  13. type SearchDatasetOptions struct {
  14. ListOptions
  15. Keyword string
  16. OwnerID int64
  17. IsPublic bool
  18. }
  19. type Dataset struct {
  20. ID int64 `xorm:"pk autoincr"`
  21. Title string `xorm:"INDEX NOT NULL"`
  22. Status int32 `xorm:"INDEX"` // normal_private: 0, pulbic: 1, is_delete: 2
  23. Category string
  24. Description string `xorm:"TEXT"`
  25. DownloadTimes int64
  26. License string
  27. Task string
  28. ReleaseID int64 `xorm:"INDEX"`
  29. UserID int64 `xorm:"INDEX"`
  30. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  31. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  32. Attachments []*Attachment `xorm:"-"`
  33. }
  34. func CreateDataset(dataset *Dataset) (err error) {
  35. if _, err = x.Insert(dataset); err != nil {
  36. return err
  37. }
  38. return nil
  39. }
  40. // AddReleaseAttachments adds a release attachments
  41. func AddDatasetAttachments(DatasetID int64, attachmentUUIDs []string) (err error) {
  42. // Check attachments
  43. attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs)
  44. if err != nil {
  45. return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err)
  46. }
  47. for i := range attachments {
  48. attachments[i].DatasetID = DatasetID
  49. // No assign value could be 0, so ignore AllCols().
  50. if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil {
  51. return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err)
  52. }
  53. }
  54. return
  55. }
  56. func SearchDataset(opts *SearchDatasetOptions) (DatasetList, int64, error) {
  57. cond := SearchDatasetCondition(opts)
  58. return SearchDatasetByCondition(opts, cond)
  59. }
  60. func SearchDatasetCondition(opts *SearchDatasetOptions) builder.Cond {
  61. var cond = builder.NewCond()
  62. cond = cond.And(builder.Eq{"user_id": opts.OwnerID})
  63. if opts.IsPublic {
  64. cond = cond.And(builder.Eq{"status": DatasetStatusPublic})
  65. } else {
  66. cond = cond.And(builder.Neq{"status": DatasetStatusDeleted})
  67. }
  68. return cond
  69. }
  70. func SearchDatasetByCondition(opts *SearchDatasetOptions, cond builder.Cond) (DatasetList, int64, error) {
  71. if opts.Page <= 0 {
  72. opts.Page = 1
  73. }
  74. var err error
  75. sess := x.NewSession()
  76. defer sess.Close()
  77. // count, err := sess.Where(cond).Count(new(DatasetList))
  78. // if err != nil {
  79. // return nil, 0, fmt.Errorf("Count: %v", err)
  80. // }
  81. repos := make(DatasetList, 0, opts.PageSize)
  82. sess.Where(cond)
  83. if opts.PageSize > 0 {
  84. sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  85. }
  86. if err = sess.Find(&repos); err != nil {
  87. return nil, 0, fmt.Errorf("Dataset: %v", err)
  88. }
  89. return repos, 0, nil
  90. }