lifedebugger's picture
Deploy files from GitHub repository
b0f0dc1
package response
import (
"errors"
"fmt"
)
type PagingInfo struct {
HasPreviousPage bool `json:"has_previous_page"`
HasNextPage bool `json:"has_next_page"`
CurrentPage int `json:"current_page"`
PerPage int `json:"per_page"`
TotalData int `json:"total_data"`
LastPage int `json:"last_page"`
From int `json:"from"`
To int `json:"to"`
TotalDataInCurrentPage int `json:"total_data_in_current_page"`
Label string `json:"label"`
}
var ErrPageInfo = errors.New("per_page harus lebih besar dari 0 dan offset tidak boleh negatif")
func NewPagingInfo(currentPage, perPage, offset, totalData int) (*PagingInfo, error) {
if perPage <= 0 || offset < 0 {
return nil, ErrPageInfo
}
lastPage := totalData / perPage
if totalData%perPage != 0 {
lastPage++
}
to := min(offset+perPage, totalData)
from := int(0)
if to > offset {
from = offset + 1
}
if currentPage > lastPage {
currentPage = lastPage
}
totalDataInCurrentPage := to - offset
label := fmt.Sprintf("Menampilkan %d sampai %d dari %d data", from, to, totalData)
return &PagingInfo{
HasPreviousPage: currentPage > 1,
HasNextPage: currentPage < lastPage,
CurrentPage: currentPage,
PerPage: perPage,
TotalData: totalData,
LastPage: lastPage,
From: from,
To: to,
TotalDataInCurrentPage: totalDataInCurrentPage,
Label: label,
}, nil
}