Spaces:
Configuration error
Configuration error
File size: 1,612 Bytes
48471f7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 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
}
|