Skip to content

Commit

Permalink
Fix test and add badge
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelgfirmino committed Sep 14, 2023
1 parent 83652a5 commit e4f9ab5
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions pkg/pagination.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package pkg

import "math"

type Paginator[T any] struct {
Items []T `json:"items"`
Total int `json:"total"`
PaginatorInfo
}

type PaginatorInfo struct {
Page int `json:"page"`
TotalPage int `json:"total_page"`
Limit int `json:"limit"`
}

type Filters[T any] struct {
PaginatorInfo
Search T
}

type Pagination struct {
Limit int `json:"limit,omitempty;query:limit"`
Page int `json:"page,omitempty;query:page"`
TotalItems int64 `json:"total_items"`
TotalPages int `json:"total_pages"`
Items any `json:"items,omitempty"`
}

func (p *Pagination) GetOffset() int {
return (p.GetPage() - 1) * p.GetLimit()
}

func (p *Pagination) GetLimit() int {
if p.Limit == 0 {
p.Limit = 10
}
return p.Limit
}

func (p *Pagination) GetPage() int {
if p.Page == 0 {
p.Page = 1
}
return p.Page
}

func (p *Pagination) GetTotalPages() int {
return int(math.Ceil(float64(p.TotalItems) / float64(p.Limit)))
}

func (p *Pagination) Mount(model any) *Pagination {
p.TotalPages = p.GetTotalPages()
p.Items = model
return p
}

0 comments on commit e4f9ab5

Please sign in to comment.