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.2 kB

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