This commit is contained in:
30
backend_v1/app/requests/pagination.go
Normal file
30
backend_v1/app/requests/pagination.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package requests
|
||||
|
||||
import "github.com/samber/lo"
|
||||
|
||||
type Pager struct {
|
||||
Pagination `json:",inline"`
|
||||
Total int64 `json:"total"`
|
||||
Items any `json:"items"`
|
||||
}
|
||||
|
||||
type Pagination struct {
|
||||
Page int64 `json:"page" form:"page" query:"page"`
|
||||
Limit int64 `json:"limit" form:"limit" query:"limit"`
|
||||
}
|
||||
|
||||
func (filter *Pagination) Offset() int64 {
|
||||
return (filter.Page - 1) * filter.Limit
|
||||
}
|
||||
|
||||
func (filter *Pagination) Format() *Pagination {
|
||||
if filter.Page <= 0 {
|
||||
filter.Page = 1
|
||||
}
|
||||
|
||||
if !lo.Contains([]int64{10, 20, 50, 100}, filter.Limit) {
|
||||
filter.Limit = 10
|
||||
}
|
||||
|
||||
return filter
|
||||
}
|
||||
41
backend_v1/app/requests/sort.go
Normal file
41
backend_v1/app/requests/sort.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package requests
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
type SortQueryFilter struct {
|
||||
Asc *string `json:"asc" form:"asc"`
|
||||
Desc *string `json:"desc" form:"desc"`
|
||||
}
|
||||
|
||||
func (s *SortQueryFilter) AscFields() []string {
|
||||
if s.Asc == nil {
|
||||
return nil
|
||||
}
|
||||
return strings.Split(*s.Asc, ",")
|
||||
}
|
||||
|
||||
func (s *SortQueryFilter) DescFields() []string {
|
||||
if s.Desc == nil {
|
||||
return nil
|
||||
}
|
||||
return strings.Split(*s.Desc, ",")
|
||||
}
|
||||
|
||||
func (s *SortQueryFilter) DescID() *SortQueryFilter {
|
||||
if s.Desc == nil {
|
||||
s.Desc = lo.ToPtr("id")
|
||||
}
|
||||
|
||||
items := s.DescFields()
|
||||
if lo.Contains(items, "id") {
|
||||
return s
|
||||
}
|
||||
|
||||
items = append(items, "id")
|
||||
s.Desc = lo.ToPtr(strings.Join(items, ","))
|
||||
return s
|
||||
}
|
||||
Reference in New Issue
Block a user