Pagination and ordering
This commit is contained in:
@ -1,12 +1,30 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"repo-pattern/app/repository/smartfilter"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/schema"
|
||||
)
|
||||
|
||||
type Pagination struct {
|
||||
Offset int
|
||||
Limit int
|
||||
}
|
||||
|
||||
type OrderDirection string
|
||||
|
||||
const (
|
||||
OrderASC OrderDirection = "ASC"
|
||||
OrderDESC OrderDirection = "DESC"
|
||||
)
|
||||
|
||||
type Order struct {
|
||||
Field string
|
||||
Direction OrderDirection
|
||||
}
|
||||
|
||||
type ListMethod[T schema.Tabler] struct {
|
||||
DbConn *gorm.DB
|
||||
}
|
||||
@ -15,7 +33,36 @@ func (m *ListMethod[T]) Init(dbConn *gorm.DB) {
|
||||
m.DbConn = dbConn
|
||||
}
|
||||
|
||||
func (m ListMethod[T]) List(filter interface{}) (*[]T, error) {
|
||||
func applyOrdering(query *gorm.DB, ordering *[]Order) *gorm.DB {
|
||||
if ordering == nil || len(*ordering) == 0 {
|
||||
return query
|
||||
}
|
||||
|
||||
for _, order := range *ordering {
|
||||
if order.Direction == OrderASC {
|
||||
query = query.Order(order.Field)
|
||||
} else {
|
||||
query = query.Order(fmt.Sprintf("%s %s", order.Field, order.Direction))
|
||||
}
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func applyPagination(query *gorm.DB, pagination *Pagination) *gorm.DB {
|
||||
if pagination == nil {
|
||||
return query
|
||||
}
|
||||
|
||||
if pagination.Limit != 0 {
|
||||
query = query.Limit(pagination.Limit)
|
||||
}
|
||||
if pagination.Offset != 0 {
|
||||
query = query.Offset(pagination.Offset)
|
||||
}
|
||||
return query
|
||||
}
|
||||
|
||||
func (m ListMethod[T]) List(filter interface{}, ordering *[]Order, pagination *Pagination) (*[]T, error) {
|
||||
var (
|
||||
model T
|
||||
models []T
|
||||
@ -26,6 +73,9 @@ func (m ListMethod[T]) List(filter interface{}) (*[]T, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query = applyOrdering(query, ordering)
|
||||
query = applyPagination(query, pagination)
|
||||
|
||||
query.Find(&models)
|
||||
return &models, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user