-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy pathindex.js
More file actions
253 lines (201 loc) · 7.33 KB
/
index.js
File metadata and controls
253 lines (201 loc) · 7.33 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
/*
Assignment #2 - Trying to code a filesystem based todo app and store data into the file
*/
// import express module using require function and store it in express variable
const express = require("express");
// import fs module using require function and store it in fs variable
const fs = require("fs");
// import path module using require function and store it in path variable
const path = require("path");
// create an express application using express function
const app = express();
// Middleware to parse JSON data in the request body
app.use(express.json());
// Path to the JSON file that will store the todos
const todosFilePath = path.join(__dirname, "todos-data.json");
// Function to read todos-data from the file and return it as an array of todos objects
const readTodosFromFile = () => {
try {
const data = fs.readFileSync(todosFilePath, "utf-8");
return JSON.parse(data);
} catch (error) {
return [];
}
};
// Function to write todos-data to the file
const writeTodosToFile = (data) => {
fs.writeFileSync(todosFilePath, JSON.stringify(data, null, 2), "utf-8");
};
/**
* create a route handler for POST request
*
* Create a new todo object and add it to the todos array
*
* URL: localhost:3000/todos/create
* Example: localhost:3000/todos/create
*/
app.post("/todos/create", (req, res) => {
// get the todo from the request body
const { todo } = req.body;
// get the todo id from the request body and convert it to integer
const id = parseInt(req.body.id);
if (!id) {
return res.send("Id cannot be empty");
}
// read the todos from the file
let todos = readTodosFromFile();
// check if todo already exists with the given id
for (let i = 0; i < todos.length; i++) {
// if todo already exists with the given id, send a response with message "Todo already exists with id" and the todo id
if (todos[i].id === id) {
return res.send("Todo already exists with id " + id);
}
}
// if todo is empty, send a response with message "Todo cannot be empty"
if (!todo || todo.trim() === "") {
return res.send("Todo cannot be empty");
}
// create a new todo object
const newTodo = {
title: todo,
id: id,
};
// add the new todo object to the todos array
todos.push(newTodo);
// write the todos to the file
writeTodosToFile(todos);
// send a response with message "Todo added successfully"
res.send("Todo added successfully");
});
/**
* create a route handler for DELETE request
*
* Delete all the todos from the array
*
* URL: localhost:3000/todos/delete/all
* Example: localhost:3000/todos/delete/all
*/
app.delete("/todos/delete/all", (req, res) => {
// write an empty array to the file
writeTodosToFile([]);
// send a response with message "All todos deleted successfully"
res.send("All todos deleted successfully");
});
/**
* create a route handler for DELETE request
*
* Delete the todos with the given id from the array
*
* URL: localhost:3000/todo/delete/:id
* Example: localhost:3000/todo/delete/1
*/
app.delete("/todos/delete/:id", function (req, res) {
// get the todo id from the request parameters and convert it to integer
const todoId = parseInt(req.params.id);
// read the todos from the file
let todos = readTodosFromFile();
// create a deleted variable and set it to false
let deleted = false;
// create a tempTodos array to store the todos after deleting the todo with the given id
const tempTodos = [];
// find the todo with the given id from the todos array and delete it
for (let i = 0; i < todos.length; i++) {
// if todo is found with the given id, set deleted to true and skip adding it to tempTodos
if (todos[i].id === todoId) {
deleted = true;
continue; // skip adding this todo to tempTodos
}
// add the todo to tempTodos array if it is not the one to be deleted
tempTodos.push(todos[i]);
}
// if todo is not found with the given id, send a response with message "Todo not found with id" and the todo id
if (!deleted) {
return res.send("Todo not found with id " + todoId);
}
// write the todos to the file
writeTodosToFile(tempTodos);
// send a response with message "Todo deleted successfully with id" and the todo id
res.send("Todo deleted successfully with id " + todoId);
});
/**
* create a route handler for PUT (Update) request
*
* Update the todos with the given id in the array
*
* URL: localhost:3000/todo/update/:id
* Example: localhost:3000/todo/update/1
*/
app.put("/todos/update/:id", function (req, res) {
// get the todo and todo id from the request body and parameters
const { todo } = req.body;
// get the todo id from the request parameters and convert it to integer
const todoId = parseInt(req.params.id);
// if todo is empty, send a response with message "Todo cannot be empty"
if (!todo || todo.trim() === "") {
return res.send("Todo cannot be empty");
}
// read the todos from the file
let todos = readTodosFromFile();
// create a updated variable and set it to false
let updated = false;
// find the todo with the given id from the todos array and update the title
for (let i = 0; i < todos.length; i++) {
// if todo is found with the given id, update the title and set updated to true
if (todos[i].id === todoId) {
todos[i].title = todo;
updated = true;
}
}
// if todo is not found with the given id, send a response with message "Todo not found with id" and the todo id
if (!updated) {
return res.send("Todo not found with id " + todoId);
}
// write the todos to the file
writeTodosToFile(todos);
// send a response with message "Todo updated successfully with id" and the todo id
res.send("Todo updated successfully with id " + todoId);
});
/**
* create a route handler for GET (Read) request
*
* Read all the todos from the array
*
* URL: localhost:3000/todo/read/all
* Example: localhost:3000/todo/read/all
*/
app.get("/todos/read/all", function (req, res) {
// read the todos from the file
let todos = readTodosFromFile();
// if no todos are found, send a response with message "No todos found"
if (todos.length === 0) {
return res.send("No todos found");
}
// send the todos array as response
res.send(todos);
});
/**
* create a route handler for GET (Read) request
*
* Read the todos with the given id from the array
*
* URL: localhost:3000/todos/read/:id
* Example: localhost:3000/todos/read/1
*/
app.get("/todos/read/:id", function (req, res) {
// get the todo id from the request parameters and convert it to integer
const todoId = parseInt(req.params.id);
// read the todos from the file
let todos = readTodosFromFile();
// find the todo with the given id from the todos array
const todo = todos.find((todo) => todo.id === todoId);
// if todo is not found, send a response with message "Todo not found with id" and the todo id
if (!todo) {
return res.send("Todo not found with id " + todoId);
}
// send the todo as response
res.send(todo);
});
// Start the server on port 3000
app.listen(3000, () => {
console.log("Server is running on port 3000");
});