From 9390b75232f8e98e5aaa892d2553b01a5b77ad99 Mon Sep 17 00:00:00 2001 From: yan Date: Tue, 26 May 2020 18:53:05 +0800 Subject: [PATCH] improved search options --- models/dataset.go | 27 ++++++++------------------- routers/admin/dataset.go | 7 ------- routers/dataset/dataset.go | 2 +- routers/user/profile.go | 1 - 4 files changed, 9 insertions(+), 28 deletions(-) diff --git a/models/dataset.go b/models/dataset.go index b7cc818a4..b1b63fa6c 100644 --- a/models/dataset.go +++ b/models/dataset.go @@ -76,10 +76,9 @@ func (datasets DatasetList) loadAttributes(e Engine) error { } type SearchDatasetOptions struct { - Keyword string - doerID int64 - OwnerID int64 - IsPublic bool + Keyword string + OwnerID int64 + IncludePublic bool ListOptions SearchOrderBy } @@ -100,27 +99,17 @@ func SearchDataset(opts *SearchDatasetOptions) (DatasetList, int64, error) { func SearchDatasetCondition(opts *SearchDatasetOptions) builder.Cond { var cond = builder.NewCond() cond = cond.And(builder.Neq{"status": DatasetStatusDeleted}) + if len(opts.Keyword) > 0 { cond = cond.And(builder.Like{"title", opts.Keyword}) } - if opts.doerID > 0 { - doer, err := GetUserByID(opts.doerID) - if err != nil { - return nil - } - if doer.IsAdmin { - + if opts.IncludePublic { + cond = cond.And(builder.Eq{"status": DatasetStatusPublic}) + if opts.OwnerID > 0 { + cond = cond.Or(builder.Eq{"user_id": opts.OwnerID}) } - } else { - if opts.IsPublic { - cond = cond.And(builder.Eq{"status": DatasetStatusPublic}) - if opts.OwnerID > 0 { - cond = cond.Or(builder.Eq{"user_id": opts.OwnerID}) - } - } - if opts.OwnerID > 0 { cond = cond.And(builder.Eq{"user_id": opts.OwnerID}) } diff --git a/routers/admin/dataset.go b/routers/admin/dataset.go index 7554bd869..a4378cf67 100644 --- a/routers/admin/dataset.go +++ b/routers/admin/dataset.go @@ -63,12 +63,6 @@ func Datasets(ctx *context.Context) { } keyword := strings.Trim(ctx.Query("q"), " ") - var uid int64 - if ctx.User != nil { - uid = ctx.User.ID - } else { - uid = 0 - } datasets, count, err = models.SearchDataset(&models.SearchDatasetOptions{ ListOptions: models.ListOptions{ @@ -76,7 +70,6 @@ func Datasets(ctx *context.Context) { PageSize: setting.UI.ExplorePagingNum, }, Keyword: keyword, - OwnerID: uid, SearchOrderBy: orderBy, }) if err != nil { diff --git a/routers/dataset/dataset.go b/routers/dataset/dataset.go index 1145f94d9..83ad59dd1 100644 --- a/routers/dataset/dataset.go +++ b/routers/dataset/dataset.go @@ -74,7 +74,7 @@ func MyList(ctx *context.Context) { } opts := &models.SearchDatasetOptions{ Keyword: keyword, - IsPublic: true, + IncludePublic: true, SearchOrderBy: orderBy, OwnerID: userID, ListOptions: models.ListOptions{ diff --git a/routers/user/profile.go b/routers/user/profile.go index 748829c15..dc1d0a113 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -212,7 +212,6 @@ func Profile(ctx *context.Context) { case "datasets": datasetSearchOptions := &models.SearchDatasetOptions{ Keyword: keyword, - IsPublic: true, OwnerID: ctxUser.ID, SearchOrderBy: orderBy, ListOptions: models.ListOptions{