-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy pathindex.js
More file actions
206 lines (170 loc) · 6.12 KB
/
index.js
File metadata and controls
206 lines (170 loc) · 6.12 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
/*
Assignment #1 - Trying to code a todo app and store data into the array
*/
// import express module using require function and store it in express variable
const express = require("express");
// create an express application using express function
const app = express();
// Middleware to parse JSON data in the request body
app.use(express.json());
// To store the todos in memory, create an empty array
let todos = [];
/**
* 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");
}
// 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);
// 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) => {
// delete all the todos from the array
todos = [];
// 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);
// 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
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);
}
// update the todos array with the temporary array
todos = 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");
}
// 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);
}
// 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) {
// 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);
// 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");
});