-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
254 lines (231 loc) · 7.54 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
const express = require("express");
const path = require("path");
const { open } = require("sqlite");
const sqlite3 = require("sqlite3");
const format = require("date-fns/format");
const isMatch = require("date-fns/isMatch");
var isValid = require("date-fns/isValid");
const app = express();
app.use(express.json());
let database;
const initializeDBandServer = async () => {
try {
database = await open({
filename: path.join(__dirname, "todoApplication.db"),
driver: sqlite3.Database,
});
app.listen(3000, () => {
console.log("Server is running on http://localhost:3000/");
});
} catch (error) {
console.log(`DataBase error is ${error.message}`);
process.exit(1);
}
};
initializeDBandServer();
//API 1
const checkQuery = (request, response, next) => {
const {
status = "",
category = "",
priority = "",
search_q = "",
} = request.query;
const statusArray = ["", "TO DO", "IN PROGRESS", "DONE"];
const categoryArray = ["", "HOME", "WORK", "LEARNING"];
const priorityArray = ["", "HIGH", "LOW", "MEDIUM"];
if (!statusArray.includes(status)) {
response.status(400);
response.send("Invalid Todo Status");
} else if (!categoryArray.includes(category)) {
response.status(400);
response.send("Invalid Todo Category");
} else if (!priorityArray.includes(priority)) {
response.status(400);
response.send("Invalid Todo Priority");
} else {
next();
}
};
app.get("/todos/", checkQuery, async (request, response) => {
let addToQuery = ``;
let counter = 0;
for (key in request.query) {
if (counter > 0) {
addToQuery += " AND ";
}
let keyHolder;
if (key === "search_q") {
keyHolder = "todo";
} else {
keyHolder = key;
}
addToQuery += `${keyHolder} LIKE '%${request.query[key]}%'`;
counter += 1;
}
addToQuery += ";";
console.log(addToQuery);
const getTodoQuery =
`SELECT
id, todo, priority, status, category, due_date AS dueDate
FROM todo WHERE ` + addToQuery;
console.log(getTodoQuery);
let todoDetails = await database.all(getTodoQuery);
response.send(todoDetails);
});
//api2
app.get("/todos/:todoId/", async (request, response) => {
const { todoId } = request.params;
const getToDoQuery = `select id, todo, priority, status, category, due_date AS dueDate from todo where id=${todoId};`;
const responseResult = await database.get(getToDoQuery);
response.send(responseResult);
});
//api3
app.get("/agenda/", async (request, response) => {
const { date } = request.query;
console.log(isMatch(date, "yyyy-MM-dd"));
if (isMatch(date, "yyyy-MM-dd")) {
const newDate = format(new Date(date), "yyyy-MM-dd");
console.log(newDate);
const requestQuery = `select id, todo, priority, status, category, due_date AS dueDate from todo where due_date='${newDate}';`;
const responseResult = await database.all(requestQuery);
//console.log(responseResult);
response.send(responseResult);
} else {
response.status(400);
response.send("Invalid Due Date");
}
});
//api4
app.post("/todos/", async (request, response) => {
const { id, todo, priority, status, category, dueDate } = request.body;
if (priority === "HIGH" || priority === "LOW" || priority === "MEDIUM") {
if (status === "TO DO" || status === "IN PROGRESS" || status === "DONE") {
if (
category === "WORK" ||
category === "HOME" ||
category === "LEARNING"
) {
if (isMatch(dueDate, "yyyy-MM-dd")) {
const postNewDueDate = format(new Date(dueDate), "yyyy-MM-dd");
const postTodoQuery = `
INSERT INTO
todo (id, todo, category,priority, status, due_date)
VALUES
(${id}, '${todo}', '${category}','${priority}', '${status}', '${postNewDueDate}');`;
await database.run(postTodoQuery);
//console.log(responseResult);
response.send("Todo Successfully Added");
} else {
response.status(400);
response.send("Invalid Due Date");
}
} else {
response.status(400);
response.send("Invalid Todo Category");
}
} else {
response.status(400);
response.send("Invalid Todo Status");
}
} else {
response.status(400);
response.send("Invalid Todo Priority");
}
});
//api5
app.put("/todos/:todoId/", async (request, response) => {
const { todoId } = request.params;
let updateColumn = "";
const requestBody = request.body;
console.log(requestBody);
const previousTodoQuery = `SELECT * FROM todo WHERE id = ${todoId};`;
const previousTodo = await database.get(previousTodoQuery);
const {
todo = previousTodo.todo,
priority = previousTodo.priority,
status = previousTodo.status,
category = previousTodo.category,
dueDate = previousTodo.dueDate,
} = request.body;
let updateTodoQuery;
switch (true) {
// update status
case requestBody.status !== undefined:
if (status === "TO DO" || status === "IN PROGRESS" || status === "DONE") {
updateTodoQuery = `
UPDATE todo SET todo='${todo}', priority='${priority}', status='${status}', category='${category}',
due_date='${dueDate}' WHERE id = ${todoId};`;
await database.run(updateTodoQuery);
response.send(`Status Updated`);
} else {
response.status(400);
response.send("Invalid Todo Status");
}
break;
//update priority
case requestBody.priority !== undefined:
if (priority === "HIGH" || priority === "LOW" || priority === "MEDIUM") {
updateTodoQuery = `
UPDATE todo SET todo='${todo}', priority='${priority}', status='${status}', category='${category}',
due_date='${dueDate}' WHERE id = ${todoId};`;
await database.run(updateTodoQuery);
response.send(`Priority Updated`);
} else {
response.status(400);
response.send("Invalid Todo Priority");
}
break;
//update todo
case requestBody.todo !== undefined:
updateTodoQuery = `
UPDATE todo SET todo='${todo}', priority='${priority}', status='${status}', category='${category}',
due_date='${dueDate}' WHERE id = ${todoId};`;
await database.run(updateTodoQuery);
response.send(`Todo Updated`);
break;
//update category
case requestBody.category !== undefined:
if (
category === "WORK" ||
category === "HOME" ||
category === "LEARNING"
) {
updateTodoQuery = `
UPDATE todo SET todo='${todo}', priority='${priority}', status='${status}', category='${category}',
due_date='${dueDate}' WHERE id = ${todoId};`;
await database.run(updateTodoQuery);
response.send(`Category Updated`);
} else {
response.status(400);
response.send("Invalid Todo Category");
}
break;
//update due date
case requestBody.dueDate !== undefined:
if (isMatch(dueDate, "yyyy-MM-dd")) {
const newDueDate = format(new Date(dueDate), "yyyy-MM-dd");
updateTodoQuery = `
UPDATE todo SET todo='${todo}', priority='${priority}', status='${status}', category='${category}',
due_date='${newDueDate}' WHERE id = ${todoId};`;
await database.run(updateTodoQuery);
response.send(`Due Date Updated`);
} else {
response.status(400);
response.send("Invalid Due Date");
}
break;
}
});
//api6
app.delete("/todos/:todoId/", async (request, response) => {
const { todoId } = request.params;
const deleteTodoQuery = `
DELETE FROM
todo
WHERE
id = ${todoId};`;
await database.run(deleteTodoQuery);
response.send("Todo Deleted");
});
module.exports = app;