-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
37 lines (30 loc) · 873 Bytes
/
list.go
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
package assets
import (
"errors"
"github.com/GoFarsi/assets/entity"
"golang.org/x/exp/maps"
)
// getPaginatedChainList returns paginated list of chains with pageNumber and pageSize params
func getPaginatedChainList(chains map[string]*entity.Chain, pageNumber, pageSize int) (result []*entity.Chain, err error) {
totalChains := len(chains)
pageNumber, err = validatePageNumber(pageNumber)
if err != nil {
return nil, err
}
start := (pageNumber - 1) * pageSize
if start > totalChains {
return nil, nil
}
end := start + pageSize
if end > totalChains {
end = totalChains
}
return maps.Values(chains)[start:end], nil
}
// validatePageNumber will check the value of pageNumber to be greater than 0
func validatePageNumber(pageNumber int) (int, error) {
if pageNumber == 0 {
return 0, errors.New("pageNumber is invalid")
}
return pageNumber, nil
}