-
Notifications
You must be signed in to change notification settings - Fork 1
/
rules.js
32 lines (30 loc) · 843 Bytes
/
rules.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
session.counter = session.counter || 1;
session.pets = session.pets || [];
rules['/pet'] = function () {
if (request.post) {
let pet = request.body;
pet.id = session.counter++;
session.pets.push(pet);
response.body = pet;
}
};
rules['/pet/{petId}'] = function() {
let id = ~~request.pathParams.petId;
let index = session.pets.findIndex(p => p.id === id);
if (request.post) {
let pet = request.body;
pet.id = id;
session.pets[index] = pet;
response.body = pet;
} else if (request.delete) {
session.pets.splice(index, 1);
response.status = 201;
} else {
if (index === -1) {
response.status = 404;
response.body = null;
} else {
response.body = session.pets[index];
}
}
};