forked from bloominstituteoftechnology/node-api1-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
123 lines (105 loc) · 3.17 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
// implement your API here
const express = require('express');
const db = require('./data/db.js');
const server = express();
server.use(express.json());
server.get("/", (req, res) => {
res.send({ api: 'api is up and running...' });
});
// Creates a user using the information sent inside the `request body`.
server.post("/users", (req, res) => {
// Get the data the client sent
const userData = req.body; // Express does not know how to parse JSON
if (!userData.name || !userData.bio) {
res.status(400).json({
message: 'Please provide a name and bio for the user.'
});
}
// Call the db and add the user
db.insert(userData)
.then(({ id }) => {
db.findById(id)
.then(addedUser => res.status(201).json(addedUser))
.catch(() => res.status(500).json({
message: 'There was an error while saving the user to the database' }));
})
})
// Returns an array of all the user objects contained in the database.
server.get("/users", (req, res) => {
db.find()
.then(users => res.status(200).json(users))
.catch(() => res.status(500).json({
message: 'The users information could not be retrieved.'
}));
});
// Returns the user object with the specified `id`.
server.get("/users/:id", (req, res) => {
const { id } = req.params;
db.findById(id)
.then(user => {
if (user) {
res.status(200).json(user);
} else {
res.status(404).json({
message: 'The user with the specified ID does not exist.'
});
}
})
.catch(() => {
res.status(500).json({
message: 'The user information could not be retrieved.'
});
});
});
// Removes the user with the specified `id` and returns the deleted user.
server.delete("/users/:id", (req, res) => {
const { id } = req.params;
db.remove(id)
.then(deleted => {
if (deleted) {
res.status(200).json({
message: 'User removed successfully.'
});
} else {
res.status(404).json({
message: 'The user with the specified ID does not exist.'
});
}
})
.catch(() => {
res.status(500).json({
message: 'The user could not be removed.' });
});
});
// Updates the user with the specified `id` using data from the `request body`. Returns the modified document, NOT the origial.
server.put("/users/:id", (req, res) => {
const { id } = req.params;
const { name, bio } = req.body;
if (!name || !bio) {
res.status(400).json({
message: 'Please provide a name and bio for the user.'
});
return;
}
db.update(id, { name, bio })
.then(updated => {
if (updated) {
db.findById(id)
.then(user => res.status(200).json(user))
.catch(() => res.status(500).json({
message: 'The user informatmion could not be retrieved.'
}));
} else {
res.status(404).json({
message: 'The user with the specified ID does not exist.'
});
}
})
.catch(() => res.status(500).json({
message: 'The user information could not be modified.'
}));
})
const port = 4000;
server.listen(port, () =>
console.log(`\n ** API running on port ${port} **\n`)
);