Skip to content

Commit

Permalink
add model.Pagination
Browse files Browse the repository at this point in the history
Signed-off-by: Toby Yan <[email protected]>
  • Loading branch information
toby1991 committed Feb 28, 2019
1 parent b691f16 commit 9ca273c
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ func (bm *Model) Paginate(model interface{}, c *gin.Context, perPage uint) (pagi
if err != nil{
return pagination, err
}
if page <= 0{
page = 1
}
paginate.Page = uint(page)

// perpage
Expand All @@ -74,18 +77,18 @@ func (bm *Model) Paginate(model interface{}, c *gin.Context, perPage uint) (pagi
// current page num
pagination.currentPageNum = paginate.Page

//// calc total item count
//count := gorm.DB(*bm)
//if err = count.Count(&out).Error; err != nil{
// return pagination, err
//}
// calc total item count
count := gorm.DB(*bm)
if err = count.Model(model).Count(&pagination.totalItemCount).Error; err != nil{
return pagination, err
}

// calc total page num
pagination.totalPageNum = uint(math.Ceil(float64(pagination.totalItemCount) / float64(perPage)))

// get data
data := gorm.DB(*bm)
if err = data.Offset(perPage * paginate.Page).Limit(perPage).Find(model).Error; err != nil{
if err = data.Offset(perPage * (paginate.Page-1)).Limit(perPage).Find(model).Error; err != nil{
return pagination, err
}
pagination.itemArr = model
Expand Down

0 comments on commit 9ca273c

Please sign in to comment.