-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathissue.go
209 lines (174 loc) · 5.09 KB
/
issue.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
)
// issue
// IssueGet .
func IssueGet(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("title", 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 `" + issueTable + "`"
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 `" + issueTable + "` where " + where)
pagination["total_count"] = count[0]["ctn"]
} else {
count, _, _ := selectProcess("select count(*) as ctn from `" + issueTable + "`")
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)
}
// IssueAdd .
func IssueAdd(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var response = make(map[string]interface{})
body := map[string]string{}
r.ParseMultipartForm(32 << 20)
for key, value := range r.Form {
body[key] = value[0]
}
fieldCheck := requiredFiledsCheck(body, issueRequiredFields)
if len(fieldCheck) > 0 {
SetReponseStatus(w, r, statusCodeBadRequest, fieldCheck+" required", dialogType, response)
return
}
// log
logAction(body["admin_name"], "added issue "+body["name"], "3", body["hostel_id"])
delete(body, "admin_name")
body["status"] = "1"
body["created_date_time"] = time.Now().UTC().String()
status, ok := insertSQL(issueTable, body)
w.Header().Set("Status", status)
if ok {
response["meta"] = setMeta(status, "Issue added", dialogType)
} 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)
}
// IssueUpdate .
func IssueUpdate(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var response = make(map[string]interface{})
body := map[string]string{}
r.ParseMultipartForm(32 << 20)
for key, value := range r.Form {
body[key] = value[0]
}
for _, field := range issueRequiredFields {
if _, ok := body[field]; ok {
if len(body[field]) == 0 {
SetReponseStatus(w, r, statusCodeBadRequest, field+" required", dialogType, response)
return
}
}
}
// log
logAction(body["admin_name"], "updated issue "+body["name"], "3", body["hostel_id"])
delete(body, "admin_name")
body["modified_date_time"] = time.Now().UTC().String()
status, ok := updateSQL(issueTable, r.URL.Query(), body)
w.Header().Set("Status", status)
if ok {
response["meta"] = setMeta(status, "Issue updated", dialogType)
} 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)
}