2020-01-25 03:00:29 +08:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
|
|
|
"xorm.io/xorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ListOptions options to paginate results
|
|
|
|
type ListOptions struct {
|
|
|
|
PageSize int
|
2020-01-26 15:32:53 +08:00
|
|
|
Page int // start from 1
|
2020-01-25 03:00:29 +08:00
|
|
|
}
|
|
|
|
|
2021-02-05 01:23:46 +08:00
|
|
|
func (opts *ListOptions) getPaginatedSession() *xorm.Session {
|
2020-01-25 03:00:29 +08:00
|
|
|
opts.setDefaultValues()
|
|
|
|
|
|
|
|
return x.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
|
|
|
}
|
|
|
|
|
2021-02-05 01:23:46 +08:00
|
|
|
func (opts *ListOptions) setSessionPagination(sess *xorm.Session) *xorm.Session {
|
2020-01-25 03:00:29 +08:00
|
|
|
opts.setDefaultValues()
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
if opts.PageSize <= 0 {
|
|
|
|
return sess
|
|
|
|
}
|
2020-01-25 03:00:29 +08:00
|
|
|
return sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
|
|
|
}
|
|
|
|
|
2021-02-05 01:23:46 +08:00
|
|
|
func (opts *ListOptions) setEnginePagination(e Engine) Engine {
|
2020-01-25 03:00:29 +08:00
|
|
|
opts.setDefaultValues()
|
|
|
|
|
|
|
|
return e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
|
|
|
}
|
|
|
|
|
2020-08-24 23:48:15 +08:00
|
|
|
// GetStartEnd returns the start and end of the ListOptions
|
2021-02-05 01:23:46 +08:00
|
|
|
func (opts *ListOptions) GetStartEnd() (start, end int) {
|
2020-08-24 23:48:15 +08:00
|
|
|
opts.setDefaultValues()
|
|
|
|
start = (opts.Page - 1) * opts.PageSize
|
2021-06-30 05:42:23 +08:00
|
|
|
end = start + opts.PageSize
|
2020-08-24 23:48:15 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-05 01:23:46 +08:00
|
|
|
func (opts *ListOptions) setDefaultValues() {
|
2020-06-11 00:23:22 +08:00
|
|
|
if opts.PageSize <= 0 {
|
|
|
|
opts.PageSize = setting.API.DefaultPagingNum
|
|
|
|
}
|
|
|
|
if opts.PageSize > setting.API.MaxResponseItems {
|
2020-01-25 03:00:29 +08:00
|
|
|
opts.PageSize = setting.API.MaxResponseItems
|
|
|
|
}
|
|
|
|
if opts.Page <= 0 {
|
|
|
|
opts.Page = 1
|
|
|
|
}
|
|
|
|
}
|