forked from infobloxopen/atlas-app-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
collection_operators.go
89 lines (78 loc) · 2.01 KB
/
collection_operators.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 gorm
import (
"context"
"strings"
"github.com/infobloxopen/atlas-app-toolkit/gateway"
"github.com/infobloxopen/atlas-app-toolkit/query"
"github.com/jinzhu/gorm"
)
func ApplyCollectionOperators(db *gorm.DB, ctx context.Context) (*gorm.DB, error) {
// ApplyCollectionOperators applies query operators taken from context ctx to gorm instance db.
f, err := gateway.Filtering(ctx)
if err != nil {
return nil, err
}
db, err = ApplyFiltering(db, f)
if err != nil {
return nil, err
}
var s *query.Sorting
s, err = gateway.Sorting(ctx)
if err != nil {
return nil, err
}
db = ApplySorting(db, s)
var p *query.Pagination
p, err = gateway.Pagination(ctx)
if err != nil {
return nil, err
}
db = ApplyPagination(db, p)
fs := gateway.FieldSelection(ctx)
if err != nil {
return nil, err
}
db = ApplyFieldSelection(db, fs)
return db, nil
}
// ApplyFiltering applies filtering operator f to gorm instance db.
func ApplyFiltering(db *gorm.DB, f *query.Filtering) (*gorm.DB, error) {
str, args, err := FilteringToGorm(f)
if err != nil {
return nil, err
}
if str != "" {
return db.Where(str, args...), nil
}
return db, nil
}
// ApplySorting applies sorting operator s to gorm instance db.
func ApplySorting(db *gorm.DB, s *query.Sorting) *gorm.DB {
var crs []string
for _, cr := range s.GetCriterias() {
if cr.IsDesc() {
crs = append(crs, cr.GetTag()+" desc")
} else {
crs = append(crs, cr.GetTag())
}
}
if len(crs) > 0 {
return db.Order(strings.Join(crs, ","))
}
return db
}
// ApplyPagination applies pagination operator p to gorm instance db.
func ApplyPagination(db *gorm.DB, p *query.Pagination) *gorm.DB {
return db.Offset(p.GetOffset()).Limit(p.DefaultLimit())
}
// ApplyFieldSelection applies field selection operator fs to gorm instance db.
func ApplyFieldSelection(db *gorm.DB, fs *query.FieldSelection) *gorm.DB {
var fields []string
for _, f := range fs.GetFields() {
fields = append(fields, f.GetName())
}
if len(fields) > 0 {
return db.Select(fields)
}
return db
}