Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Use constants
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo committed Nov 9, 2020
1 parent 946b435 commit d8d7c42
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pkg/database/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import (
"github.com/jinzhu/gorm"
)

const (
// maxPrevPages and maxNextPages control the "window" to show.
maxPrevPages = 5
maxNextPages = 5
)

// Paginate is a helper that paginates a gorm query into the given result. In
// addition to reflecting into the provided result, it returns a pagination
// struct.
Expand Down Expand Up @@ -65,14 +71,14 @@ func PaginateFn(query *gorm.DB, page, limit uint64, populateFn func(query *gorm.
if current := offset + limit; current < total {
remaining := total - current
nextPages = uint64(math.Ceil(float64(remaining) / float64(limit)))
if nextPages > 10 {
nextPages = 5
if nextPages > maxNextPages {
nextPages = maxNextPages
}
}

prevPages := page - 1
if prevPages > 10 {
prevPages = 5
if prevPages > maxPrevPages {
prevPages = maxPrevPages
}

var paginator pagination.Paginator
Expand Down

0 comments on commit d8d7c42

Please sign in to comment.