-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (44 loc) · 1.42 KB
/
main.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
package main
import (
"fmt"
"github.com/manuelarte/pagorminator"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"strconv"
)
type Product struct {
gorm.Model
Code string
Price uint
}
func (p Product) String() string {
return fmt.Sprintf("Product{Code: %s, Price: %d}", p.Code, p.Price)
}
func main() {
db, err := gorm.Open(sqlite.Open("file:mem?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
_ = db.Use(pagorminator.PaGormMinator{})
_ = db.AutoMigrate(&Product{})
length := 10
for i := 0; i < length; i++ {
db.Create(&Product{Code: strconv.Itoa(i), Price: uint(i)})
}
fmt.Printf("%s product created\n", length)
var products []*Product
pageRequest, _ := pagorminator.PageRequest(0, 5)
db.Clauses(pageRequest).Find(&products)
fmt.Printf("PageRequest result:(Page: %d, Size: %d, TotalElements: %d, TotalPages: %d)\n",
pageRequest.GetPage(), pageRequest.GetSize(), pageRequest.GetTotalElements(), pageRequest.GetTotalPages())
for _, product := range products {
fmt.Printf("\t Product: %s\n", product)
}
pageRequest, _ = pagorminator.PageRequest(1, 5)
db.Clauses(pageRequest).Find(&products)
fmt.Printf("PageRequest result:(Page: %d, Size: %d, TotalElements: %d, TotalPages: %d)\n",
pageRequest.GetPage(), pageRequest.GetSize(), pageRequest.GetTotalElements(), pageRequest.GetTotalPages())
for _, product := range products {
fmt.Printf("\t Product: %s\n", product)
}
}