-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
96 lines (85 loc) · 2.23 KB
/
server.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
const express = require("express");
const User = require("./models").user;
const TodoList = require("./models").todoList
const TodoItem = require("./models").todoItem
const PORT = 4000;
const app = express();
app.use(express.json()); //parse the body
//get users
//http :4000/users
app.get("/users", async (request, response, next) => {
try {
const users = await User.findAll();
response.send(users);
} catch (e) {
console.log(e.message);
next(e);
}
});
//get one user with lists and items
//http :4000/users/3
app.get("/users/:id", async (req, res, next) => {
try {
// 1. req.params.id;
const userId = req.params.id;
// 2. findByPk => id
const theUser = await User.findByPk(userId, { include:
{ model: TodoList, include: [TodoItem]}
});
res.send(theUser);
} catch (e) {
next(e);
}
});
// axios.post("/signup", { name: 'Matias', email: '[email protected]' });
app.post("/signup", async (req, res, next) => {
try {
// email, name => frontend
const { email, name } = req.body;
const newUser = await User.create({ name, email });
res.send(newUser);
} catch (e) {
console.log(e.message);
next(e);
}
});
//update user
//http PATCH :4000/user/5 name=ABCD
app.patch("/user/:id", async (req, res, next) => {
try {
//1. get the id from the params
const { id } = req.params
//1.1 get the info from the body
const { name, address } = req.body
//2. find the user to update
const userToUpdate = await User.findByPk(id)
if (!userToUpdate) {
res.status(404).send("User not found")
}
//3. update the user
const updated = await userToUpdate.update({ name, address })
//4. send a response
res.send(updated)
} catch (e){
console.log(e.message)
next(e)
}
})
//delete user
//http DELETE :4000/user/5
app.delete("/user/:id", async (req, res, next) => {
try {
//1.get the id from the params
const { id } = req.params
//2. find what you want to delete
const userToDelete = await User.findByPk(id)
//3. delete
await userToDelete.destroy()
//4. send a response
res.send("User teminated")
} catch (e){
console.log(e.message)
next(e)
}
})
app.listen(PORT, () => console.log("Listening on port 4000"));