summaryrefslogtreecommitdiffstats
path: root/models/list_options.go
blob: c49b83d1fff9d0ce202744a891e4c927143c956e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// 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
	Page     int // start from 1
}

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()

	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() {
	if opts.PageSize <= 0 || opts.PageSize > setting.API.MaxResponseItems {
		opts.PageSize = setting.API.MaxResponseItems
	}
	if opts.Page <= 0 {
		opts.Page = 1
	}
}