-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
331 lines (317 loc) · 10.2 KB
/
app.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
const express = require("express");
const { open } = require("sqlite");
const path = require("path");
const sqlite3 = require("sqlite3");
const format = require("date-fns/format");
const isValid = require("date-fns/isValid");
const app = express();
const pathDb = path.join(__dirname, "todoApplication.db");
app.use(express.json());
let db = null;
const initializeDbToServer = async (request, response) => {
try {
db = await open({
filename: pathDb,
driver: sqlite3.Database,
});
app.listen(3000, () => {
console.log("Server is Running at http://localhost:3000/");
});
} catch (error) {
console.log(`DB ERROR: ${error.message}`);
process.exit(1);
}
};
initializeDbToServer();
const convertObjToReqObj = (dbObject) => {
return {
id: dbObject.id,
todo: dbObject.todo,
priority: dbObject.priority,
category: dbObject.category,
status: dbObject.status,
dueDate: dbObject.due_date,
};
};
// Get Method
app.get("/todos/", async (request, response) => {
const { search_q = "", category, priority, status } = request.query;
let getTodoQuery = "";
let dbResponse = null;
switch (true) {
//Scenario 3
case priority !== undefined && status !== undefined:
if (priority === "HIGH" || priority === "MEDIUM" || priority === "LOW") {
if (
status === "TO DO" ||
status === "IN PROGRESS" ||
status === "DONE"
) {
getTodoQuery = `
SELECT * FROM todo WHERE todo LIKE '%${search_q}%' AND status = '${status}' AND priority = '${priority}';`;
dbResponse = await db.all(getTodoQuery);
response.send(
dbResponse.map((eachItem) => convertObjToReqObj(eachItem))
);
} else {
response.status(400);
response.send("Invalid Todo Status");
}
} else {
response.status(400);
response.send("Invalid Todo Priority");
}
break;
//Scenario 5
case category !== undefined && status !== undefined:
if (
category === "WORK" ||
category === "HOME" ||
category === "LEARNING"
) {
if (
status === "TO DO" ||
status === "IN PROGRESS" ||
status === "DONE"
) {
getTodoQuery = `
SELECT * FROM todo WHERE todo LIKE '%${search_q}%' AND status = '${status}' AND category = '${category}';`;
dbResponse = await db.all(getTodoQuery);
response.send(
dbResponse.map((eachItem) => convertObjToReqObj(eachItem))
);
} else {
response.status(400);
response.send("Invalid Todo Status");
}
} else {
response.status(400);
response.send("Invalid Todo Category");
}
break;
//Scenario 7
case category !== undefined && priority !== undefined:
if (
category === "WORK" ||
category === "HOME" ||
category === "LEARNING"
) {
if (
priority === "HIGH" ||
priority === "MEDIUM" ||
priority === "LOW"
) {
getTodoQuery = `
SELECT * FROM todo WHERE todo LIKE '%${search_q}%' AND priority = '${priority}' AND category = '${category}';`;
dbResponse = await db.all(getTodoQuery);
response.send(
dbResponse.map((eachItem) => convertObjToReqObj(eachItem))
);
} else {
response.status(400);
response.send("Invalid Todo Priority");
}
} else {
response.status(400);
response.send("Invalid Todo Category");
}
break;
//Scenario 2
case priority !== undefined:
if (priority === "HIGH" || priority === "MEDIUM" || priority === "LOW") {
getTodoQuery = `
SELECT * FROM todo WHERE todo LIKE '%${search_q}%' AND priority = '${priority}';`;
dbResponse = await db.all(getTodoQuery);
response.send(
dbResponse.map((eachItem) => convertObjToReqObj(eachItem))
);
} else {
response.status(400);
response.send("Invalid Todo Priority");
}
break;
//Scenario 1
case status !== undefined:
if (status === "TO DO" || status === "IN PROGRESS" || status === "DONE") {
getTodoQuery = `
SELECT * FROM todo WHERE todo LIKE '%${search_q}%' AND status = '${status}';`;
dbResponse = await db.all(getTodoQuery);
response.send(
dbResponse.map((eachItem) => convertObjToReqObj(eachItem))
);
} else {
response.status(400);
response.send("Invalid Todo Status");
}
break;
//Scenario 6
case category !== undefined:
if (
category === "WORK" ||
category === "HOME" ||
category === "LEARNING"
) {
getTodoQuery = `
SELECT * FROM todo WHERE todo LIKE '%${search_q}%' AND category = '${category}';`;
dbResponse = await db.all(getTodoQuery);
response.send(
dbResponse.map((eachItem) => convertObjToReqObj(eachItem))
);
} else {
response.status(400);
response.send("Invalid Todo Category");
}
break;
//Scenario 4
default:
getTodoQuery = `
SELECT * FROM todo WHERE todo LIKE '%${search_q}%';`;
dbResponse = await db.all(getTodoQuery);
response.send(dbResponse.map((eachItem) => convertObjToReqObj(eachItem)));
break;
}
});
// Get particular Todo
app.get("/todos/:todoId/", async (request, response) => {
const { todoId } = request.params;
const reqQuery = `
SELECT * FROM todo WHERE id = ${todoId};`;
const dbResponse = await db.get(reqQuery);
response.send(convertObjToReqObj(dbResponse));
});
// Get Method using date as query parameter
app.get("/agenda/", async (request, response) => {
const { date } = request.query;
if (isValid(new Date(date))) {
const newDate = format(new Date(date), "yyyy-MM-dd");
const reqQuery = `
SELECT * FROM todo WHERE due_date = '${newDate}';`;
const dbResponse = await db.all(reqQuery);
if (dbResponse !== undefined) {
response.send(dbResponse.map((eachItem) => convertObjToReqObj(eachItem)));
} else {
response.status(400);
response.send("Invalid Due Date");
}
} else {
response.status(400);
response.send("Invalid Due Date");
}
});
// Post Method
app.post("/todos/", async (request, response) => {
const { id, todo, priority, status, category, dueDate } = request.body;
if (category === "WORK" || category === "HOME" || category === "LEARNING") {
if (status === "TO DO" || status === "IN PROGRESS" || status === "DONE") {
if (priority === "HIGH" || priority === "MEDIUM" || priority === "LOW") {
if (isValid(new Date(dueDate))) {
const newDate = format(new Date(dueDate), "yyyy-MM-dd");
const reqQuery = `
INSERT INTO todo(id, todo, priority, status, category, due_date)
VALUES (${id}, '${todo}', '${priority}', '${status}', '${category}', '${newDate}');`;
const dbResponse = await db.run(reqQuery);
response.send("Todo Successfully Added");
} else {
response.status(400);
response.send("Invalid Due Date");
}
} else {
response.status(400);
response.send("Invalid Todo Priority");
}
} else {
response.status(400);
response.send("Invalid Todo Status");
}
} else {
response.status(400);
response.send("Invalid Todo Category");
}
});
// Put Method
app.put("/todos/:todoId/", async (request, response) => {
const { todoId } = request.params;
const { todo, priority, status, category, dueDate } = request.body;
const givenIdQuery = `
SELECT * FROM todo WHERE id = ${todoId};`;
const givenDbRes = await db.get(givenIdQuery);
if (givenDbRes !== undefined) {
switch (true) {
case status !== undefined:
if (
status === "TO DO" ||
status === "IN PROGRESS" ||
status === "DONE"
) {
const reqQuery = `
UPDATE todo SET status = '${status}' WHERE id = ${todoId};`;
const dbResponse = await db.run(reqQuery);
response.send("Status Updated");
} else {
response.status(400);
response.send("Invalid Todo Status");
}
break;
case priority !== undefined:
if (
priority === "HIGH" ||
priority === "MEDIUM" ||
priority === "LOW"
) {
const reqQuery = `
UPDATE todo SET priority = '${priority}' WHERE id = ${todoId};`;
const dbResponse = await db.run(reqQuery);
response.send("Priority Updated");
} else {
response.status(400);
response.send("Invalid Todo Priority");
}
break;
case category !== undefined:
if (
category === "WORK" ||
category === "HOME" ||
category === "LEARNING"
) {
const reqQuery = `
UPDATE todo SET category = '${category}' WHERE id = ${todoId};`;
const dbResponse = await db.run(reqQuery);
response.send("Category Updated");
} else {
response.status(400);
response.send("Invalid Todo Category");
}
break;
case dueDate !== undefined:
if (isValid(new Date(dueDate))) {
const newDate = format(new Date(dueDate), "yyyy-MM-dd");
const reqQuery = `
UPDATE todo SET due_date = '${newDate}' WHERE id = ${todoId};`;
const dbResponse = await db.run(reqQuery);
response.send("Due Date Updated");
} else {
response.status(400);
response.send("Invalid Due Date");
}
break;
default:
if (todo !== undefined) {
const reqQuery = `
UPDATE todo SET todo = '${todo}' WHERE id = ${todoId};`;
const dbResponse = await db.run(reqQuery);
response.send("Todo Updated");
}
break;
}
}
});
//Delete Method
app.delete("/todos/:todoId/", async (request, response) => {
const { todoId } = request.params;
const reqQuery = `
DELETE FROM todo
WHERE id = ${todoId};`;
await db.run(reqQuery);
response.send("Todo Deleted");
});
module.exports = app;