From 093ed5bfdc38e0c2451cc8ea8fbfe6f3eab98d2b Mon Sep 17 00:00:00 2001 From: ychao_1983 Date: Mon, 18 Apr 2022 11:03:47 +0800 Subject: [PATCH] =?UTF-8?q?fix-1898=201801=201761=E5=90=8E=E7=AB=AF?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- models/attachment.go | 8 ++++++++ models/dataset.go | 16 ++++++++++++++-- routers/admin/dataset.go | 19 +++++++++++++++++++ routers/home.go | 1 + routers/repo/dataset.go | 3 +++ routers/routes/routes.go | 1 + routers/user/profile.go | 8 +++++--- 7 files changed, 51 insertions(+), 5 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index 7c95a73dd..778b9095f 100755 --- a/models/attachment.go +++ b/models/attachment.go @@ -64,6 +64,7 @@ type AttachmentInfo struct { Repo *Repository `xorm:"extends"` RelAvatarLink string `xorm:"extends"` UserName string `xorm:"extends"` + Recommend bool `xorm:"-"` } type AttachmentsOptions struct { @@ -78,6 +79,7 @@ type AttachmentsOptions struct { JustNeedZipFile bool NeedRepoInfo bool Keyword string + RecommendOnly bool } func (a *Attachment) AfterUpdate() { @@ -570,6 +572,11 @@ func Attachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) { builder.Eq{"attachment.is_private": opts.IsPrivate}, ) } + if opts.RecommendOnly { + cond = cond.And(builder.In("attachment.id", builder.Select("attachment.id"). + From("attachment"). + Join("INNER", "dataset", "attachment.dataset_id = dataset.id and dataset.recommend=true"))) + } if opts.JustNeedZipFile { var DecompressState []int32 @@ -618,6 +625,7 @@ func Attachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) { if err != nil { return nil, 0, fmt.Errorf("GetDatasetByID failed error: %v", err) } + attachment.Recommend = dataset.Recommend repo, err := GetRepositoryByID(dataset.RepoID) if err == nil { attachment.Repo = repo diff --git a/models/dataset.go b/models/dataset.go index 95800100c..e841261c7 100755 --- a/models/dataset.go +++ b/models/dataset.go @@ -23,7 +23,8 @@ type Dataset struct { Category string Description string `xorm:"TEXT"` DownloadTimes int64 - NumStars int `xorm:"INDEX NOT NULL DEFAULT 0"` + NumStars int `xorm:"INDEX NOT NULL DEFAULT 0"` + Recommend bool `xorm:"INDEX NOT NULL DEFAULT false"` License string Task string ReleaseID int64 `xorm:"INDEX"` @@ -99,6 +100,7 @@ type SearchDatasetOptions struct { OwnerID int64 RepoID int64 IncludePublic bool + RecommendOnly bool Category string Task string License string @@ -132,6 +134,13 @@ func CreateDataset(dataset *Dataset) (err error) { } +func RecommendDataset(dataSetId int64, recommend bool) error { + + dataset := Dataset{Recommend: recommend} + _, err := x.ID(dataSetId).Cols("recommend").Update(dataset) + return err +} + func SearchDataset(opts *SearchDatasetOptions) (DatasetList, int64, error) { cond := SearchDatasetCondition(opts) return SearchDatasetByCondition(opts, cond) @@ -146,6 +155,9 @@ func SearchDatasetCondition(opts *SearchDatasetOptions) builder.Cond { if opts.RepoID > 0 { cond = cond.And(builder.Eq{"dataset.repo_id": opts.RepoID}) } + if opts.RecommendOnly { + cond = cond.And(builder.Eq{"dataset.recommend": opts.RecommendOnly}) + } if opts.IncludePublic { cond = cond.And(builder.Eq{"dataset.status": DatasetStatusPublic}) @@ -198,7 +210,7 @@ func SearchDatasetByCondition(opts *SearchDatasetOptions, cond builder.Cond) (Da 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" + 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" count, err := sess.Distinct("dataset.id").Join("INNER", "repository", "repository.id = dataset.repo_id"). Join("INNER", "attachment", "attachment.dataset_id=dataset.id"). diff --git a/routers/admin/dataset.go b/routers/admin/dataset.go index a4378cf67..c233c388c 100644 --- a/routers/admin/dataset.go +++ b/routers/admin/dataset.go @@ -1,6 +1,8 @@ package admin import ( + "net/http" + "strconv" "strings" "code.gitea.io/gitea/models" @@ -88,6 +90,23 @@ func Datasets(ctx *context.Context) { ctx.HTML(200, tplDatasets) } +func DatasetAction(ctx *context.Context) { + var err error + datasetId, _ := strconv.ParseInt(ctx.Params(":id"), 10, 64) + switch ctx.Params(":action") { + + case "recommend": + err = models.RecommendDataset(datasetId, true) + case "unrecommend": + err = models.RecommendDataset(datasetId, false) + } + if err != nil { + ctx.JSON(http.StatusOK, models.BaseErrorMessage(ctx.Tr("repo.star_fail", ctx.Params(":action")))) + } else { + ctx.JSON(http.StatusOK, models.BaseOKMessage) + } +} + func DeleteDataset(ctx *context.Context) { dataset, err := models.GetDatasetByID(ctx.QueryInt64("id")) if err != nil { diff --git a/routers/home.go b/routers/home.go index 324bb1032..274e8af69 100755 --- a/routers/home.go +++ b/routers/home.go @@ -331,6 +331,7 @@ func ExploreDatasets(ctx *context.Context) { Task: task, License: license, OwnerID: ownerID, + RecommendOnly: ctx.QueryBool("recommend"), ListOptions: models.ListOptions{ Page: page, PageSize: 30, diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 1a3762be3..73036a2cc 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -358,6 +358,7 @@ func MyDatasets(ctx *context.Context) { NeedIsPrivate: false, JustNeedZipFile: true, NeedRepoInfo: true, + RecommendOnly: ctx.QueryBool("recommend"), }) if err != nil { ctx.ServerError("datasets", err) @@ -398,6 +399,7 @@ func PublicDataset(ctx *context.Context) { Type: cloudbrainType, JustNeedZipFile: true, NeedRepoInfo: true, + RecommendOnly: ctx.QueryBool("recommend"), }) if err != nil { ctx.ServerError("datasets", err) @@ -454,6 +456,7 @@ func MyFavoriteDataset(ctx *context.Context) { Type: cloudbrainType, JustNeedZipFile: true, NeedRepoInfo: true, + RecommendOnly: ctx.QueryBool("recommend"), }) if err != nil { ctx.ServerError("datasets", err) diff --git a/routers/routes/routes.go b/routers/routes/routes.go index cf1a18b31..c281d832b 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -525,6 +525,7 @@ func RegisterRoutes(m *macaron.Macaron) { m.Group("/datasets", func() { m.Get("", admin.Datasets) + m.Put(":id/action/:action", admin.DatasetAction) // m.Post("/delete", admin.DeleteDataset) }) m.Group("/cloudbrains", func() { diff --git a/routers/user/profile.go b/routers/user/profile.go index 41d8561d6..f82c03a75 100755 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -106,9 +106,9 @@ func Profile(ctx *context.Context) { for _, org := range orgs { _, repoCount, err := models.SearchRepository(&models.SearchRepoOptions{ - OwnerID: org.ID, - Private: ctx.IsSigned, - Actor: ctx.User, + OwnerID: org.ID, + Private: ctx.IsSigned, + Actor: ctx.User, }) if err != nil { ctx.ServerError("SearchRepository", err) @@ -175,6 +175,8 @@ func Profile(ctx *context.Context) { orderBy = models.SearchOrderByAlphabeticallyReverse case "alphabetically": orderBy = models.SearchOrderByAlphabetically + case "downloadtimes": + orderBy = models.SearchOrderByDownloadTimes case "moststars": orderBy = models.SearchOrderByStarsReverse case "feweststars":