-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pagination support sorting * fixing lint issues * removing coverage restrictions to errors.go file --------- Co-authored-by: Manuel Doncel Martos <[email protected]>
- Loading branch information
1 parent
d420128
commit 432939b
Showing
10 changed files
with
462 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package pagorminator | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
var ( | ||
ErrPageCantBeNegative = errors.New("page number can't be negative") | ||
ErrSizeCantBeNegative = errors.New("size can't be negative") | ||
ErrSizeNotAllowed = errors.New("size is not allowed") | ||
ErrOrderPropertyIsEmpty = errors.New("order property is empty") | ||
) | ||
|
||
type ErrOrderDirectionNotValid struct { | ||
Direction Direction | ||
} | ||
|
||
func (e ErrOrderDirectionNotValid) Error() string { | ||
return fmt.Sprintf("order direction is not valid: %s", e.Direction) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
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{}) | ||
db.Create(&Product{Code: "D42", Price: 100}) | ||
db.Create(&Product{Code: "E42", Price: 200}) | ||
fmt.Printf("2 products created\n") | ||
|
||
var products []*Product | ||
pageRequest, _ := pagorminator.PageRequest(0, 1, pagorminator.MustNewOrder("price", pagorminator.DESC)) | ||
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("%s\n", product) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package pagorminator | ||
|
||
import ( | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/clause" | ||
) | ||
|
||
const pagorminatorClause = "pagorminator:clause" | ||
|
||
var _ clause.Expression = new(Pagination) | ||
var _ gorm.StatementModifier = new(Pagination) | ||
|
||
// ModifyStatement Modify the query clause to apply pagination | ||
func (p *Pagination) ModifyStatement(stm *gorm.Statement) { | ||
db := stm.DB | ||
db.Set(pagorminatorClause, p) | ||
tx := stm.DB | ||
if !p.IsUnPaged() { | ||
tx = tx.Limit(p.size).Offset(p.GetOffset()) | ||
} | ||
if p.IsSort() { | ||
tx.Order(p.sort.String()) | ||
} | ||
} | ||
|
||
// Build N/A for pagination | ||
func (p *Pagination) Build(_ clause.Builder) { | ||
} |
File renamed without changes.
Oops, something went wrong.