-
Notifications
You must be signed in to change notification settings - Fork 41
/
fakeApi.js
77 lines (69 loc) · 1.46 KB
/
fakeApi.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
var _ = require('lodash');
var people = [
{
id: 1,
firstName: 'Henrik',
lastName: 'Joreteg',
coolnessFactor: 11
},
{
id: 2,
firstName: 'Bob',
lastName: 'Saget',
coolnessFactor: 2
},
{
id: 3,
firstName: 'Larry',
lastName: 'King',
coolnessFactor: 4
},
{
id: 4,
firstName: 'Diana',
lastName: 'Ross',
coolnessFactor: 6
},
{
id: 5,
firstName: 'Crazy',
lastName: 'Dave',
coolnessFactor: 8
},
{
id: 6,
firstName: 'Larry',
lastName: 'Johannson',
coolnessFactor: 4
}
];
var id = 7;
function get(id) {
return _.find(people, {id: parseInt(id + '', 10)});
}
exports.list = function (req, res) {
res.send(people);
};
exports.add = function (req, res) {
var person = req.body;
person.id = id++;
people.push(person);
res.status(201).send(person);
};
exports.get = function (req, res) {
var found = get(req.params.id);
res.status(found ? 200 : 404);
res.send(found);
};
exports.delete = function (req, res) {
var found = get(req.params.id);
if (found) people = _.without(people, found);
res.status(found ? 200 : 404);
res.send(found);
};
exports.update = function (req, res) {
var found = get(req.params.id);
if (found) _.extend(found, req.body);
res.status(found ? 200 : 404);
res.send(found);
};