forked from crawlab-team/crawlab-core
/
utils_sort.go
58 lines (51 loc) · 1.3 KB
/
utils_sort.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
package controllers
import (
"encoding/json"
"github.com/xulei324/crawlab-core/constants"
"github.com/xulei324/crawlab-core/entity"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
)
// GetSorts Get entity.Sort from gin.Context
func GetSorts(c *gin.Context) (sorts []entity.Sort, err error) {
// bind
sortStr := c.Query(constants.SortQueryField)
if err := json.Unmarshal([]byte(sortStr), &sorts); err != nil {
return nil, err
}
return sorts, nil
}
// GetSortsOption Get entity.Sort from gin.Context
func GetSortsOption(c *gin.Context) (sort bson.D, err error) {
sorts, err := GetSorts(c)
if err != nil {
return nil, err
}
if sorts == nil || len(sorts) == 0 {
return bson.D{{"_id", -1}}, nil
}
return SortsToOption(sorts)
}
func MustGetSortOption(c *gin.Context) (sort bson.D) {
sort, err := GetSortsOption(c)
if err != nil {
return nil
}
return sort
}
// SortsToOption Translate entity.Sort to bson.D
func SortsToOption(sorts []entity.Sort) (sort bson.D, err error) {
sort = bson.D{}
for _, s := range sorts {
switch s.Direction {
case constants.ASCENDING:
sort = append(sort, bson.E{Key: s.Key, Value: 1})
case constants.DESCENDING:
sort = append(sort, bson.E{Key: s.Key, Value: -1})
}
}
if len(sort) == 0 {
sort = bson.D{{"_id", -1}}
}
return sort, nil
}