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.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. if opts.PageSize <= 0 {
  21. return sess
  22. }
  23. return sess.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  24. }
  25. func (opts ListOptions) setEnginePagination(e Engine) Engine {
  26. opts.setDefaultValues()
  27. return e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
  28. }
  29. func (opts ListOptions) setDefaultValues() {
  30. if opts.PageSize <= 0 {
  31. opts.PageSize = setting.API.DefaultPagingNum
  32. }
  33. if opts.PageSize > setting.API.MaxResponseItems {
  34. opts.PageSize = setting.API.MaxResponseItems
  35. }
  36. if opts.Page <= 0 {
  37. opts.Page = 1
  38. }
  39. }