-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfakeApi.js
99 lines (91 loc) · 2.15 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
var _ = require('underscore');
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 _.findWhere(people, {id: parseInt(id + '', 10)});
}
exports.name = 'fake_api';
exports.version = '0.0.0';
exports.register = function (plugin, options, next) {
plugin.route({
method: 'GET',
path: '/api/people',
handler: function (request, reply) {
reply(people);
}
});
plugin.route({
method: 'POST',
path: '/api/people',
handler: function (request, reply) {
var person = request.payload;
person.id = id++;
people.push(person);
reply(person).code(201);
}
});
plugin.route({
method: 'GET',
path: '/api/people/{id}',
handler: function (request, reply) {
var found = get(request.params.id);
reply(found).code(found ? 200 : 404);
}
});
plugin.route({
method: 'DELETE',
path: '/api/people/{id}',
handler: function (request, reply) {
var found = get(request.params.id);
if (found) people = _.without(people, found);
reply(found).code(found ? 200 : 404);
}
});
plugin.route({
method: 'PUT',
path: '/api/people/{id}',
handler: function (request, reply) {
var found = get(request.params.id);
if (found) _.extend(found, request.payload);
reply(found).code(found ? 200 : 404);
}
});
next();
};