Skip to content

Commit 8f80c83

Browse files
committed
Refactor search
1 parent 6a81f44 commit 8f80c83

File tree

6 files changed

+30
-324
lines changed

6 files changed

+30
-324
lines changed

query.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"strings"
1010
)
1111

12-
func BuildSearchResult(ctx context.Context, collection *mongo.Collection, results interface{}, query bson.M, fields bson.M, sort bson.M, limit int64, skip int64, opts ...func(context.Context, interface{}) (interface{}, error)) (int64, error) {
12+
func BuildSearchResult(ctx context.Context, collection *mongo.Collection, results interface{}, query bson.D, fields bson.M, sort bson.D, limit int64, skip int64, opts ...func(context.Context, interface{}) (interface{}, error)) (int64, error) {
1313
var mp func(context.Context, interface{}) (interface{}, error)
1414
if len(opts) > 0 {
1515
mp = opts[0]
@@ -43,23 +43,26 @@ func BuildSearchResult(ctx context.Context, collection *mongo.Collection, result
4343
return count, er3
4444
}
4545

46-
func BuildSort(s string, modelType reflect.Type) bson.M {
47-
var sort = bson.M{}
46+
func BuildSort(s string, modelType reflect.Type) bson.D {
47+
var sort = bson.D{}
4848
if len(s) == 0 {
4949
return sort
5050
}
5151
sorts := strings.Split(s, ",")
5252
for i := 0; i < len(sorts); i++ {
5353
sortField := strings.TrimSpace(sorts[i])
54-
fieldName := sortField
55-
c := sortField[0:1]
56-
if c == "-" || c == "+" {
57-
fieldName = sortField[1:]
54+
if len(sortField) > 0 {
55+
fieldName := sortField
56+
c := sortField[0:1]
57+
if c == "-" || c == "+" {
58+
fieldName = sortField[1:]
59+
}
60+
columnName := GetBsonNameForSort(modelType, fieldName)
61+
if len(columnName) > 0 {
62+
sortType := GetSortType(c)
63+
sort = append(sort, bson.E{Key: columnName, Value: sortType})
64+
}
5865
}
59-
60-
columnName := GetBsonNameForSort(modelType, fieldName)
61-
sortType := GetSortType(c)
62-
sort[columnName] = sortType
6366
}
6467
return sort
6568
}

query/query_builder.go

Lines changed: 0 additions & 297 deletions
This file was deleted.

search_builder.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import (
99

1010
type SearchBuilder struct {
1111
Collection *mongo.Collection
12-
BuildQuery func(m interface{}) (bson.M, bson.M)
12+
BuildQuery func(m interface{}) (bson.D, bson.M)
1313
GetSort func(m interface{}) string
14-
BuildSort func(s string, modelType reflect.Type) bson.M
14+
BuildSort func(s string, modelType reflect.Type) bson.D
1515
Map func(ctx context.Context, model interface{}) (interface{}, error)
1616
}
1717

18-
func NewSearchBuilderWithSort(db *mongo.Database, collectionName string, buildQuery func(interface{}) (bson.M, bson.M), getSort func(interface{}) string, buildSort func(string, reflect.Type) bson.M, options ...func(context.Context, interface{}) (interface{}, error)) *SearchBuilder {
18+
func NewSearchBuilderWithSort(db *mongo.Database, collectionName string, buildQuery func(interface{}) (bson.D, bson.M), getSort func(interface{}) string, buildSort func(string, reflect.Type) bson.D, options ...func(context.Context, interface{}) (interface{}, error)) *SearchBuilder {
1919
var mp func(context.Context, interface{}) (interface{}, error)
2020
if len(options) > 0 && options[0] != nil {
2121
mp = options[0]
@@ -24,13 +24,13 @@ func NewSearchBuilderWithSort(db *mongo.Database, collectionName string, buildQu
2424
builder := &SearchBuilder{Collection: collection, BuildQuery: buildQuery, GetSort: getSort, BuildSort: buildSort, Map: mp}
2525
return builder
2626
}
27-
func NewSearchBuilder(db *mongo.Database, collectionName string, buildQuery func(interface{}) (bson.M, bson.M), getSort func(interface{}) string, options ...func(context.Context, interface{}) (interface{}, error)) *SearchBuilder {
27+
func NewSearchBuilder(db *mongo.Database, collectionName string, buildQuery func(interface{}) (bson.D, bson.M), getSort func(interface{}) string, options ...func(context.Context, interface{}) (interface{}, error)) *SearchBuilder {
2828
return NewSearchBuilderWithSort(db, collectionName, buildQuery, getSort, BuildSort, options...)
2929
}
3030
func (b *SearchBuilder) Search(ctx context.Context, m interface{}, results interface{}, limit int64, options ...int64) (int64, string, error) {
3131
query, fields := b.BuildQuery(m)
3232

33-
var sort = bson.M{}
33+
var sort = bson.D{}
3434
s := b.GetSort(m)
3535
modelType := reflect.TypeOf(results).Elem().Elem()
3636
sort = b.BuildSort(s, modelType)

search_load.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import (
77
"reflect"
88
)
99

10-
func NewSearchLoaderWithQueryAndSort(db *mongo.Database, collection string, modelType reflect.Type, buildQuery func(interface{}) (bson.M, bson.M), getSort func(interface{}) string, buildSort func(string, reflect.Type) bson.M, options ...func(context.Context, interface{}) (interface{}, error)) (*Searcher, *Loader) {
10+
func NewSearchLoaderWithQueryAndSort(db *mongo.Database, collection string, modelType reflect.Type, buildQuery func(interface{}) (bson.D, bson.M), getSort func(interface{}) string, buildSort func(string, reflect.Type) bson.D, options ...func(context.Context, interface{}) (interface{}, error)) (*Searcher, *Loader) {
1111
return NewMongoSearchLoaderWithQueryAndSort(db, collection, modelType, false, buildQuery, getSort, buildSort, options...)
1212
}
13-
func NewMongoSearchLoaderWithQuery(db *mongo.Database, collection string, modelType reflect.Type, idObjectId bool, buildQuery func(interface{}) (bson.M, bson.M), getSort func(interface{}) string, options ...func(context.Context, interface{}) (interface{}, error)) (*Searcher, *Loader) {
13+
func NewMongoSearchLoaderWithQuery(db *mongo.Database, collection string, modelType reflect.Type, idObjectId bool, buildQuery func(interface{}) (bson.D, bson.M), getSort func(interface{}) string, options ...func(context.Context, interface{}) (interface{}, error)) (*Searcher, *Loader) {
1414
return NewMongoSearchLoaderWithQueryAndSort(db, collection, modelType, idObjectId, buildQuery, getSort, BuildSort, options...)
1515
}
16-
func NewSearchLoaderWithQuery(db *mongo.Database, collection string, modelType reflect.Type, buildQuery func(interface{}) (bson.M, bson.M), getSort func(interface{}) string, options ...func(context.Context, interface{}) (interface{}, error)) (*Searcher, *Loader) {
16+
func NewSearchLoaderWithQuery(db *mongo.Database, collection string, modelType reflect.Type, buildQuery func(interface{}) (bson.D, bson.M), getSort func(interface{}) string, options ...func(context.Context, interface{}) (interface{}, error)) (*Searcher, *Loader) {
1717
return NewMongoSearchLoaderWithQueryAndSort(db, collection, modelType, false, buildQuery, getSort, BuildSort, options...)
1818
}
19-
func NewMongoSearchLoaderWithQueryAndSort(db *mongo.Database, collection string, modelType reflect.Type, idObjectId bool, buildQuery func(interface{}) (bson.M, bson.M), getSort func(interface{}) string, buildSort func(string, reflect.Type) bson.M, options ...func(context.Context, interface{}) (interface{}, error)) (*Searcher, *Loader) {
19+
func NewMongoSearchLoaderWithQueryAndSort(db *mongo.Database, collection string, modelType reflect.Type, idObjectId bool, buildQuery func(interface{}) (bson.D, bson.M), getSort func(interface{}) string, buildSort func(string, reflect.Type) bson.D, options ...func(context.Context, interface{}) (interface{}, error)) (*Searcher, *Loader) {
2020
var mp func(context.Context, interface{}) (interface{}, error)
2121
if len(options) > 0 && options[0] != nil {
2222
mp = options[0]

0 commit comments

Comments
 (0)