-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
319 lines (281 loc) · 7.48 KB
/
index.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
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
require("dotenv").config();
const express = require("express");
const cors = require("cors");
const http = require("http");
const bodyParser = require("body-parser");
const bcrypt = require("bcrypt");
const jwt = require("jsonwebtoken");
const {
getUserById,
getUserByEmail,
createUser,
editUser,
getToDosById,
getToDoLists,
createToDoList,
createToDo,
editToDo,
getToDoById,
getToDoListById,
deleteToDoById,
deleteToDoListById,
shareToDoListWithUser,
checkedToDo,
} = require("./services/database");
// Express Server
const port = process.env.PORT;
const secret = process.env.SECRET;
const app = express();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// Server functions
app.get("/", (req, res) => {
res.send({ message: "Hello from Planner API!" });
});
app.get("/users/:userid", async (req, res) => {
try {
const userId = req.params.userid;
const user = await getUserById(userId);
res.send(user);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
app.post("/signup", async (req, res) => {
const { firstname, surname, email, password, img } = req.body;
// Hashing password
const saltRounds = 10;
const hashedPassword = await bcrypt.hash(password, saltRounds);
try {
const newUser = await createUser(
firstname,
surname,
email,
hashedPassword,
img
);
res.send(newUser);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
app.put("/users/:userid", async (req, res) => {
const { id, firstname, surname, email, img } = req.body;
const user = await getUserById(id);
try {
const updatedUser = await editUser(id, firstname, surname, email, img);
console.log(updatedUser);
res.send(updatedUser);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
//With password edit
// app.put("/users/:userid", async (req, res) => {
// const { id, firstname, surname, email, password, img } = req.body;
// // checks if password is changed for re-hashing
// const user = await getUserById(id);
// const passwordIsChanged = user.password !== password;
// let newPassword = "";
// if (passwordIsChanged) {
// const saltRounds = 10;
// newPassword = await bcrypt.hash(password, saltRounds);
// }
// try {
// const updatedUser = editUser(
// id,
// firstname,
// surname,
// email,
// passwordIsChanged ? newPassword : password,
// img
// );
// res.send(updatedUser);
// } catch (error) {
// console.log(error);
// res.status(500).send({
// error: "Unable to contact database - please try again",
// });
// }
// });
app.post("/login", async (req, res) => {
const { email, password } = req.body;
try {
const user = await getUserByEmail(email);
if (!user) {
return res.status(401).send({ error: "Unknown user" });
}
// Load hash from your password DB
const isCorrectPassword = await bcrypt.compare(password, user.password);
if (!isCorrectPassword) {
console.log("not correct password");
return res.status(401).send({ error: "Wrong password" });
} else {
const token = jwt.sign(
{
id: user.id,
email: user.email,
firstname: user.firstname,
surname: user.surname,
},
Buffer.from(secret, "base64")
);
res.send({
token: token,
});
}
} catch (error) {
res.status(500).send({ error: error.message });
}
});
app.get("/todolists/:userId", async (req, res) => {
const toDoLists = await getToDoLists(req.params.userId);
res.status(200).send(toDoLists);
});
app.get("/todos/:id", async (req, res) => {
try {
const todolistId = req.params.id;
const todos = await getToDosById(todolistId);
res.send(todos);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
app.post("/todolists", async (req, res) => {
const { name, owner, day } = req.body;
try {
const newToDoList = await createToDoList(
name,
new Date().toISOString(),
owner,
day
);
await shareToDoListWithUser(owner, newToDoList.id);
res.send(newToDoList);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
app.post("/todo", async (req, res) => {
const { text, startTime, checked, todolistId } = req.body;
try {
const newToDo = await createToDo(text, startTime, checked, todolistId);
res.send(newToDo);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
app.put("/todo/:id", async (req, res) => {
const { id, text, startTime } = req.body;
try {
const newToDo = await editToDo(id, text, startTime);
res.send(newToDo);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
app.put("/checktodo/:todoId", async (req, res) => {
const { checked } = req.body;
try {
const newToDo = await checkedToDo(req.params.todoId, checked);
res.send(newToDo);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
app.get("/todo/:id", async (req, res) => {
try {
const todoId = req.params.id;
const todos = await getToDoById(todoId);
res.send(todos);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
app.get("/todolist/:id", async (req, res) => {
try {
const todoListId = req.params.id;
const todoList = await getToDoListById(todoListId);
res.send(todoList);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
app.delete("/todo/:id", async (req, res) => {
try {
const todoId = req.params.id;
const todo = await deleteToDoById(todoId);
res.status(200).send(todo);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
app.delete("/todolist/:id", async (req, res) => {
try {
const todolistId = req.params.id;
const todolist = await deleteToDoListById(todolistId);
res.status(200).send(todolist);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
app.post("/sharetodolist/:todolistId", async (req, res) => {
const { email } = req.body;
try {
const user = await getUserByEmail(email);
if (!user) {
return res.status(404).send({ error: "Unknown user" });
}
const shareToDoList = await shareToDoListWithUser(
user.id,
req.params.todolistId
);
res.send(shareToDoList);
} catch (error) {
console.log(error);
res.status(500).send({
error: "Unable to contact database - please try again",
});
}
});
var server = http.createServer(app);
server.listen(port, () => {
console.log(`Planner API listening on port ${port}`);
});