-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
95 lines (73 loc) · 1.6 KB
/
example_test.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package paginator_test
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"github.com/yassinouider/paginator"
)
func Example() {
handler := func(w http.ResponseWriter, r *http.Request) {
p := paginator.New(r)
customers, total, _ := getFakeDataStore().FindCustomers(p.Limit(), p.Offset())
res := map[string]interface{}{
"data": customers,
"pagination": p.SetCount(len(customers)).SetTotal(total),
}
b, _ := json.MarshalIndent(res, "", " ")
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}
req := httptest.NewRequest("GET", "http://example.com/customers?page=3&per_page=2", nil)
w := httptest.NewRecorder()
handler(w, req)
resp := w.Result()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
// Output:
//{
// "data": [
// "User 5",
// "User 6"
// ],
// "pagination": {
// "total": 7,
// "count": 2,
// "per_page": 2,
// "current_page": 3,
// "total_page": 4
// }
//}
}
func getFakeDataStore() *fakeDataStore {
return &fakeDataStore{}
}
type fakeDataStore struct{}
func (s *fakeDataStore) FindCustomers(limit, offset int) ([]string, int, error) {
customers := []string{
"User 1",
"User 2",
"User 3",
"User 4",
"User 5",
"User 6",
"User 7",
}
var start = offset
var end = offset + limit
if start > len(customers) {
start = len(customers)
}
if end > len(customers) {
end = len(customers)
}
return customers[start:end], len(customers), nil
}
func ExampleOffset() {
page := 3
limit := 10
result := paginator.Offset(page, limit)
fmt.Printf("%d", result)
// Output: 20
}