-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_builder.go
44 lines (41 loc) · 1.37 KB
/
search_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package dynamodb
import (
"context"
"github.com/aws/aws-sdk-go/service/dynamodb"
"reflect"
)
type SearchBuilder struct {
DB *dynamodb.DynamoDB
ModelType reflect.Type
BuildQuery func(m interface{}) (dynamodb.ScanInput, error)
Map func(ctx context.Context, model interface{}) (interface{}, error)
}
func NewSearchBuilder(db *dynamodb.DynamoDB, modelType reflect.Type, buildQuery func(interface{}) (dynamodb.ScanInput, error), options ...func(context.Context, interface{}) (interface{}, error)) *SearchBuilder {
var mp func(ctx context.Context, model interface{}) (interface{}, error)
if len(options) > 0 && options[0] != nil {
mp = options[0]
}
return &SearchBuilder{DB: db, ModelType: modelType, BuildQuery: buildQuery, Map: mp}
}
func (b *SearchBuilder) Search(ctx context.Context, m interface{}, results interface{}, limit int64, options ...int64) (int64, string, error) {
query, er1 := b.BuildQuery(m)
if er1 != nil {
return 0, "", er1
}
var skip int64 = 0
if len(options) > 0 && options[0] > 0 {
skip = options[0]
}
if skip == 0 {
total, er2 := BuildSearchResult(ctx, b.DB, results, query, limit, 1, b.Map)
return total, "", er2
} else {
pageIndex := skip / limit
m := skip % limit
if m > 0 {
pageIndex = pageIndex + 1
}
total, er2 := BuildSearchResult(ctx, b.DB, results, query, limit, pageIndex, b.Map)
return total, "", er2
}
}