#5416 fix-5409

Merged
zouap merged 2 commits from fix-5409 into V20240423 1 month ago
  1. +116
    -0
      models/dataset.go
  2. +2
    -0
      routers/home.go
  3. +8
    -2
      routers/repo/dataset.go

+ 116
- 0
models/dataset.go View File

@@ -189,6 +189,7 @@ type SearchDatasetOptions struct {
JustNeedZipFile bool
NeedAttachment bool
UploadAttachmentByMe bool
ExploreMine bool
QueryReference bool
}

@@ -224,6 +225,91 @@ func RecommendDataset(dataSetId int64, recommend bool) error {
return err
}

func SearchMyDataset(opts *SearchDatasetOptions) (DatasetList, int64, error) {
cond := SearchMyDatasetCondition(opts)
return SearchMyDatasetByCondition(opts, cond)
}

func SearchMyDatasetCondition(opts *SearchDatasetOptions) builder.Cond {
var cond = builder.NewCond()
cond = cond.And(builder.Neq{"dataset.status": DatasetStatusDeleted})

cond = generateBasicFilterCond(opts, cond)

if opts.RepoID > 0 {
cond = cond.And(builder.Eq{"dataset.repo_id": opts.RepoID})
}

if opts.ExcludeDatasetId > 0 {
cond = cond.And(builder.Neq{"dataset.id": opts.ExcludeDatasetId})
}

if opts.PublicOnly {
cond = cond.And(builder.Eq{"dataset.status": DatasetStatusPublic})
} else if opts.IncludePublic {
cond = cond.And(builder.Eq{"dataset.status": DatasetStatusPublic})
if opts.OwnerID > 0 {
subCon := builder.NewCond()
subCon = subCon.And(builder.Eq{"repository.owner_id": opts.OwnerID})
subCon = generateBasicFilterCond(opts, subCon)
cond = cond.Or(subCon)

}
} else if opts.OwnerID > 0 && !opts.StarByMe && !opts.UploadAttachmentByMe {
cond = cond.And(builder.Eq{"repository.owner_id": opts.OwnerID})
if !opts.IsOwner {
cond = cond.And(builder.Eq{"dataset.status": DatasetStatusPublic})
}
}
if len(opts.DatasetIDs) > 0 && opts.UploadAttachmentByMe {
cond = cond.And(builder.In("dataset.id", opts.DatasetIDs))
}

return cond
}
func SearchMyDatasetByCondition(opts *SearchDatasetOptions, cond builder.Cond) (DatasetList, int64, error) {
if opts.Page <= 0 {
opts.Page = 1
}

var err error
sess := x.NewSession()
defer sess.Close()

datasets := make(DatasetList, 0, opts.PageSize)
selectColumnsSql := "distinct dataset.id,dataset.title, dataset.status, dataset.category, dataset.description, dataset.download_times, dataset.license, dataset.task, dataset.release_id, dataset.user_id, dataset.repo_id, dataset.created_unix,dataset.updated_unix,dataset.num_stars,dataset.recommend,dataset.use_count"

count, err := sess.Distinct("dataset.id").Join("INNER", "repository", "repository.id = dataset.repo_id").
Where(cond).Count(new(Dataset))

if err != nil {
return nil, 0, fmt.Errorf("Count: %v", err)
}

builderQuery := builder.Dialect(setting.Database.Type).Select("id", "title", "status", "category", "description", "download_times", "license", "task", "release_id", "user_id", "repo_id", "created_unix", "updated_unix", "num_stars", "recommend", "use_count").From(builder.Dialect(setting.Database.Type).Select(selectColumnsSql).From("dataset").Join("INNER", "repository", "repository.id = dataset.repo_id").
Where(cond), "d").OrderBy(opts.SearchOrderBy.String())

if opts.PageSize > 0 {
builderQuery.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}

if err = sess.SQL(builderQuery).Find(&datasets); err != nil {
return nil, 0, fmt.Errorf("Dataset: %v", err)
}

if err = datasets.loadAttributes(sess); err != nil {
return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
}

if opts.NeedAttachment {
if err = datasets.loadAttachmentAttributes(opts); err != nil {
return nil, 0, fmt.Errorf("LoadAttributes: %v", err)
}
}

return datasets, count, nil
}

func SearchDataset(opts *SearchDatasetOptions) (DatasetList, int64, error) {
cond := SearchDatasetCondition(opts)
return SearchDatasetByCondition(opts, cond)
@@ -281,6 +367,29 @@ func SearchDatasetCondition(opts *SearchDatasetOptions) builder.Cond {
return cond
}

func generateBasicFilterCond(opts *SearchDatasetOptions, cond builder.Cond) builder.Cond {
if len(opts.Keyword) > 0 {
cond = cond.And(builder.Or(builder.Like{"LOWER(dataset.title)", strings.ToLower(opts.Keyword)}, builder.Like{"LOWER(dataset.description)", strings.ToLower(opts.Keyword)}))
}

if len(opts.Category) > 0 {
cond = cond.And(builder.Eq{"dataset.category": opts.Category})
}

if len(opts.Task) > 0 {
cond = cond.And(builder.Eq{"dataset.task": opts.Task})
}
if len(opts.License) > 0 {
cond = cond.And(builder.Eq{"dataset.license": opts.License})
}

if opts.RecommendOnly {
cond = cond.And(builder.Eq{"dataset.recommend": opts.RecommendOnly})
}

return cond
}

func generateFilterCond(opts *SearchDatasetOptions, cond builder.Cond) builder.Cond {
if len(opts.Keyword) > 0 {
cond = cond.And(builder.Or(builder.Like{"LOWER(dataset.title)", strings.ToLower(opts.Keyword)}, builder.Like{"LOWER(dataset.description)", strings.ToLower(opts.Keyword)}))
@@ -586,6 +695,13 @@ func GetCollaboratorDatasetIdsByUserID(userID int64) []int64 {
return datasets
}

func GetOwnedDatasetIdsByUserID(userID int64) []int64 {
var datasets []int64
_ = x.Table("dataset").Join("INNER", "repository", "dataset.repo_id = repository.id and repository.owner_id=?", userID).
Cols("dataset.id").Find(&datasets)
return datasets
}

func GetTeamDatasetIdsByUserID(userID int64) []int64 {
var datasets []int64
_ = x.Table("dataset").Join("INNER", "team_repo", "dataset.repo_id = team_repo.repo_id").


+ 2
- 0
routers/home.go View File

@@ -448,6 +448,8 @@ func ExploreMyDatasets(ctx *context.Context) {
CloudBrainType: -1,
SearchOrderBy: getDatasetOrderBy(ctx),
JustNeedZipFile: false,
DatasetIDs: models.GetOwnedDatasetIdsByUserID(ctx.User.ID),
ExploreMine: true,
}
repo.DatasetMultiple(ctx, opts)
}


+ 8
- 2
routers/repo/dataset.go View File

@@ -384,8 +384,14 @@ func DatasetMultiple(ctx *context.Context, opts *models.SearchDatasetOptions) {
opts.Category = category
opts.Task = task
opts.License = license

datasets, count, err := models.SearchDataset(opts)
var datasets models.DatasetList
var count int64
var err error
if opts.ExploreMine {
datasets, count, err = models.SearchMyDataset(opts)
} else {
datasets, count, err = models.SearchDataset(opts)
}

if err != nil {
ctx.ServerError("datasets", err)


Loading…
Cancel
Save