forked from content-services/content-sources-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
40 lines (29 loc) · 876 Bytes
/
helpers.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
package dao
import "strings"
func convertSortByToSQL(SortBy string, SortMap map[string]string) string {
sqlOrderBy := ""
sortByArray := strings.Split(SortBy, ",")
lengthOfSortByParams := len(sortByArray)
for i := 0; i < lengthOfSortByParams; i++ {
sortBy := sortByArray[i]
split := strings.Split(sortBy, ":")
ascOrDesc := " asc"
if len(split) > 1 && split[1] == "desc" {
ascOrDesc = " desc"
}
sortField, ok := SortMap[strings.TrimSpace(split[0])]
// Only update if the SortMap above returns a valid value
if ok {
// Concatenate (e.g. "url desc," + "name" + " asc")
sqlOrderBy = sqlOrderBy + sortField + ascOrDesc
// Add a comma if this isn't the last item in the "sortByArray".
if i+1 < lengthOfSortByParams {
sqlOrderBy = sqlOrderBy + ", "
}
}
}
if sqlOrderBy == "" {
sqlOrderBy = "name asc"
}
return sqlOrderBy
}