-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.js
236 lines (223 loc) · 7.33 KB
/
rest.js
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
var mysql = require('mysql');
function restRouter(router, connection) {
console.log("router set in rest.js");
handleApiRequests(router, connection);
}
// function sendJson(response, text) {
// console.log(text);
// response.json({
// "response_type": "in_channel",
// "username": "TODO Bot",
// "text": text
// });
// }
function sendAllTodosJson(response, headerText, text) {
console.log("Message: " + text);
response.json({
"response_type": "in_channel",
"username": "TODO Bot",
"text": "_" + headerText + "_",
"attachments": [
{
"color": "good",
"mrkdwn_in": [
"text",
"pretext"
],
"text": text
}
]
});
}
function sendSuccessJson(response, headerText, text) {
console.log("Success message: " + text);
response.json({
"response_type": "in_channel",
"username": "TODO Bot",
"text": "_" + headerText + "_",
"attachments": [
{
"color": "good",
"mrkdwn_in": [
"text",
"pretext"
],
"text": "*" + text + "*"
}
]
});
}
function sendErrorJson(response, errorText) {
console.log("error: " + errorText);
response.json({
"response_type": "in_channel",
"username": "TODO Bot",
"attachments": [
{
"color": "danger",
"mrkdwn_in": [
"text",
"pretext"
],
"text": "_" + errorText + "_"
}
]
});
}
function sendWarningJson(response, warningText) {
console.log("warning: " + warningText);
response.json({
"response_type": "in_channel",
"username": "TODO Bot",
"attachments": [
{
"color": "warning",
"mrkdwn_in": [
"text",
"pretext"
],
"text": "_" + warningText + "_"
}
]
});
}
function handleApiRequests(router, connection) {
router.post("/", function(req, res) {
console.log("handling /");
res.json({
"Message" : "param1 : " + req.query.param1
});
});
router.post("/addTask", function(req, res) {
console.log("handling /addTask req url : " + req.originalUrl);
console.log("response_url: " + req.body.response_url);
console.log("text: " + req.body.text);
console.log("channel: " + req.body.channel_id);
console.log("user_id: " + req.body.user_id);
console.log("user_name: " + req.body.user_name);
var taskDescription = req.body.text;
if(taskDescription === undefined || taskDescription === "") {
sendErrorJson(res, "Please define a TODO to add");
return;
}
var channelPostedIn = req.body.channel_id;
var userPostedBy = req.body.user_id;
var userName = req.body.user_name;
var status = "0";
console.log("taskDescription: " + taskDescription);
console.log("channelPostedIn: " + channelPostedIn);
console.log("userPostedBy: " + userPostedBy);
console.log("userName: " + userName);
//first check if task is present in db or not.
var countQuery = "SELECT * from ?? WHERE ??=? AND ??=?";
var tableCountQuery = ["tasks", "task_description", taskDescription,
"channel_posted_in", channelPostedIn];
countQuery = mysql.format(countQuery, tableCountQuery);
console.log(countQuery);
connection.query(countQuery, function(err, result) {
if(err) {
console.log(err);
sendErrorJson(res, "Sorry!! unable to add TODO");
} else {
if(result.length === 0) {
var query = "INSERT INTO ??(??,??,??,??,??) VALUES (?,?,?,?,?)";
var table = ["tasks",
"task_description", "channel_posted_in", "user_posted_by", "user_name", "status",
taskDescription, channelPostedIn, userPostedBy, userName, status];
query = mysql.format(query, table);
console.log(query);
connection.query(query, function(err, rows) {
if(err) {
sendErrorJson(res, "Sorry!! unable to add TODO");
} else {
sendSuccessJson(res, "TODO added", taskDescription);
}
});
} else {
console.log("result: " + JSON.stringify(result));
if(result[0].status === 1) {
var updateStatusQuery = "UPDATE ?? SET ?? = ? WHERE ?? = ?";
var updateStatusTable = ["tasks", "status", "0", "task_description", taskDescription];
updateStatusQuery = mysql.format(updateStatusQuery, updateStatusTable);
console.log(updateStatusQuery);
connection.query(updateStatusQuery, function(err, rows) {
if(err) {
sendErrorJson(res, "Sorry!! unable to add TODO");
} else {
sendSuccessJson(res, "TODO added", taskDescription);
}
});
} else {
sendWarningJson(res, "TODO already added");
}
}
}
});
});
router.post("/getAllTasks", function(req, res) {
console.log("handling /getAllTasks");
var channelPostedIn = req.body.channel_id;
var query = "SELECT * FROM ?? WHERE ??=? AND ??=?";
var table = ["tasks", "status", "0", "channel_posted_in", channelPostedIn];
query = mysql.format(query,table);
console.log(query);
connection.query(query, function(err,rows) {
if(err) {
console.log(err);
sendErrorJson(res, "Sorry!! unable to retrieve all TODOs from our database");
} else {
//If row count == 0, then show "No TODOs"
if(rows.length === 0) {
sendWarningJson(res, "No TODOs");
} else {
//else show all the tasks.
console.log("todos : " + JSON.stringify(rows));
var text = "";
for(i = 0; i < rows.length; ++i) {
text += "*- " + rows[i].task_description + "*\n";
}
sendAllTodosJson(res, "TODO list", text);
}
}
});
});
router.post("/taskCompleted", function(req, res) {
console.log("handling /taskCompleted");
console.log("task: " + req.body.text);
var taskDescription = req.body.text;
var channelPostedIn = req.body.channel_id;
if(taskDescription === undefined || taskDescription === "") {
sendErrorJson(res, "Please define a TODO to mark");
return;
}
var checkStatusQuery = "SELECT * FROM ?? WHERE ??=? AND ??=? AND ??=?";
var checkStatusTable = ["tasks", "task_description", taskDescription,
"status", "0", "channel_posted_in", channelPostedIn];
checkStatusQuery = mysql.format(checkStatusQuery, checkStatusTable);
console.log(checkStatusQuery);
connection.query(checkStatusQuery, function(err, rows) {
if(err) {
sendErrorJson(res, "Sorry!! unable to remove TODO");
} else {
//if there is any task with status == 1, then this task has been completed.
if(rows.length > 0) {
var query = "UPDATE ?? SET ?? = ? WHERE ?? = ?";
var table = ["tasks", "status", "1", "task_description", taskDescription];
query = mysql.format(query,table);
console.log(query);
connection.query(query, function(err, rows) {
if(err) {
sendErrorJson(res, "Sorry!! unable to remove TODO");
} else {
sendSuccessJson(res, "Removed TODO", taskDescription);
}
});
} else {
sendWarningJson(res, "No such TODO found in our database");
return;
}
}
});
});
}
module.exports = restRouter;