-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83652a5
commit e4f9ab5
Showing
1 changed file
with
56 additions
and
0 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
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 | ||
} |