You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

list_options.go 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "code.gitea.io/gitea/modules/setting"
  7. "xorm.io/xorm"
  8. )
  9. // ListOptions options to paginate results
  10. type ListOptions struct {
  11. PageSize int
  12. Page int // start from 1
  13. }
  14. func (opts ListOptions) getPaginatedSession() *xorm.Session {
  15. opts.setDefaultValues()
  16. return x.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  17. }
  18. func (opts ListOptions) setSessionPagination(sess *xorm.Session) *xorm.Session {
  19. opts.setDefaultValues()
  20. return sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  21. }
  22. func (opts ListOptions) setEnginePagination(e Engine) Engine {
  23. opts.setDefaultValues()
  24. return e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  25. }
  26. func (opts ListOptions) setDefaultValues() {
  27. if opts.PageSize <= 0 || opts.PageSize > setting.API.MaxResponseItems {
  28. opts.PageSize = setting.API.MaxResponseItems
  29. }
  30. if opts.Page <= 0 {
  31. opts.Page = 1
  32. }
  33. }