aboutsummaryrefslogtreecommitdiff
path: root/utils/meta/pagination.go
diff options
context:
space:
mode:
Diffstat (limited to 'utils/meta/pagination.go')
-rw-r--r--utils/meta/pagination.go71
1 files changed, 25 insertions, 46 deletions
diff --git a/utils/meta/pagination.go b/utils/meta/pagination.go
index 9244294..d0dda08 100644
--- a/utils/meta/pagination.go
+++ b/utils/meta/pagination.go
@@ -1,80 +1,59 @@
package meta
import (
- "dove/types"
"strconv"
"github.com/gofiber/fiber/v2"
"gorm.io/gorm"
)
-func Paginate(context *fiber.Ctx) Pagination {
- requestData := Request(context)
-
- page := DEFAULT_PAGE
- perPage := DEFAULT_PER_PAGE
-
- if requestData != nil {
- if pageValue := requestData.Query("page"); pageValue != nil {
- parsed, _ := strconv.Atoi(pageValue.String())
- if parsed >= DEFAULT_PAGE {
- page = parsed
- }
- }
-
- if perPageValue := requestData.Query("per_page"); perPageValue != nil {
- parsed, _ := strconv.Atoi(perPageValue.String())
- if parsed >= DEFAULT_PAGE && parsed <= MAX_PER_PAGE {
- perPage = parsed
- }
- }
- }
+type Pagination struct {
+ Page int
+ PerPage int
+}
- return Pagination{Page: page, PerPage: perPage}
+type PaginatedResponse struct {
+ Items any `json:"items"`
+ Total int64 `json:"total"`
+ Page int `json:"page"`
+ PerPage int `json:"per_page"`
+ TotalPages int `json:"total_pages"`
}
-func Sort(context *fiber.Ctx, allowedFields []string, fallbackField string) Sorting {
+func Paginate(context *fiber.Ctx) Pagination {
requestData := Request(context)
- field := fallbackField
- direction := "desc"
+ page := DefaultPage
+ perPage := DefaultPerPage
- if requestData != nil {
- if sortValue := requestData.Query("sort"); sortValue != nil {
- for _, allowedField := range allowedFields {
- if sortValue.String() == allowedField {
- field = sortValue.String()
- break
- }
- }
+ if pageValue := requestData.Query("page"); pageValue != "" {
+ parsed, _ := strconv.Atoi(pageValue)
+ if parsed >= DefaultPage {
+ page = parsed
}
+ }
- if orderValue := requestData.Query("order"); orderValue != nil {
- switch orderValue.String() {
- case "asc", "desc":
- direction = orderValue.String()
- }
+ if perPageValue := requestData.Query("per_page"); perPageValue != "" {
+ parsed, _ := strconv.Atoi(perPageValue)
+ if parsed >= DefaultPage && parsed <= MaxPerPage {
+ perPage = parsed
}
}
- return Sorting{Field: field, Direction: direction}
+ return Pagination{Page: page, PerPage: perPage}
}
func (self Pagination) Apply(query *gorm.DB) *gorm.DB {
return query.Offset((self.Page - 1) * self.PerPage).Limit(self.PerPage)
}
-func (self Sorting) Apply(query *gorm.DB) *gorm.DB {
- return query.Order(self.Field + " " + self.Direction)
-}
-
-func (self Pagination) Response(items any, total int64) types.PaginatedResponse {
+func (self Pagination) Response(items any, total int64) PaginatedResponse {
totalPages := int(total) / self.PerPage
if int(total)%self.PerPage > 0 {
totalPages++
}
- return types.PaginatedResponse{
+ return PaginatedResponse{
Items: items,
Total: total,
Page: self.Page,