-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLab5.js
189 lines (185 loc) · 6.38 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
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: "Web Development",
description: "Web Development using NodeJS",
course: "CS 5610 Web Development",
};
const todos = [
{ id: 1, title: "Task 1", completed: false, description: "Create a NodeJS server with ExpressJS", due: "2021-09-09" },
{ id: 2, title: "Task 2", completed: true, description: "Description", due: "2021-09-09"},
{ id: 3, title: "Task 3", completed: false, description: "Description", due: "2021-09-09"},
{ id: 4, title: "Task 4", completed: true, description: "Description", due: "2021-09-09" },
];
const Lab5 = (app) => {
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 sum = parseInt(a) * parseInt(b);
res.send(sum.toString());
});
app.get("/a5/divide/:a/:b", (req, res) => {
const { a, b } = req.params;
const sum = parseInt(a) / parseInt(b);
res.send(sum.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());
});
app.get("/a5/assignment", (req, res) => {
res.json(assignment);
});
app.get("/a5/assignment/title", (req, res) => {
res.json(assignment.title);
});
app.get("/a5/assignment/title/:newTitle", (req, res) => {
const { newTitle } = req.params;
assignment.title = newTitle;
res.json(assignment);
});
app.get("/a5/assignment/score/:newScore", (req, res) => {
const { newScore } = req.params;
assignment.score = newScore;
res.json(assignment);
});
app.get("/a5/assignment/completed/:newCompleted", (req, res) => {
const { newCompleted } = req.params;
assignment.completed = newCompleted;
res.json(assignment);
});
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/module/description/:newDescription", (req, res) => {
const { newDescription } = req.params;
module.description = newDescription;
res.json(module);
});
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/: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);
});
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/description/:description", (req, res) => {
const { id, description } = req.params;
const todo = todos.find((t) => t.id === parseInt(id));
todo.description = description;
res.json(todo);
});
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;
res.json(todos);
});
app.delete("/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 delete Todo with ID ${id}` });
return;
}
todos.splice(todos.indexOf(todo), 1);
res.sendStatus(200);
});
app.get("/a5/todos/:id", (req, res) => {
const { id } = req.params;
const todo = todos.find((t) => t.id === parseInt(id));
res.json(todo);
});
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);
});
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;
}
res.json(todos);
});
app.post("/a5/todos", (req, res) => {
const newTodo = {
...req.body,
id: new Date().getTime(),
};
todos.push(newTodo);
res.json(newTodo);
});
};
export default Lab5;