-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.go
121 lines (105 loc) · 2.88 KB
/
log.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"encoding/json"
"net/http"
"strconv"
"strings"
)
// log
// LogGet .
func LogGet(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var response = make(map[string]interface{})
params := r.URL.Query()
limitOffset := " "
if _, ok := params["limit"]; ok {
limitOffset += " limit " + params["limit"][0]
delete(params, "limit")
} else {
limitOffset += " limit " + defaultLimit
}
offset := defaultOffset
if _, ok := params["offset"]; ok {
limitOffset += " offset " + params["offset"][0]
offset = params["offset"][0]
delete(params, "offset")
} else {
limitOffset += " offset " + defaultOffset
}
orderBy := " "
if _, ok := params["orderby"]; ok {
orderBy += " order by " + params["orderby"][0]
delete(params, "orderby")
if _, ok := params["sortby"]; ok {
orderBy += " " + params["sortby"][0] + " "
delete(params, "sortby")
} else {
orderBy += " asc "
}
} else {
orderBy += " order by created_date_time desc "
}
resp := " * "
if _, ok := params["resp"]; ok {
resp = " " + params["resp"][0] + " "
delete(params, "resp")
}
shouldMail := false
shouldMailID := ""
if _, ok := params["shouldMail"]; ok {
shouldMailID = params["shouldMailID"][0]
shouldMail = true
delete(params, "shouldMail")
delete(params, "shouldMailID")
}
where := ""
init := false
for key, val := range params {
if init {
where += " and "
}
if strings.EqualFold("name", key) {
where += " `" + key + "` like '%" + val[0] + "%' "
} else if strings.EqualFold("amount", key) {
values := strings.Split(val[0], ",")
where += " `" + key + "` between '" + values[0] + "' and '" + values[1] + "' "
} else {
where += " `" + key + "` = '" + val[0] + "' "
}
init = true
}
SQLQuery := " from `" + logTable + "`"
if strings.Compare(where, "") != 0 {
SQLQuery += " where " + where
}
SQLQuery += orderBy
if shouldMail {
mailResults("select "+resp+SQLQuery, shouldMailID)
}
SQLQuery += limitOffset
data, status, ok := selectProcess("select " + resp + SQLQuery)
w.Header().Set("Status", status)
if ok {
response["data"] = data
pagination := map[string]string{}
if len(where) > 0 {
count, _, _ := selectProcess("select count(*) as ctn from `" + logTable + "` where " + where)
pagination["total_count"] = count[0]["ctn"]
} else {
count, _, _ := selectProcess("select count(*) as ctn from `" + logTable + "`")
pagination["total_count"] = count[0]["ctn"]
}
pagination["count"] = strconv.Itoa(len(data))
pagination["offset"] = offset
response["pagination"] = pagination
response["meta"] = setMeta(status, "ok", "")
} else {
response["meta"] = setMeta(status, "", dialogType)
}
w.WriteHeader(getHTTPStatusCode(response["meta"].(map[string]string)["status"]))
meta, required := checkAppUpdate(r)
if required {
response["meta"] = meta
}
json.NewEncoder(w).Encode(response)
}