diff --git a/go.mod b/go.mod index 5795639..67b7ffb 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/a-h/templ v0.2.778 github.com/go-sql-driver/mysql v1.8.1 github.com/gofiber/fiber/v2 v2.52.5 + github.com/google/go-querystring v1.1.0 github.com/google/uuid v1.6.0 github.com/jmoiron/sqlx v1.4.0 github.com/joho/godotenv v1.5.1 diff --git a/go.sum b/go.sum index b63f146..50c221f 100644 --- a/go.sum +++ b/go.sum @@ -11,8 +11,11 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= +github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= +github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= @@ -61,6 +64,7 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/paging/paging.go b/internal/paging/paging.go new file mode 100644 index 0000000..3669e53 --- /dev/null +++ b/internal/paging/paging.go @@ -0,0 +1,73 @@ +package paging + +import ( + "reflect" + + "github.com/google/go-querystring/query" +) + +type Paging struct { + Limit int `url:"limit,omitempty"` // rows per page + Page int `url:"page"` + SortBy string `url:"sort_by,omitempty"` + SortDir string `url:"sort_dir,omitempty"` + + SortDirection string `url:"sort_direction,omitempty"` // DEPRECATED: use SortDir + + // Refresh interval + Refresh int `url:"refresh,omitempty"` +} + +// a-h templ helpers +func EncodedQuery(q interface{}, exclude interface{}) string { + arr := reflect.ValueOf(exclude) + v, _ := query.Values(q) + + for i := 0; i < arr.Len(); i++ { + v.Del(arr.Index(i).String()) + } + + return v.Encode() +} + +type Pagination struct { + CurrentPage int + TotalPages int + Pages []int +} + +func NewPagination(currentPage, totalPages int) Pagination { + var pages []int + const maxButtons = 5 + + if totalPages <= maxButtons { + for i := 1; i <= totalPages; i++ { + pages = append(pages, i) + } + } else { + start := max(1, currentPage-2) + end := min(totalPages, currentPage+2) + + if currentPage <= 3 { + end = maxButtons + } else if currentPage > totalPages-3 { + start = totalPages - (maxButtons - 1) + } + + for i := start; i <= end; i++ { + pages = append(pages, i) + } + if start > 1 { + pages = append([]int{1, -1}, pages...) // -1 indicates ellipsis + } + if end < totalPages { + pages = append(pages, -1, totalPages) // -1 indicates ellipsis + } + } + + return Pagination{ + CurrentPage: currentPage, + TotalPages: totalPages, + Pages: pages, + } +}