2020-01-25 00:30:29 +05:30
|
|
|
// 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 13:02:53 +05:30
|
|
|
Page int // start from 1
|
2020-01-25 00:30:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (opts ListOptions) getPaginatedSession() *xorm.Session {
|
|
|
|
opts.setDefaultValues()
|
|
|
|
|
|
|
|
return x.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts ListOptions) setSessionPagination(sess *xorm.Session) *xorm.Session {
|
|
|
|
opts.setDefaultValues()
|
|
|
|
|
2020-03-22 20:42:55 +05:30
|
|
|
if opts.PageSize <= 0 {
|
|
|
|
return sess
|
|
|
|
}
|
2020-01-25 00:30:29 +05:30
|
|
|
return sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts ListOptions) setEnginePagination(e Engine) Engine {
|
|
|
|
opts.setDefaultValues()
|
|
|
|
|
|
|
|
return e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts ListOptions) setDefaultValues() {
|
2020-06-10 21:53:22 +05:30
|
|
|
if opts.PageSize <= 0 {
|
|
|
|
opts.PageSize = setting.API.DefaultPagingNum
|
|
|
|
}
|
|
|
|
if opts.PageSize > setting.API.MaxResponseItems {
|
2020-01-25 00:30:29 +05:30
|
|
|
opts.PageSize = setting.API.MaxResponseItems
|
|
|
|
}
|
|
|
|
if opts.Page <= 0 {
|
|
|
|
opts.Page = 1
|
|
|
|
}
|
|
|
|
}
|