From 6a6d2c7042103b17f1bdd47def6cedccf37bb970 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 13 Oct 2023 08:49:38 +0200 Subject: [PATCH] add pagination --- go.mod | 3 +++ go.sum | 6 ++++++ rspagination/rspagination.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 rspagination/rspagination.go diff --git a/go.mod b/go.mod index 7345106..9dd094c 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,8 @@ require ( github.com/go-playground/validator/v10 v10.15.5 // indirect github.com/gofiber/fiber/v2 v2.49.2 // indirect github.com/google/uuid v1.3.1 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect github.com/klauspost/compress v1.16.7 // indirect github.com/leodido/go-urn v1.2.4 // indirect github.com/mattn/go-colorable v0.1.13 // indirect @@ -24,4 +26,5 @@ require ( golang.org/x/net v0.8.0 // indirect golang.org/x/sys v0.12.0 // indirect golang.org/x/text v0.8.0 // indirect + gorm.io/gorm v1.25.5 // indirect ) diff --git a/go.sum b/go.sum index 602d1ed..8e9589d 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,10 @@ github.com/gofiber/fiber/v2 v2.49.2 h1:ONEN3/Vc+dUCxxDgZZwpqvhISgHqb+bu+isBiEyKE github.com/gofiber/fiber/v2 v2.49.2/go.mod h1:gNsKnyrmfEWFpJxQAV0qvW6l70K1dZGno12oLtukcts= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I= github.com/klauspost/compress v1.16.7/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q= @@ -60,3 +64,5 @@ golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls= +gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= diff --git a/rspagination/rspagination.go b/rspagination/rspagination.go new file mode 100644 index 0000000..61e7c04 --- /dev/null +++ b/rspagination/rspagination.go @@ -0,0 +1,35 @@ +package rspagination + +import ( + "math" + + "gorm.io/gorm" +) + +// GetTotalPages returns total pages for pagination +// Example whereQuery = "stock_item_id = ?" and args = stockItemId is Where("stock_item_id = ?", stockItemId) +func GetTotalPages(database *gorm.DB, paginationLimit int, any interface{}, whereQuery interface{}, args ...interface{}) int { + var totalPages int64 + + database.Model(any). + Where(whereQuery, args). + Count(&totalPages) + + return int(math.Ceil(float64(totalPages) / float64(paginationLimit))) +} + +func GetPageOffset(page int, paginationLimit int) int { + return (page - 1) * paginationLimit +} + +type PageQuery struct { + Page int +} + +func DbPageQuery(database *gorm.DB, query PageQuery, paginationLimit int, result any, orderBy, whereQuery interface{}, args ...interface{}) *gorm.DB { + return database.Limit(paginationLimit). + Offset(GetPageOffset(query.Page, paginationLimit)). + Where(whereQuery, args). + Order(orderBy). + Find(result) +}