forked from geek/hapi-example
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathroutes.js
More file actions
85 lines (73 loc) · 2.12 KB
/
routes.js
File metadata and controls
85 lines (73 loc) · 2.12 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
'use strict';
const Joi = require('joi');
const Chance = require('chance');
let internals = {};
const chance = new Chance();
internals.getProducts = (request, reply) => {
if (request.query.name) {
return reply(internals.findProducts(request.query.name));
}
reply(internals.products);
}
internals.findProducts = function(name) {
return internals.products.filter(function(product) {
return product.name === name;
});
}
internals.getProduct = function(request, reply) {
const filtered = internals.products.filter(function(product) {
return product.id === parseInt(request.params.id);
});
reply(filtered.pop());
}
internals.addProduct = function(request, reply) {
const product = {
id: internals.products[internals.products.length - 1].id + 1,
name: request.payload.name,
description: request.payload.description,
price: request.payload.price
};
internals.products.push(product);
reply(product).created('/products/' + product.id);
}
internals.products = [];
// Populate the products with a variable amount of random data
const numProductsToCreate = chance.integer({ min: 100, max: 1000 })
for (let id = 0; id < numProductsToCreate; id++) {
internals.products.push({
id,
name: chance.animal(),
description: chance.sentence(),
price: chance.floating({ fixed: 2, min: 1, max: 20000 })
})
}
module.exports.internals = internals;
module.exports.routes = [{
method: 'GET',
path: '/products',
config: {
validate: {
query: {
name: Joi.string()
}
},
handler: internals.getProducts
}
}, {
method: 'GET',
path: '/products/{id}',
handler: internals.getProduct
}, {
method: 'POST',
path: '/products',
config: {
validate: {
payload: {
name: Joi.string().required().min(3).max(100),
description: Joi.string().min(5).max(1000),
price: Joi.number().required().min(1).max(20000).precision(2)
}
},
handler: internals.addProduct
},
}];