-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pager_test.go
49 lines (37 loc) · 991 Bytes
/
pager_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
package pageboy
import (
"encoding/json"
"fmt"
"gorm.io/gorm"
)
func ExamplePager() {
db := openDB()
// Please execute it only once immediately after opening DB.
RegisterCallbacks(db)
type User struct {
gorm.Model
Name string
}
db.Migrator().DropTable(&User{})
db.AutoMigrate(&User{})
db.Create(&User{Name: "Alice"})
db.Create(&User{Name: "Bob"})
db.Create(&User{Name: "Carol"})
// Default Values.
pager := &Pager{Page: 1, PerPage: 2}
// Update values from a http request.
// Fetch Records.
var users []User
db.Scopes(pager.Scope()).Order("id ASC").Find(&users)
fmt.Printf("len(users) == %d\n", len(users))
fmt.Printf("users[0].Name == \"%s\"\n", users[0].Name)
fmt.Printf("users[1].Name == \"%s\"\n", users[1].Name)
// Return the Summary.
j, _ := json.Marshal(pager.Summary())
fmt.Println(string(j))
// Output:
// len(users) == 2
// users[0].Name == "Alice"
// users[1].Name == "Bob"
// {"page":1,"per_page":2,"total_count":3,"total_page":2}
}