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
|
const express = require('express');
const uuid = require('uuid/v4');
const router = express.Router();
const members = require('../../Members');
// Gets all members
router.get('/', (req, res) => res.json(members));
// Get single member
router.get('/:id', (req, res) => {
const found = members.some(member => member.id === parseInt(req.params.id));
if(found) {
res.json(members.filter(member =>
member.id === parseInt(req.params.id)));
} else {
res.status(400).json({ msg: `No member with the id of ${req.params.id}`
});
}
});
// Create Member
router.post('/', (req, res) => {
const newMember = {
id: uuid(),
name: req.body.name,
email: req.body.email,
status: 'active'
}
if(!newMember.name || !newMember.email) {
return res.status(400).json({ msg: 'Please populate name and email' });
}
members.push(newMember);
res.json(members);
});
// Updat Member
router.put('/:id', (req, res) => {
const found = members.some(member => member.id === parseInt(req.params.id));
if (found) {
const updMember = req.body;
members.forEach(member => {
if(member.id === parseInt(req.params.id)) {
member.name = updMember.name ? updMember.name : member.name;
member.email = updMember.email ? updMember.email : member.email;
res.json({ msg: 'Member updated', member });
}
});
} else {
res.status(400).json({ msg: `No member with the id of ${req.prams.id}`
});
}
});
// Delete member
router.delete('/:id', (req, res) => {
const found = members.some(member => member.id === parseInt(req.params.id));
if(found) {
res.json({ msg: 'Member deleted',
members: members.filter(member =>
member.id !== parseInt(req.params.id))});
res.json(members.filter(member =>
member.id !== parseInt(req.params.id)));
} else {
res.status(400).json({ msg: `No member with the id of ${req.params.id}`
});
}
});
module.exports = router;
|