Skip to content

Commit

Permalink
dev, fixing bug page0=page1
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Doncel Martos committed Dec 17, 2024
1 parent 9a1f76d commit 16b300b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v0.0.1-rc3] 2024-12-17

## BugFix

- Fixing bug for page 0 and page 1 returning same result

## [v0.0.1-rc2] 2024-11-30

### Added
Expand Down
6 changes: 3 additions & 3 deletions examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ module filter
go 1.18

require (
gorm.io/driver/sqlite v1.5.6
gorm.io/driver/sqlite v1.5.7
gorm.io/gorm v1.25.12
)

require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/manuelarte/pagorminator v0.0.0-20241112184441-94e89258c801 // indirect
github.com/manuelarte/pagorminator v0.0.1-rc2 // indirect
github.com/mattn/go-sqlite3 v1.14.24 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/text v0.21.0 // indirect
)
6 changes: 6 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ github.com/manuelarte/pagorminator v0.0.0-20241112002222-7b6cda225c98 h1:2TbBVYW
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/manuelarte/pagorminator v0.0.1-rc2 h1:yXShWu5v1VK7MXZTyiXXDOlb/CU7jhC0FBSNL6eX4qQ=
github.com/manuelarte/pagorminator v0.0.1-rc2/go.mod h1:0fRra9ZJL6SF7i7jVd5oFfMkgsWrTg2dXAD5xVVCkko=
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=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE=
gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I=
gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=
53 changes: 53 additions & 0 deletions examples/many-pages/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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)
}
}
2 changes: 1 addition & 1 deletion model.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (p *Pagination) GetSize() int {

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

// GetTotalPages Get the total number of pages
Expand Down

0 comments on commit 16b300b

Please sign in to comment.