github.com/ars0915/glossika-exercise@v0.0.0-20240904180321-a5617a00d892/util/paging/paginator.go (about)

     1  package paging
     2  
     3  const (
     4  	// LimitKeyName is the request key name.
     5  	LimitKeyName = "limit"
     6  
     7  	// PageKeyName is the request page key name.
     8  	PageKeyName = "page"
     9  
    10  	DefaultLimit    = 20
    11  	DefaultMaxLimit = 5000
    12  	DefaultPage     = 1
    13  )
    14  
    15  // Paginator page data
    16  type Paginator struct {
    17  	TotalCount int  `json:"recordCount" snake:"record_count"`
    18  	TotalPage  int  `json:"pageCount" snake:"page_count"`
    19  	Page       int  `json:"absolutePage" snake:"absolute_page"`
    20  	Limit      int  `json:"pageSize" snake:"page_size"`
    21  	Offset     int  `json:"-" snake:"-"`
    22  	SnakeCase  bool `json:"-" snake:"-"`
    23  }
    24  
    25  func (p *Paginator) SetTotalCount(count int) {
    26  	p.TotalCount = count
    27  	if p.Limit < 0 {
    28  		// unlimit
    29  		p.TotalPage = 1
    30  	} else {
    31  		p.TotalPage = p.TotalCount / p.Limit
    32  	}
    33  	if p.TotalCount%p.Limit > 0 || p.TotalPage == 0 {
    34  		p.TotalPage++
    35  	}
    36  }