-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
259 lines (250 loc) · 7.78 KB
/
app.js
File metadata and controls
259 lines (250 loc) · 7.78 KB
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
const { format, isValid } = require("date-fns");
const express = require("express");
const path = require("path");
const { open } = require("sqlite");
const sqlite3 = require("sqlite3");
const filePath = path.join(__dirname, "todoApplication.db");
const app = express();
app.use(express.json());
module.exports = app;
let db;
const initializeDbAndServer = async () => {
try {
db = await open({
filename: filePath,
driver: sqlite3.Database,
});
app.listen(3000, () =>
console.log("Server running at http://localhost:3000/")
);
} catch (e) {
console.log(`Db error ${e.message}`);
process.exit(1);
}
};
initializeDbAndServer();
const convertTodo = (obj) => {
return {
id: obj.id,
todo: obj.todo,
priority: obj.priority,
status: obj.status,
category: obj.category,
dueDate: obj.due_date,
};
};
app.get("/todos/", async (request, response) => {
const { status, priority, search_q, category } = request.query;
let query;
if (
status !== undefined &&
priority === undefined &&
search_q === undefined &&
category === undefined
) {
query = `select * from todo where status = "${status}";`;
let dbReply = await db.get(query);
if (dbReply === undefined) {
response.status(400);
response.send("Invalid Todo Status");
} else {
const dbReply = await db.all(query);
response.send(dbReply.map((obj) => convertTodo(obj)));
}
} else if (
status === undefined &&
priority !== undefined &&
search_q === undefined &&
category === undefined
) {
query = `select * from todo where priority = "${priority}";`;
let dbReply = await db.get(query);
if (dbReply === undefined) {
response.status(400);
response.send("Invalid Todo Priority");
} else {
const dbReply = await db.all(query);
response.send(dbReply.map((obj) => convertTodo(obj)));
}
} else if (
status !== undefined &&
priority !== undefined &&
search_q === undefined &&
category === undefined
) {
query = `select * from todo where status = "${status}" and priority = "${priority}";`;
const dbReply = await db.all(query);
response.send(dbReply.map((obj) => convertTodo(obj)));
} else if (
status === undefined &&
priority === undefined &&
search_q !== undefined &&
category === undefined
) {
query = `select * from todo where todo like "%${search_q}%";`;
const dbReply = await db.all(query);
response.send(dbReply.map((obj) => convertTodo(obj)));
} else if (
status !== undefined &&
priority === undefined &&
search_q === undefined &&
category !== undefined
) {
query = `select * from todo where status = "${status}" and category = "${category}";`;
const dbReply = await db.all(query);
response.send(dbReply.map((obj) => convertTodo(obj)));
} else if (
status === undefined &&
priority === undefined &&
search_q === undefined &&
category !== undefined
) {
query = `select * from todo where category = "${category}";`;
let dbReply = await db.get(query);
if (dbReply === undefined) {
response.status(400);
response.send("Invalid Todo Category");
} else {
const dbReply = await db.all(query);
response.send(dbReply.map((obj) => convertTodo(obj)));
}
} else if (
status === undefined &&
priority !== undefined &&
search_q === undefined &&
category !== undefined
) {
query = `select * from todo where priority = "${priority}" and category = "${category}";`;
const dbReply = await db.all(query);
response.send(dbReply.map((obj) => convertTodo(obj)));
}
});
app.get("/todos/:todoId/", async (request, response) => {
const { todoId } = request.params;
const query = `select * from todo where id = ${todoId};`;
const dbReply = await db.get(query);
response.send(convertTodo(dbReply));
});
app.get("/agenda/", async (request, response) => {
let { date } = request.query;
const isDateValid = isValid(new Date(date));
if (isDateValid === false) {
response.status(400);
response.send("Invalid Due Date");
} else {
date = format(new Date(date), "yyyy-MM-dd");
const query = `select * from todo where due_date = "${date}";`;
const dbReply = await db.all(query);
response.send(dbReply.map((eachObj) => convertTodo(eachObj)));
}
});
app.post("/todos/", async (request, response) => {
let { id, todo, priority, status, category, dueDate } = request.body;
if (status !== "TO DO" && status !== "IN PROGRESS" && status !== "DONE") {
response.status(400);
response.send("Invalid Todo Status");
} else if (
priority !== "HIGH" &&
priority !== "LOW" &&
priority !== "MEDIUM"
) {
response.status(400);
response.send("Invalid Todo Priority");
} else if (
category !== "WORK" &&
category !== "HOME" &&
category !== "LEARNING"
) {
response.status(400);
response.send("Invalid Todo Category");
} else if (isValid(new Date(dueDate)) === false) {
response.status(400);
response.send("Invalid Due Date");
} else {
const query = `insert into todo values(${id},"${todo}","${priority}","${status}","${category}","${dueDate}");`;
await db.run(query);
response.send("Todo Successfully Added");
}
});
app.put("/todos/:todoId/", async (request, response) => {
const { todoId } = request.params;
let { todo, priority, status, category, dueDate } = request.body;
if (
status !== undefined &&
priority === undefined &&
todo === undefined &&
category === undefined &&
dueDate === undefined
) {
if (status !== "TO DO" && status !== "IN PROGRESS" && status !== "DONE") {
response.status(400);
response.send("Invalid Todo Status");
} else {
const query = `update todo set status = "${status}" where id = ${todoId};`;
await db.run(query);
response.send("Status Updated");
}
} else if (
status === undefined &&
priority !== undefined &&
todo === undefined &&
category === undefined &&
dueDate === undefined
) {
if (priority !== "HIGH" && priority !== "LOW" && priority !== "MEDIUM") {
response.status(400);
response.send("Invalid Todo Priority");
} else {
const query = `update todo set priority = "${priority}" where id = ${todoId};`;
await db.run(query);
response.send("Priority Updated");
}
} else if (
status === undefined &&
priority === undefined &&
todo !== undefined &&
category === undefined &&
dueDate === undefined
) {
const query = `update todo set todo = "${todo}" where id = ${todoId};`;
await db.run(query);
response.send("Todo Updated");
} else if (
status === undefined &&
priority === undefined &&
todo === undefined &&
category !== undefined &&
dueDate === undefined
) {
if (category !== "WORK" && category !== "HOME" && category !== "LEARNING") {
response.status(400);
response.send("Invalid Todo Category");
} else {
const query = `update todo set category = "${category}" where id = ${todoId};`;
await db.run(query);
response.send("Category Updated");
}
} else if (
status === undefined &&
priority === undefined &&
todo === undefined &&
category === undefined &&
dueDate !== undefined
) {
if (isValid(new Date(dueDate)) === false) {
response.status(400);
response.send("Invalid Due Date");
} else {
dueDate = format(new Date(dueDate), "yyyy-MM-dd");
const query = `update todo set due_date = "${dueDate}" where id = ${todoId};`;
await db.run(query);
response.send("Due Date Updated");
}
}
});
app.delete("/todos/:todoId/", async (request, response) => {
const { todoId } = request.params;
const query = `delete from todo where id = ${todoId};`;
await db.run(query);
response.send("Todo Deleted");
});