forked from Lvsprasad/nodejs-todo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
213 lines (187 loc) · 6.62 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
const express = require("express");
const path = require("path");
const { open } = require("sqlite");
const sqlite3 = require("sqlite3");
const format = require("date-fns/format");
const app = express();
const dbPath = path.join(__dirname, "todoApplication.db");
let db = null;
const initializeDbAndServer = async () => {
try {
const db = await open({
filename: dbPath,
driver: sqlite3.Database,
});
app.listen(3000, () => {
console.log("Server Running at http://localhost:3000/");
});
} catch (error) {
console.log(`DB Error : ${error.message}`);
process.exit(1);
}
};
initializeDbAndServer();
function convertTodo(todo) {
return {
id: todo.id,
todo: todo.todo,
priority: todo.priority,
category: todo.category,
status: todo.status,
dueDate: format(nwe Date(todo.due_date),'yyyy-MM-dd')
};
}
app.get("/todos/", async (request, response) => {
try {
const { status } = request.query;
const getTodoStatusQuery = `SELECT * FROM todo WHERE status = '${status}`;
const todo_array = await db.all(getTodoStatusQuery);
response.send(todo_array.map((todo) => convertTodo(todo)));
} catch (error) {
response.status(400);
response.send("Invalid Todo Status");
}
});
app.get("/todos/", async (request, response) => {
try {
const { priority } = request.query;
const getTodoStatusQuery = `SELECT * FROM todo WHERE priority = '${priority}'`;
const todo_array = await db.all(getTodoStatusQuery);
response.send(todo_array.map((todo) => convertTodo(todo)));
} catch (error) {
response.status(400);
response.send("Invalid Todo Priority");
}
});
app.get("/todos/", async (request, response) => {
try {
const { priority,status } = request.query;
const getTodoStatusQuery = `SELECT * FROM todo WHERE priority = '${priority}' and status = '${status}'`;
const todo_array = await db.all(getTodoStatusQuery);
response.send(todo_array.map((todo) => convertTodo(todo)));
} catch (error) {
response.status(400);
response.send("Invalid Todo Status and Priority");
}
});
app.get("/todos/", async (request, response) => {
try {
const { search_q } = request.query;
const getTodoStatusQuery = `SELECT * FROM todo WHERE todo like '%${search_q}%'`;
const todo_array = await db.all(getTodoStatusQuery);
response.send(todo_array.map((todo) => convertTodo(todo)));
} catch (error) {
response.status(400);
response.send("Invalid Todo search_q");
}
});
app.get("/todos/", async (request, response) => {
try {
const { category,status } = request.query;
const getTodoStatusQuery = `SELECT * FROM todo WHERE category = '${category}' and status = '${status}'`;
const todo_array = await db.all(getTodoStatusQuery);
response.send(todo_array.map((todo) => convertTodo(todo)));
} catch (error) {
response.status(400);
response.send("Invalid Todo Status and Category");
}
});
app.get("/todos/", async (request, response) => {
try {
const { category } = request.query;
const getTodoStatusQuery = `SELECT * FROM todo WHERE category = '${category}'`;
const todo_array = await db.all(getTodoStatusQuery);
response.send(todo_array.map((todo) => convertTodo(todo)));
} catch (error) {
response.status(400);
response.send("Invalid Todo Status");
}
});
app.get("/todos/", async (request, response) => {
try {
const { category,priority } = request.query;
const getTodoStatusQuery = `SELECT * FROM todo WHERE category = '${category}' and priority = '${priority}'`;
const todo_array = await db.all(getTodoStatusQuery);
response.send(todo_array.map((todo) => convertTodo(todo)));
} catch (error) {
response.status(400);
response.send("Invalid Todo Status");
}
});
app.get("/todos/:todoId/",async (request,response) => {
try{
const {todoId} = request.params;
const query = `select * from todo where id = ${todoId}`;
const todo = await db.get(query);
response.send(convertTodo(todo)));
}catch(error){
response.status(400);
response.send("Invalid Todo Id");
}
});
app.get("/agenda",async (request,response) => {
try{
const {date} =request.query;
const date = format(new Date(date),"yyyy-MM-dd");
const dateQuery = `select * from todo where date = ${date}`;
const todos = await db.all(dateQuery);
response.send(todos.map(todo => convertTodo(todo)));
}catch(error){
response.status(400);
response.send("Invalid Agenda");
}
});
app.post("/todos/", async (request,response) => {
try{
const {id,todo,category,priority,status,due_date} = request.body;
const postQuery = `insert into todo (id,todo,category,priority,status,due_date)
values (${id},'${todo}','${category}','${priority}','${status}',${due_date})`;
const response = await db.run(postQuery);
response.send("Todo Successfully Added");
}catch(error){
response.status(400);
}
})
//id=${id},todo='${todo}',category='${category}',priority='${priority}'
app.put("/todos/:todoId",async (request,response) => {
const {todoId} = request.query;
const {status} = request.body;
const putQuery = `update todo set status='${status}' where id = ${todoId}`
await db.run(putQuery);
response.send("Status Updated");
});
app.put("/todos/:todoId",async (request,response) => {
const {todoId} = request.query;
const {priority} = request.body;
const putQuery = `update todo set priority='${priority}' where id = ${todoId}`
await db.run(putQuery);
response.send("Priority Updated");
});
app.put("/todos/:todoId",async (request,response) => {
const {todoId} = request.query;
const {todo} = request.body;
const putQuery = `update todo set todo='${todo}' where id = ${todoId}`
await db.run(putQuery);
response.send("Todo Updated");
});
app.put("/todos/:todoId",async (request,response) => {
const {todoId} = request.query;
const {category} = request.body;
const putQuery = `update todo set category ='${category}' where id = ${todoId}`
await db.run(putQuery);
response.send("Category Updated");
});
app.put("/todos/:todoId",async (request,response) => {
const {todoId} = request.query;
const {dueDate} = request.body;
const putQuery = `update todo set due_date ='${dueDate}' where id = ${todoId}`
await db.run(putQuery);
response.send("Due Date Updated");
});
app.delete("/todos/:todoId",async (request,response) => {
const {todoId} = request.query;
const deleteQuery = `delete from todo where id = ${todoId}`
await db.run(deleteQuery);
response.send("Todo Deleted");
});
module.exports = app;