Skip to content

Commit

Permalink
refactor: Make number options an integer.
Browse files Browse the repository at this point in the history
Signed-off-by: Erik <[email protected]>
  • Loading branch information
webstradev committed Oct 10, 2024
1 parent 255cb2b commit ad2dee4
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
12 changes: 6 additions & 6 deletions pkg/pagination/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ package pagination
type options struct {
PageText string
SizeText string
DefaultPage string
DefaultPageSize string
DefaultPage int
DefaultPageSize int
MinPageSize int
MaxPageSize int
}

var defaultOptions = options{
PageText: "page",
SizeText: "size",
DefaultPage: "1",
DefaultPageSize: "10",
DefaultPage: 1,
DefaultPageSize: 10,
MinPageSize: 10,
MaxPageSize: 100,
}
Expand All @@ -32,13 +32,13 @@ func WithSizeText(sizeText string) CustomOption {
}
}

func WithDefaultPage(page string) CustomOption {
func WithDefaultPage(page int) CustomOption {
return func(opts *options) {
opts.DefaultPage = page
}
}

func WithDefaultPageSize(pageSize string) CustomOption {
func WithDefaultPageSize(pageSize int) CustomOption {
return func(opts *options) {
opts.DefaultPageSize = pageSize
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func New(customOptions ...CustomOption) gin.HandlerFunc {

return func(c *gin.Context) {
// Extract the page from the query string and convert it to an integer.
pageStr := c.DefaultQuery(opts.PageText, opts.DefaultPage)
pageStr := c.DefaultQuery(opts.PageText, strconv.Itoa(opts.DefaultPage))
page, err := strconv.Atoi(pageStr)
if err != nil {
c.AbortWithStatusJSON(
Expand All @@ -42,7 +42,7 @@ func New(customOptions ...CustomOption) gin.HandlerFunc {
}

// Extract the size from the query string and convert it to an integer.
sizeStr := c.DefaultQuery(opts.SizeText, opts.DefaultPageSize)
sizeStr := c.DefaultQuery(opts.SizeText, strconv.Itoa(opts.DefaultPageSize))
size, err := strconv.Atoi(sizeStr)
if err != nil {
c.AbortWithStatusJSON(
Expand Down
4 changes: 2 additions & 2 deletions pkg/pagination/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ func TestPaginationMiddleware(t *testing.T) {
pagination.New(
pagination.WithPageText("pages"),
pagination.WithSizeText("items"),
pagination.WithDefaultPage("0"),
pagination.WithDefaultPageSize("5"),
pagination.WithDefaultPage(0),
pagination.WithDefaultPageSize(5),
pagination.WithMinPageSize(1),
pagination.WithMaxPageSize(25),
),
Expand Down

0 comments on commit ad2dee4

Please sign in to comment.