forked from bloominstituteoftechnology/webapi-iii-challenge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
99 lines (85 loc) · 2.22 KB
/
Copy pathserver.js
File metadata and controls
99 lines (85 loc) · 2.22 KB
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
const express = require('express');
const cors = require('cors');
const posts = require('./data/helpers/postDb.js');
const users = require('./data/helpers/userDb.js');
const tags = require('./data/helpers/tagDb.js');
const port = 5001;
const server = express();
server.use(express.json());
server.use(cors());
const sendUserError = (status, message, res) => {
res.status(staus).json({ errorMessage: message });
return;
}
server.get('/api/users', (req, res) => {
users.get()
.then(posts => {
res.json(posts);
})
});
server.get('/api/users/:id', (req, res) => {
const { id } = req.params;
users.get(id)
.then(response => {
res.json(response);
})
.catch(error => {
console.log(error);
})
});
server.get('/api/user/posts/:id', (req, res) => {
const { id } = req.params;
users.getUserPosts(id)
.then(users => {
res.json({ users });
})
.catch(error => {
res.json({ error });
});
})
server.get('/api/post/tags/:id', (req, res) => {
const { id } = req.params;
posts.getPostTags(id)
.then(posts => {
res.json({ posts });
})
.catch(error => {
res.json({ error });
});
})
server.post('/api/users', (req, res) => {
const { name } = req.body;
users.insert({name})
.then(user => {
res.json( user );
})
.catch(error => {
console.log(error);
})
})
server.delete('/api/users/:id', (req, res) => {
const { id } = req.params;
users.remove(id)
.then(response => {
if (response === 1) {
res.send('hello you have deleted');
} else {
res.send('sorry cant find that');
}
})
.catch(error => {
console.log(error);
})
})
server.put('/api/users/:id', (req, res) => {
const { id } = req.params;
const { name } = req.body;
users.update(id, {name})
.then(response => {
res.json(response);
})
.catch(error => {
console.log(error);
})
})
server.listen(port, () => console.log(`Server running on port, ${port}`));