Skip to content

Commit

Permalink
improving docs and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelarte committed Nov 30, 2024
1 parent 94e8925 commit 824c956
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 37 deletions.
6 changes: 4 additions & 2 deletions examples/filter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ func main() {
migrateProducts := []*Product{{Code: "1", Price: 1}, {Code: "10", Price: 10},
{Code: "20", Price: 20}, {Code: "21", Price: 21}}
db.CreateInBatches(&migrateProducts, len(migrateProducts))
fmt.Printf("%d products created\n", len(migrateProducts))

var products []*Product
pageRequest, _ := pagorminator.PageRequest(0, 1)
var products []*Product
db.Clauses(pageRequest).Where("price > 10").Find(&products)
fmt.Printf("Query: Products (Page: %d, Size: %d) with '%s'\n", pageRequest.GetPage(), pageRequest.GetSize(), "price > 10")

fmt.Printf("PageRequest: {Page: %d, Size: %d, TotalElements: %d, TotalPages: %d\n",
fmt.Printf("PageRequest result:(Page: %d, Size: %d, TotalElements: %d, TotalPages: %d)\n",
pageRequest.GetPage(), pageRequest.GetSize(), pageRequest.GetTotalElements(), pageRequest.GetTotalPages())
}
4 changes: 3 additions & 1 deletion examples/filter/go.mod → examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module filter

go 1.18

replace github.com/manuelarte/pagorminator v0.0.0-20241112184441-94e89258c801 => ../

require (
gorm.io/driver/sqlite v1.5.6
gorm.io/gorm v1.25.12
Expand All @@ -10,7 +12,7 @@ require (
require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/manuelarte/pagorminator v0.0.0-20241112002222-7b6cda225c98 // indirect
github.com/manuelarte/pagorminator v0.0.0-20241112184441-94e89258c801 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
golang.org/x/text v0.20.0 // indirect
)
2 changes: 2 additions & 0 deletions examples/filter/go.sum → examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/manuelarte/pagorminator v0.0.0-20241112000751-11fca8bd8d58 h1:0yPDhAB
github.com/manuelarte/pagorminator v0.0.0-20241112000751-11fca8bd8d58/go.mod h1:e7ZYAl1XwI3uc0rOXmfF4FToPSS+C65DM4sPXwRNkKs=
github.com/manuelarte/pagorminator v0.0.0-20241112002222-7b6cda225c98 h1:2TbBVYWOrRrkZ9u1gqrTNojbvzQ9wgEG5hBJqzwDv/c=
github.com/manuelarte/pagorminator v0.0.0-20241112002222-7b6cda225c98/go.mod h1:e7ZYAl1XwI3uc0rOXmfF4FToPSS+C65DM4sPXwRNkKs=
github.com/manuelarte/pagorminator v0.0.0-20241112184441-94e89258c801 h1:C9Dm0kyXn4SmtUFredKb902rdSBWgW/J/Q8l6i/WtCM=
github.com/manuelarte/pagorminator v0.0.0-20241112184441-94e89258c801/go.mod h1:e7ZYAl1XwI3uc0rOXmfF4FToPSS+C65DM4sPXwRNkKs=
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
Expand Down
16 changes: 0 additions & 16 deletions examples/simple/go.mod

This file was deleted.

16 changes: 0 additions & 16 deletions examples/simple/go.sum

This file was deleted.

3 changes: 2 additions & 1 deletion examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ func main() {
_ = db.Use(pagorminator.PaGormMinator{})
_ = db.AutoMigrate(&Product{})
db.Create(&Product{Code: "D42", Price: 100})
fmt.Printf("1 product created\n")

var products []*Product
pageRequest, _ := pagorminator.PageRequest(0, 1)
db.Clauses(pageRequest).Find(&products)

fmt.Printf("PageRequest: {Page: %d, Size: %d, TotalElements: %d, TotalPages: %d\n",
fmt.Printf("PageRequest result:(Page: %d, Size: %d, TotalElements: %d, TotalPages: %d)\n",
pageRequest.GetPage(), pageRequest.GetSize(), pageRequest.GetTotalElements(), pageRequest.GetTotalPages())
}
39 changes: 39 additions & 0 deletions examples/unpaged/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"github.com/manuelarte/pagorminator"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)

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{})
migrateProducts := []*Product{{Code: "1", Price: 1}, {Code: "10", Price: 10},
{Code: "20", Price: 20}, {Code: "21", Price: 21}}
db.CreateInBatches(&migrateProducts, len(migrateProducts))
fmt.Printf("%d products created\n", len(migrateProducts))

var products []*Product
pageRequest := pagorminator.UnPaged()
db.Clauses(pageRequest).Find(&products)

fmt.Printf("Unpaged(TotalElements: %d, TotalPages: %d)\n",
pageRequest.GetTotalElements(), pageRequest.GetTotalPages())
}
9 changes: 8 additions & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,22 @@ type Pagination struct {
totalElements int64
}

// GetPage Get the page number
func (p *Pagination) GetPage() int {
return p.page
}

// GetSize Get the page size
func (p *Pagination) GetSize() int {
return p.size
}

// GetOffset Get the offset
func (p *Pagination) GetOffset() int {
return (p.page - 1) * p.size
}

// GetTotalPages Get the total number of pages
func (p *Pagination) GetTotalPages() int {
if p.size > 0 {
return calculateTotalPages(p.totalElements, p.size)
Expand All @@ -71,14 +76,16 @@ func (p *Pagination) IsUnPaged() bool {
return p.page == 0 && p.size == 0
}

// ModifyStatement Modify the query clause to apply pagination
func (p *Pagination) ModifyStatement(stm *gorm.Statement) {
db := stm.DB
db.Set(pagorminatorClause, p)
if !p.IsUnPaged() {
stm.DB.Limit(p.size).Offset((p.page - 1) * p.size)
stm.DB.Limit(p.size).Offset(p.GetOffset())
}
}

// Build N/A for pagination
func (p *Pagination) Build(_ clause.Builder) {
}

Expand Down

0 comments on commit 824c956

Please sign in to comment.