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 3.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. // AddDatasetAttachments adds a Dataset 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. // GetDatasetByID returns Dataset with given ID.
  57. func GetDatasetByID(id int64) (*Dataset, error) {
  58. rel := new(Dataset)
  59. has, err := x.
  60. ID(id).
  61. Get(rel)
  62. if err != nil {
  63. return nil, err
  64. } else if !has {
  65. return nil, ErrDatasetNotExist{id}
  66. }
  67. return rel, nil
  68. }
  69. func UpdateDataset(ctx DBContext, rel *Dataset) error {
  70. _, err := ctx.e.ID(rel.ID).AllCols().Update(rel)
  71. return err
  72. }
  73. func SearchDataset(opts *SearchDatasetOptions) (DatasetList, int64, error) {
  74. cond := SearchDatasetCondition(opts)
  75. return SearchDatasetByCondition(opts, cond)
  76. }
  77. func SearchDatasetCondition(opts *SearchDatasetOptions) builder.Cond {
  78. var cond = builder.NewCond()
  79. cond = cond.And(builder.Eq{"user_id": opts.OwnerID})
  80. if opts.IsPublic {
  81. cond = cond.And(builder.Eq{"status": DatasetStatusPublic})
  82. } else {
  83. cond = cond.And(builder.Neq{"status": DatasetStatusDeleted})
  84. }
  85. return cond
  86. }
  87. func SearchDatasetByCondition(opts *SearchDatasetOptions, cond builder.Cond) (DatasetList, int64, error) {
  88. if opts.Page <= 0 {
  89. opts.Page = 1
  90. }
  91. var err error
  92. sess := x.NewSession()
  93. defer sess.Close()
  94. // count, err := sess.Where(cond).Count(new(DatasetList))
  95. // if err != nil {
  96. // return nil, 0, fmt.Errorf("Count: %v", err)
  97. // }
  98. repos := make(DatasetList, 0, opts.PageSize)
  99. sess.Where(cond)
  100. if opts.PageSize > 0 {
  101. sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  102. }
  103. if err = sess.Find(&repos); err != nil {
  104. return nil, 0, fmt.Errorf("Dataset: %v", err)
  105. }
  106. return repos, 0, nil
  107. }