Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
623c021689
|
|||
afcc89bf9e
|
@@ -1,5 +1,5 @@
|
|||||||
package goext
|
package goext
|
||||||
|
|
||||||
const GoextVersion = "0.0.306"
|
const GoextVersion = "0.0.308"
|
||||||
|
|
||||||
const GoextVersionTimestamp = "2023-11-09T09:36:41+0100"
|
const GoextVersionTimestamp = "2023-11-09T10:02:31+0100"
|
||||||
|
@@ -1,29 +1,28 @@
|
|||||||
package pagination
|
package pagination
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Filter interface {
|
type Filter interface {
|
||||||
FilterQuery() mongo.Pipeline
|
FilterQuery() mongo.Pipeline
|
||||||
Pagination() (string, ct.SortDirection)
|
Sort() bson.D
|
||||||
}
|
}
|
||||||
|
|
||||||
type dynamicFilter struct {
|
type dynamicFilter struct {
|
||||||
pipeline mongo.Pipeline
|
pipeline mongo.Pipeline
|
||||||
sortField string
|
sort bson.D
|
||||||
sortDir ct.SortDirection
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d dynamicFilter) FilterQuery() mongo.Pipeline {
|
func (d dynamicFilter) FilterQuery() mongo.Pipeline {
|
||||||
return d.pipeline
|
return d.pipeline
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d dynamicFilter) Pagination() (string, ct.SortDirection) {
|
func (d dynamicFilter) Sort() bson.D {
|
||||||
return d.sortField, d.sortDir
|
return d.sort
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateFilter(pipeline mongo.Pipeline, sortField string, sortdir ct.SortDirection) Filter {
|
func CreateFilter(pipeline mongo.Pipeline, sort bson.D) Filter {
|
||||||
return dynamicFilter{pipeline: pipeline, sortField: sortField, sortDir: sortdir}
|
return dynamicFilter{pipeline: pipeline, sort: sort}
|
||||||
}
|
}
|
||||||
|
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/mongo"
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
ct "gogs.mikescher.com/BlackForestBytes/goext/cursortoken"
|
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
"gogs.mikescher.com/BlackForestBytes/goext/exerr"
|
||||||
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
||||||
pag "gogs.mikescher.com/BlackForestBytes/goext/pagination"
|
pag "gogs.mikescher.com/BlackForestBytes/goext/pagination"
|
||||||
@@ -21,18 +20,15 @@ func (c *Coll[TData]) Paginate(ctx context.Context, filter pag.Filter, page int,
|
|||||||
|
|
||||||
pipelineSort := mongo.Pipeline{}
|
pipelineSort := mongo.Pipeline{}
|
||||||
pipelineFilter := mongo.Pipeline{}
|
pipelineFilter := mongo.Pipeline{}
|
||||||
pf1 := "_id"
|
sort := bson.D{}
|
||||||
pd1 := ct.SortASC
|
|
||||||
|
|
||||||
if filter != nil {
|
if filter != nil {
|
||||||
pipelineFilter = filter.FilterQuery()
|
pipelineFilter = filter.FilterQuery()
|
||||||
pf1, pd1 = filter.Pagination()
|
sort = filter.Sort()
|
||||||
}
|
}
|
||||||
|
|
||||||
if pd1 == ct.SortASC {
|
if len(sort) != 0 {
|
||||||
pipelineSort = append(pipelineSort, bson.D{{Key: "$sort", Value: bson.D{{Key: pf1, Value: +1}}}})
|
pipelineSort = append(pipelineSort, bson.D{{Key: "$sort", Value: sort}})
|
||||||
} else if pd1 == ct.SortDESC {
|
|
||||||
pipelineSort = append(pipelineSort, bson.D{{Key: "$sort", Value: bson.D{{Key: pf1, Value: -1}}}})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pipelinePaginate := mongo.Pipeline{}
|
pipelinePaginate := mongo.Pipeline{}
|
||||||
|
Reference in New Issue
Block a user