diff --git a/models/dataset.go b/models/dataset.go index 9ad7cdd75..90475c6a6 100644 --- a/models/dataset.go +++ b/models/dataset.go @@ -8,20 +8,6 @@ import ( "xorm.io/builder" ) -const ( - DatasetStatusPrivate int = iota - DatasetStatusPublic - DatasetStatusDeleted -) - -type DatasetList []*Dataset -type SearchDatasetOptions struct { - ListOptions - Keyword string - OwnerID int64 - IsPublic bool -} - type Dataset struct { ID int64 `xorm:"pk autoincr"` Title string `xorm:"INDEX NOT NULL"` @@ -39,51 +25,28 @@ type Dataset struct { Attachments []*Attachment `xorm:"-"` } -func CreateDataset(dataset *Dataset) (err error) { - if _, err = x.Insert(dataset); err != nil { - return err - } - - return nil -} - -// AddDatasetAttachments adds a Dataset attachments -func AddDatasetAttachments(DatasetID int64, attachmentUUIDs []string) (err error) { - // Check attachments - attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs) - if err != nil { - return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err) - } +type DatasetList []*Dataset - for i := range attachments { - attachments[i].DatasetID = DatasetID - // No assign value could be 0, so ignore AllCols(). - if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil { - return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err) - } - } +const ( + DatasetStatusPrivate int = iota + DatasetStatusPublic + DatasetStatusDeleted +) - return +type SearchDatasetOptions struct { + Keyword string + OwnerID int64 + IsPublic bool + ListOptions + SearchOrderBy } -// GetDatasetByID returns Dataset with given ID. -func GetDatasetByID(id int64) (*Dataset, error) { - rel := new(Dataset) - has, err := x. - ID(id). - Get(rel) - if err != nil { - return nil, err - } else if !has { - return nil, ErrDatasetNotExist{id} +func CreateDataset(dataset *Dataset) (err error) { + if _, err = x.Insert(dataset); err != nil { + return err } - return rel, nil -} - -func UpdateDataset(ctx DBContext, rel *Dataset) error { - _, err := ctx.e.ID(rel.ID).AllCols().Update(rel) - return err + return nil } func SearchDataset(opts *SearchDatasetOptions) (DatasetList, int64, error) { @@ -120,7 +83,7 @@ func SearchDatasetByCondition(opts *SearchDatasetOptions, cond builder.Cond) (Da // } repos := make(DatasetList, 0, opts.PageSize) - sess.Where(cond) + sess.Where(cond).OrderBy(opts.SearchOrderBy.String()) if opts.PageSize > 0 { sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) } @@ -190,3 +153,42 @@ func geDatasetAttachments(e Engine, rels ...*Dataset) (err error) { return } + +// AddDatasetAttachments adds a Dataset attachments +func AddDatasetAttachments(DatasetID int64, attachmentUUIDs []string) (err error) { + // Check attachments + attachments, err := GetAttachmentsByUUIDs(attachmentUUIDs) + if err != nil { + return fmt.Errorf("GetAttachmentsByUUIDs [uuids: %v]: %v", attachmentUUIDs, err) + } + + for i := range attachments { + attachments[i].DatasetID = DatasetID + // No assign value could be 0, so ignore AllCols(). + if _, err = x.ID(attachments[i].ID).Update(attachments[i]); err != nil { + return fmt.Errorf("update attachment [%d]: %v", attachments[i].ID, err) + } + } + + return +} + +func UpdateDataset(ctx DBContext, rel *Dataset) error { + _, err := ctx.e.ID(rel.ID).AllCols().Update(rel) + return err +} + +// GetDatasetByID returns Dataset with given ID. +func GetDatasetByID(id int64) (*Dataset, error) { + rel := new(Dataset) + has, err := x. + ID(id). + Get(rel) + if err != nil { + return nil, err + } else if !has { + return nil, ErrDatasetNotExist{id} + } + + return rel, nil +} diff --git a/modules/auth/dataset.go b/modules/auth/dataset.go index aa57dee7e..a399e63ba 100644 --- a/modules/auth/dataset.go +++ b/modules/auth/dataset.go @@ -17,6 +17,10 @@ type CreateDatasetForm struct { Files []string } +func (f *CreateDatasetForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { + return validate(errs, ctx.Data, f, ctx.Locale) +} + type EditDatasetForm struct { ID int64 `binding:"Required"` Title string `binding:"Required"` @@ -28,8 +32,3 @@ type EditDatasetForm struct { ReleaseID int64 `xorm:"INDEX"` Files []string } - -// Validate validates the fields -func (f *CreateDatasetForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors { - return validate(errs, ctx.Data, f, ctx.Locale) -} diff --git a/routers/dataset/dataset.go b/routers/dataset/dataset.go index 0b4723a03..1dde219e4 100644 --- a/routers/dataset/dataset.go +++ b/routers/dataset/dataset.go @@ -23,20 +23,62 @@ type ListOptions struct { } func MyList(ctx *context.Context) { + + var ( + datasets []*models.Dataset + count int64 + err error + orderBy models.SearchOrderBy + ) + ctxUser := ctx.User page := ctx.QueryInt("page") if page <= 0 { page = 1 } + switch ctx.Query("sort") { + case "newest": + orderBy = models.SearchOrderByNewest + case "oldest": + orderBy = models.SearchOrderByOldest + case "recentupdate": + orderBy = models.SearchOrderByRecentUpdated + case "leastupdate": + orderBy = models.SearchOrderByLeastUpdated + case "reversealphabetically": + orderBy = models.SearchOrderByAlphabeticallyReverse + case "alphabetically": + orderBy = models.SearchOrderByAlphabetically + case "reversesize": + orderBy = models.SearchOrderBySizeReverse + case "size": + orderBy = models.SearchOrderBySize + case "moststars": + orderBy = models.SearchOrderByStarsReverse + case "feweststars": + orderBy = models.SearchOrderByStars + case "mostforks": + orderBy = models.SearchOrderByForksReverse + case "fewestforks": + orderBy = models.SearchOrderByForks + default: + ctx.Data["SortType"] = "recentupdate" + orderBy = models.SearchOrderByRecentUpdated + } datasetSearchOptions := &models.SearchDatasetOptions{ - OwnerID: ctxUser.ID, + Keyword: ctx.QueryTrim("keyword"), + OwnerID: ctxUser.ID, + IsPublic: false, + SearchOrderBy: orderBy, + ListOptions: models.ListOptions{ + Page: page, + PageSize: setting.UI.ExplorePagingNum, + }, } - var ( - datasets []*models.Dataset - count int64 - err error - ) + if len(datasetSearchOptions.SearchOrderBy) == 0 { + datasetSearchOptions.SearchOrderBy = models.SearchOrderByAlphabetically + } datasets, count, err = models.SearchDataset(datasetSearchOptions) if err != nil { diff --git a/routers/user/profile.go b/routers/user/profile.go index 215dff008..dc1d0a113 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -209,6 +209,27 @@ func Profile(ctx *context.Context) { } total = int(count) + case "datasets": + datasetSearchOptions := &models.SearchDatasetOptions{ + Keyword: keyword, + OwnerID: ctxUser.ID, + SearchOrderBy: orderBy, + ListOptions: models.ListOptions{ + Page: page, + PageSize: setting.UI.ExplorePagingNum, + }, + } + + if len(datasetSearchOptions.SearchOrderBy) == 0 { + datasetSearchOptions.SearchOrderBy = models.SearchOrderByAlphabetically + } + + datasets, count, err := models.SearchDataset(datasetSearchOptions) + if err != nil { + ctx.ServerError("SearchDatasets", err) + } + total = int(count) + ctx.Data["datasets"] = datasets default: repos, count, err = models.SearchRepository(&models.SearchRepoOptions{ ListOptions: models.ListOptions{ diff --git a/templates/datasets/dataset_list.tmpl b/templates/datasets/dataset_list.tmpl index 88bb71158..2d334ef3c 100644 --- a/templates/datasets/dataset_list.tmpl +++ b/templates/datasets/dataset_list.tmpl @@ -1,4 +1,4 @@ -
{{$.i18n.Tr "org.repo_updated"}} {{TimeSinceUnix .UpdatedUnix $.i18n.Lang}}
{{$.i18n.Tr "org.repo_updated"}} 2019-02-01
-{{$.i18n.Tr "org.repo_updated"}} 2019-02-01
-{{$.i18n.Tr "org.repo_updated"}} 2019-02-01
-{{.DescriptionHTML}}
{{end}} - {{if .Topics }} - - {{end}} -{{$.i18n.Tr "org.repo_updated"}} {{TimeSinceUnix .UpdatedUnix $.i18n.Lang}}
-