Skip to content

Commit

Permalink
returning directly a pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelarte committed Nov 12, 2024
1 parent 3d0e11d commit 7b6cda2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ Gorm plugin to add pagination to your select queries
```go
DB.Use(pagorminator.PaGormMinator{})
var products []*Products
// give me the first 10 products
pageRequest, err := pagorminator.PageRequest(0, 1)
DB.Clauses(&pageRequest).First(&products)
pageRequest, err := pagorminator.PageRequest(0, 10)
DB.Clauses(pageRequest).First(&products)
```

The plugin will calculate the total amount of elements so then the fields `total amounts` and `total pages` can be used too.
Expand Down
2 changes: 1 addition & 1 deletion examples/simple/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
// Read
var products []*Product
pageRequest, _ := pagorminator.PageRequest(0, 1)
db.Clauses(&pageRequest).First(&products)
db.Clauses(pageRequest).First(&products)
for _, product := range products {
fmt.Printf("PageRequest: {Page: %d, Size: %d, TotalElements: %d, TotalPages: %d\n",
pageRequest.GetPage(), pageRequest.GetSize(), pageRequest.GetTotalElements(), pageRequest.GetTotalPages())
Expand Down
14 changes: 7 additions & 7 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,22 @@ var _ clause.Expression = new(Pagination)
var _ gorm.StatementModifier = new(Pagination)

// PageRequest Create page to query the database
func PageRequest(page, size int) (Pagination, error) {
func PageRequest(page, size int) (*Pagination, error) {
if page < 0 {
return Pagination{}, ErrPageCantBeNegative
return nil, ErrPageCantBeNegative
}
if size < 0 {
return Pagination{}, ErrSizeCantBeNegative
return nil, ErrSizeCantBeNegative
}
if page > 0 && size == 0 {
return Pagination{}, ErrSizeNotAllowed
return nil, ErrSizeNotAllowed
}
return Pagination{page: page, size: size}, nil
return &Pagination{page: page, size: size}, nil
}

// UnPaged Create an unpaged request (no pagination is applied)
func UnPaged() Pagination {
return Pagination{page: 0, size: 0}
func UnPaged() *Pagination {
return &Pagination{page: 0, size: 0}
}

// Pagination Clause to apply pagination
Expand Down
30 changes: 15 additions & 15 deletions pagorminator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ func TestPaginationScopeMetadata_NoWhere(t *testing.T) {
t.Parallel()
tests := map[string]struct {
toMigrate []*TestStruct
pageRequest Pagination
expectedPage Pagination
pageRequest *Pagination
expectedPage *Pagination
}{
"UnPaged one item": {
toMigrate: []*TestStruct{
{Code: "1"},
},
pageRequest: UnPaged(),
expectedPage: Pagination{
expectedPage: &Pagination{
page: 0,
size: 0,
totalElements: 1,
Expand All @@ -36,7 +36,7 @@ func TestPaginationScopeMetadata_NoWhere(t *testing.T) {
{Code: "1", Price: 1}, {Code: "2", Price: 2},
},
pageRequest: UnPaged(),
expectedPage: Pagination{
expectedPage: &Pagination{
page: 0,
size: 0,
totalElements: 2,
Expand All @@ -47,7 +47,7 @@ func TestPaginationScopeMetadata_NoWhere(t *testing.T) {
{Code: "1", Price: 1}, {Code: "2", Price: 2},
},
pageRequest: mustPageRequestOf(1, 1),
expectedPage: Pagination{
expectedPage: &Pagination{
page: 1,
size: 1,
totalElements: 2,
Expand All @@ -63,7 +63,7 @@ func TestPaginationScopeMetadata_NoWhere(t *testing.T) {
// Read
var products []*TestStruct

db.Clauses(&test.pageRequest).Find(&products) // find product with integer primary key
db.Clauses(test.pageRequest).Find(&products) // find product with integer primary key
if !equalPageRequests(test.pageRequest, test.expectedPage) {
t.Fatalf("expected page to be %d, got %d", test.expectedPage, test.pageRequest)
}
Expand All @@ -75,17 +75,17 @@ func TestPaginationScopeMetadata_Where(t *testing.T) {
t.Parallel()
tests := map[string]struct {
toMigrate []*TestStruct
pageRequest Pagination
pageRequest *Pagination
where string
expectedPage Pagination
expectedPage *Pagination
}{
"UnPaged one item, not filtered": {
toMigrate: []*TestStruct{
{Code: "1", Price: 1},
},
pageRequest: UnPaged(),
where: "price < 100",
expectedPage: Pagination{
expectedPage: &Pagination{
page: 0,
size: 0,
totalElements: 1,
Expand All @@ -97,7 +97,7 @@ func TestPaginationScopeMetadata_Where(t *testing.T) {
},
pageRequest: UnPaged(),
where: "price > 100",
expectedPage: Pagination{
expectedPage: &Pagination{
page: 0,
size: 0,
totalElements: 0,
Expand All @@ -109,7 +109,7 @@ func TestPaginationScopeMetadata_Where(t *testing.T) {
},
pageRequest: UnPaged(),
where: "price > 50",
expectedPage: Pagination{
expectedPage: &Pagination{
page: 0,
size: 0,
totalElements: 1,
Expand All @@ -122,7 +122,7 @@ func TestPaginationScopeMetadata_Where(t *testing.T) {
},
pageRequest: mustPageRequestOf(0, 1),
where: "price > 50",
expectedPage: Pagination{
expectedPage: &Pagination{
page: 0,
size: 1,
totalElements: 2,
Expand All @@ -138,7 +138,7 @@ func TestPaginationScopeMetadata_Where(t *testing.T) {
// Read
var products []*TestStruct

db.Clauses(&test.pageRequest).Where(test.where).Find(&products)
db.Clauses(test.pageRequest).Where(test.where).Find(&products)
if !equalPageRequests(test.pageRequest, test.expectedPage) {
t.Fatalf("expected page to be %d, got %d", test.expectedPage, test.pageRequest)
}
Expand All @@ -165,12 +165,12 @@ func setupDb(t *testing.T, name string) *gorm.DB {
return db
}

func mustPageRequestOf(page, size int) Pagination {
func mustPageRequestOf(page, size int) *Pagination {
toReturn, _ := PageRequest(page, size)
return toReturn
}

func equalPageRequests(p1, p2 Pagination) bool {
func equalPageRequests(p1, p2 *Pagination) bool {
return p1.page == p2.page &&
p1.size == p2.size &&
p1.totalElements == p2.totalElements &&
Expand Down

0 comments on commit 7b6cda2

Please sign in to comment.