/
query.go
89 lines (66 loc) · 1.77 KB
/
query.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package impl
import (
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo/options"
"github.com/infraboard/keyauth/apps/mconf"
"github.com/infraboard/mcube/exception"
)
func newGroupPaggingQuery(req *mconf.QueryGroupRequest) *queryGroupRequest {
return &queryGroupRequest{req}
}
type queryGroupRequest struct {
*mconf.QueryGroupRequest
}
func (r *queryGroupRequest) FindOptions() *options.FindOptions {
pageSize := int64(r.Page.PageSize)
skip := int64(r.Page.PageSize) * int64(r.Page.PageNumber-1)
opt := &options.FindOptions{
Sort: bson.D{bson.E{Key: "create_at", Value: -1}},
Limit: &pageSize,
Skip: &skip,
}
return opt
}
func (r *queryGroupRequest) FindFilter() bson.M {
filter := bson.M{}
return filter
}
func newDescribeGroupQuery(req *mconf.DescribeGroupRequest) (*describeGroupRequest, error) {
if err := req.Validate(); err != nil {
return nil, exception.NewBadRequest(err.Error())
}
return &describeGroupRequest{req}, nil
}
type describeGroupRequest struct {
*mconf.DescribeGroupRequest
}
func (r *describeGroupRequest) FindFilter() bson.M {
filter := bson.M{}
if r.Name != "" {
filter["_id"] = r.Name
}
return filter
}
func newItemPaggingQuery(req *mconf.QueryItemRequest) *queryItemRequest {
return &queryItemRequest{req}
}
type queryItemRequest struct {
*mconf.QueryItemRequest
}
func (r *queryItemRequest) FindOptions() *options.FindOptions {
pageSize := int64(r.Page.PageSize)
skip := int64(r.Page.PageSize) * int64(r.Page.PageNumber-1)
opt := &options.FindOptions{
Sort: bson.D{bson.E{Key: "create_at", Value: -1}},
Limit: &pageSize,
Skip: &skip,
}
return opt
}
func (r *queryItemRequest) FindFilter() bson.M {
filter := bson.M{}
if r.GroupName != "" {
filter["group"] = r.GroupName
}
return filter
}