Skip to content

Commit

Permalink
Merge pull request #1 from Leukocyte-Lab/refactor/testable
Browse files Browse the repository at this point in the history
refactor/testable
  • Loading branch information
knowlet authored Oct 5, 2021
2 parents 61a0dd7 + 01e0df3 commit 132c50a
Showing 1 changed file with 57 additions and 8 deletions.
65 changes: 57 additions & 8 deletions paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,45 @@ func New(page Page, orders []Order, filter map[string]string) *Paginator {

// GenGormTransaction generate GORMv2 sql Transaction (gorm.DB) for paging
func (pgntr Paginator) GenGormTransaction(tx *gorm.DB) *gorm.DB {
tx = pgntr.offset(tx)
tx = pgntr.limit(tx)
tx = pgntr.orderBy(tx)
tx = pgntr.GenPgntrStmt(tx)
tx = pgntr.where(tx)

return tx
}

// CountPageTotal is setter of Paginator.Page.Total by counting total page number
func (pgntr *Paginator) CountPageTotal(tx *gorm.DB) error {
var count int64
// remove offset, limit and order by before count
delete(tx.Statement.Clauses, "ORDER BY")
tx.Offset(-1).Limit(-1).Count(&count)
pgntr.Page.Total = int(math.Ceil(float64(count) / float64(pgntr.Page.Size)))
// limit PageNumber <= PageTotal
tx = pgntr.DelPgntrStmt(tx)

var recordCount int64
tx.Count(&recordCount)

// set PageTotal by counting total page number
pgntr.Page.Total = pgntr.countPageTotal(recordCount)
pgntr.LimitPageTotal()

return nil
}

// GenPgntrStmt add statements for paging
func (pgntr Paginator) GenPgntrStmt(tx *gorm.DB) *gorm.DB {
tx = pgntr.offset(tx)
tx = pgntr.limit(tx)
tx = pgntr.orderBy(tx)

return tx
}

// DelPgntrStmt remove statements for paging
func (pgntr Paginator) DelPgntrStmt(tx *gorm.DB) *gorm.DB {
tx = pgntr.rmOrderBy(tx)
tx = pgntr.rmLimit(tx)
tx = pgntr.rmOffset(tx)

return tx
}

func (pgntr Paginator) where(tx *gorm.DB) *gorm.DB {
return tx.Where(pgntr.Filter)
}
Expand All @@ -64,11 +83,19 @@ func (pgntr Paginator) offset(tx *gorm.DB) *gorm.DB {
return tx.Offset((pgntr.Page.Number - 1) * pgntr.Page.Size)
}

func (pgntr Paginator) rmOffset(tx *gorm.DB) *gorm.DB {
return tx.Offset(-1)
}

func (pgntr Paginator) limit(tx *gorm.DB) *gorm.DB {
// concat LIMIT SQL query statement by Paginator.Page.Size
return tx.Limit(pgntr.Page.Size)
}

func (pgntr Paginator) rmLimit(tx *gorm.DB) *gorm.DB {
return tx.Limit(-1)
}

func (pgntr Paginator) orderBy(tx *gorm.DB) *gorm.DB {
// concat ORDER SQL query statement by Paginator.order
for _, order := range pgntr.Order {
Expand All @@ -77,6 +104,20 @@ func (pgntr Paginator) orderBy(tx *gorm.DB) *gorm.DB {
return tx
}

func (pgntr Paginator) rmOrderBy(tx *gorm.DB) *gorm.DB {
delete(tx.Statement.Clauses, "ORDER BY")
return tx
}

func (pgntr Paginator) countPageTotal(recordCount int64) (pageTotal int) {
// page total start with 1
if recordCount == 0 || pgntr.Page.Size == 0 {
return 1
}

return int(math.Ceil(float64(recordCount) / float64(pgntr.Page.Size)))
}

func (pgntr *Paginator) limitMinPageNumber(minPageNumber int) {
// limit PageNumber >= minPageNumber
if pgntr.Page.Number < minPageNumber {
Expand All @@ -92,10 +133,18 @@ func (pgntr *Paginator) limitMinPageSize(minPageSize int) {
}

func (pgntr *Paginator) LimitPageTotal() {
pgntr.limitPageTotal()
pgntr.limitPageNumber()
}

func (pgntr *Paginator) limitPageTotal() {
// set page total default to 1
if pgntr.Page.Total == 0 {
pgntr.Page.Total = 1
}
}

func (pgntr *Paginator) limitPageNumber() {
// limit PageNumber <= PageTotal
if pgntr.Page.Number > pgntr.Page.Total {
pgntr.Page.Number = pgntr.Page.Total
Expand Down

0 comments on commit 132c50a

Please sign in to comment.