82 lines
1.4 KiB
Go
82 lines
1.4 KiB
Go
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
|
|
}
|
|
|
|
func (m *ListMethod[T]) Init(dbConn *gorm.DB) {
|
|
m.DbConn = dbConn
|
|
}
|
|
|
|
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
|
|
)
|
|
|
|
query, err := smartfilter.ToQuery(model, filter, m.DbConn)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
query = applyOrdering(query, ordering)
|
|
query = applyPagination(query, pagination)
|
|
|
|
query.Find(&models)
|
|
return &models, nil
|
|
}
|