generated from alecodes/base-template
53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package synchronizator
|
|
|
|
import "fmt"
|
|
|
|
// StartPagination is the initial pagination configuration.
|
|
var StartPagination = Pagination{
|
|
Total: 0,
|
|
Pages: 0,
|
|
HasMore: false,
|
|
Limit: 10,
|
|
Offset: 0,
|
|
}
|
|
|
|
type FetchResponse[T any] struct {
|
|
Pagination
|
|
Response T
|
|
}
|
|
|
|
// Pagination represents the pagination information for fetching data.
|
|
type Pagination struct {
|
|
Total uint64 // Total number of items.
|
|
Pages uint64
|
|
HasMore bool // Indicates if there are more items to fetch.
|
|
Limit uint64 // Number of items to fetch per request.
|
|
Offset uint64 // Offset for the next set of items to fetch.
|
|
}
|
|
|
|
func calculatePages(pagination *Pagination, offset uint64) (uint64, error) {
|
|
if pagination.Limit == 0 {
|
|
return 0, fmt.Errorf("division by zero")
|
|
}
|
|
|
|
dividend := pagination.Total - offset
|
|
divisor := pagination.Limit
|
|
|
|
result := dividend / divisor
|
|
if dividend%divisor != 0 {
|
|
result++
|
|
}
|
|
return result, nil
|
|
}
|
|
|
|
func getPageByOffset(pagination *Pagination) (uint64, error) {
|
|
if pagination.Pages == 0 {
|
|
return 0, fmt.Errorf("division by zero")
|
|
}
|
|
|
|
total := pagination.Pages * pagination.Limit
|
|
|
|
result := (pagination.Pages * pagination.Offset) / total
|
|
|
|
return result, nil
|
|
}
|