forked from latteiscoding/kanbas-node-server-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab5.js
221 lines (203 loc) · 6.16 KB
/
Lab5.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
const assignment = {
id: 1, title: "NodeJS Assignment",
description: "Create a NodeJS server with ExpressJS",
due: "2021-10-10", completed: false, score: 0,
};
const module = {
id: 1, name: "NodeJS Module",
description: "Learn NodeJS with ExpressJS",
course: "RS101"
};
// array of objects
const todos = [
{ id: 1, title: "Task 1", completed: false },
{ id: 2, title: "Task 2", completed: true },
{ id: 3, title: "Task 3", completed: false },
{ id: 4, title: "Task 4", completed: true },
];
const Lab5 = (app) => {
/*
app.get("/a5/todos", (req, res) => {
res.json(todos);
});*/
// create using post
app.post("/a5/todos", (req, res) => {
const newTodo = {
...req.body,
id: new Date().getTime(),
};
todos.push(newTodo);
res.json(newTodo);
});
// this is kept only because we do not want to break the previous code
app.get("/a5/todos/create", (req, res) => {
const newTodo = {
id: new Date().getTime(),
title: "New Task",
completed: false,
};
todos.push(newTodo);
res.json(todos);
});
app.get("/a5/todos", (req, res) => {
res.json(todos);
});
// update a todo's title using the given id.
app.get("/a5/todos/:id/title/:title", (req, res) => {
const { id, title } = req.params;
const todo = todos.find((t) => t.id === parseInt(id));
todo.title = title;
res.json(todos);
});
// update using put
app.put("/a5/todos/:id", (req, res) => {
const { id } = req.params;
const todo = todos.find((t) => t.id === parseInt(id));
if (!todo) {
res.status(404)
.json({ message: `Unable to update Todo with ID ${id}` });
return;
}
todo.title = req.body.title;
todo.description = req.body.description;
todo.due = req.body.due;
todo.completed = req.body.completed;
res.sendStatus(200);
});
// elder version of update completed
app.get("/a5/todos/:id/completed/:completed", (req, res) => {
const { id, completed } = req.params;
const todo = todos.find((t) => t.id === parseInt(id));
todo.completed = completed === "true";
res.json(todos);
});
// elder version of update description
app.get("/a5/todos/:id/description/:description", (req, res) => {
const { id, description } = req.params;
const todo = todos.find((t) => t.id === parseInt(id));
todo.description = description;
res.json(todos);
});
// reimplementation of delete -- convention : URL path should include only nouns and no verbs
// simply returns a simple ok status
app.delete("/a5/todos/:id", (req, res) => {
const { id } = req.params;
const todo = todos.find((t) => t.id === parseInt(id));
if (!todo) { // error handling if the todo is not found
res.status(404)
.json({ message: `Unable to delete Todo with ID ${id}` });
return;
}
todos.splice(todos.indexOf(todo), 1);
res.sendStatus(200);
});
// elder version of delete
app.get("/a5/todos/:id/delete", (req, res) => {
const { id } = req.params;
const todo = todos.find((t) => t.id === parseInt(id));
const todoIndex = todos.indexOf(todo);
if (todoIndex !== -1) {
todos.splice(todoIndex, 1);
}
res.json(todos);
});
app.get("/a5/todos/:id", (req, res) => {
const { id } = req.params;
const todo = todos.find((t) => t.id === parseInt(id));
res.json(todo);
});
/*
app.get("/a5/todos", (req, res) => {
const { completed } = req.query;
if (completed !== undefined) {
const completedBool = completed === "true";
const completedTodos = todos.filter(
(t) => t.completed === completedBool);
res.json(completedTodos);
return;
}
});*/
app.get("/a5/assignment", (req, res) => {
res.json(assignment); // use json instead of send if you know the response is formatted as JSON
});
app.get("/a5/assignment/title", (req, res) => {
res.json(assignment.title);
});
// change objects in the server
// persist as long as the server is running
// rebooting the server to reset the object state
app.get("/a5/assignment/title/:newTitle", (req, res) => {
const { newTitle } = req.params;
assignment.title = newTitle;
res.json(assignment);
});
// update the assignment score
app.get("/a5/assignment/score/:newScore", (req, res) => {
const { newScore } = req.params;
assignment.score = newScore;
res.json(assignment);
});
//update the assignment completed status
app.get("/a5/assignment/completed/:newCompleted", (req, res) => {
const { newCompleted } = req.params;
assignment.completed = newCompleted;
res.json(assignment);
});
// practice with module object
app.get("/a5/module", (req, res) => {
res.json(module);
});
app.get("/a5/module/name", (req, res) => {
res.json(module.name);
});
app.get("/a5/module/name/:newName", (req, res) => {
const { newName } = req.params;
module.name = newName;
res.json(module);
});
app.get("/a5/welcome", (req, res) => {
res.send("Welcome to Assignment 5");
});
app.get("/a5/add/:a/:b", (req, res) => {
const { a, b } = req.params;
const sum = parseInt(a) + parseInt(b);
res.send(sum.toString());
});
app.get("/a5/subtract/:a/:b", (req, res) => {
const { a, b } = req.params;
const sum = parseInt(a) - parseInt(b);
res.send(sum.toString());
});
app.get("/a5/multiply/:a/:b", (req, res) => {
const { a, b } = req.params;
const product = parseInt(a) * parseInt(b);
res.send(product.toString());
});
app.get("/a5/divide/:a/:b", (req, res) => {
const { a, b } = req.params;
const quotient = parseInt(a) / parseInt(b);
res.send(quotient.toString());
});
app.get("/a5/calculator", (req, res) => {
const { a, b, operation } = req.query;
let result = 0;
switch (operation) {
case "add":
result = parseInt(a) + parseInt(b);
break;
case "subtract":
result = parseInt(a) - parseInt(b);
break;
case "multiply":
result = parseInt(a) * parseInt(b);
break;
case "divide":
result = parseInt(a) / parseInt(b);
break;
default:
result = "Invalid operation";
}
res.send(result.toString());
});
};
export default Lab5;