From beb3646f2af0b3fa798701e33b04cf38d599084d Mon Sep 17 00:00:00 2001 From: liuzx Date: Fri, 10 Jun 2022 18:03:50 +0800 Subject: [PATCH] fix-2208 --- models/attachment.go | 10 ---------- routers/repo/dataset.go | 38 +++++++++++++++++++++++++------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/models/attachment.go b/models/attachment.go index e8d2142a2..2bc405b01 100755 --- a/models/attachment.go +++ b/models/attachment.go @@ -723,16 +723,6 @@ func MyFvAttachments(opts *AttachmentsOptions) ([]*AttachmentInfo, int64, error) return nil, 0, fmt.Errorf("Count: %v", err) } - // if opts.Page >= 0 && opts.PageSize > 0 { - // var start int - // if opts.Page == 0 { - // start = 0 - // } else { - // start = (opts.Page - 1) * opts.PageSize - // } - // sess.Limit(opts.PageSize, start) - // } - sess.OrderBy("attachment.created_unix DESC") attachments := make([]*AttachmentInfo, 0) if err := sess.Table(&Attachment{}).Where(cond). diff --git a/routers/repo/dataset.go b/routers/repo/dataset.go index 7f2aa572b..464ae002e 100755 --- a/routers/repo/dataset.go +++ b/routers/repo/dataset.go @@ -521,19 +521,17 @@ func MyFavoriteDataset(ctx *context.Context) { datasets := merge(IsColDatasets, NotColDatasets) count := NotColcount + IsColcount - pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, getTotalPage(count, setting.UI.IssuePagingNum)) - pager.SetDefaultParams(ctx) - - data, err := json.Marshal(datasets) - if err != nil { - log.Error("json.Marshal failed:", err.Error()) - ctx.JSON(200, map[string]string{ - "result_code": "-1", - "error_msg": err.Error(), - "data": "", - }) - return + // page := ctx.QueryInt("page") + if page <= 0 { + page = 1 } + pagesize := ctx.QueryInt("pagesize") + if pagesize <= 0 { + pagesize = 5 + } + pageDatasetsInfo := getPageDatasets(datasets, page, pagesize) + + data, err := json.Marshal(pageDatasetsInfo) ctx.JSON(200, map[string]string{ "result_code": "0", "data": string(data), @@ -541,7 +539,21 @@ func MyFavoriteDataset(ctx *context.Context) { }) } -func merge(a, b interface{}) interface{} { +func getPageDatasets(datasets []*models.AttachmentInfo, page int, pagesize int) []*models.AttachmentInfo { + begin := (page - 1) * pagesize + end := (page) * pagesize + + if begin > len(datasets)-1 { + return nil + } + if end > len(datasets)-1 { + return datasets[begin:] + } else { + return datasets[begin:end] + } + +} +func merge(a, b []*models.AttachmentInfo) []*models.AttachmentInfo { jb, err := json.Marshal(b) if err != nil {