-
Notifications
You must be signed in to change notification settings - Fork 16
/
store.js
84 lines (78 loc) · 2.2 KB
/
store.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
'use strict';
const t = require('koa-joi-router').Joi;
const Order = t.object().label('Order').keys({
id: t.number(),
petId: t.number(),
quantity: t.number(),
shipDate: t.date(),
status: t.string().valid(['placed', 'approved', 'delivered']),
complete: t.boolean()
});
const Quantity = t.number().integer().label('Quantity');
const storeInventory = {
method: 'get',
path: '/inventory',
meta: {
friendlyName: 'Store inventory',
description: 'Returns pet inventories by status',
extendedDescription: `
**Implementation notes**
* Returns a map of status codes to quantities
`
},
validate: {
output: {
200: {
body: {
available: Quantity.description('Pets available for sale'),
pending: Quantity.description('# of pets awaiting processing'),
sold: Quantity.description('# of pets sold')
}
},
400: {
body: {
code: t.number().integer().min(0).max(100).default(0).description('Code to explain the response.'),
errors: t.object().keys({
name: {
message: t.string().required().default('Some pet has no name!').description('Thrown when some pets has no name.')
}
}),
tags: t.array().items(t.object().keys({
label: t.string().example('Hello').example('World'),
signal: t.array().items(t.string())
})),
error: t.string().valid('Pets not found!').description('Pets not found!')
}
},
500: {
body: t.string().default('Server Internal Error.')
}
}
},
*handler () {
// This route does not have any validations
return this.db().table('store')
.groupBy('statusCode')
.map('quantity')
.run();
}
};
const orderPet = {
method: 'post',
path: '/order',
meta: {
friendlyName: 'Place an order for a pet'
},
validate: {
type: 'json',
body: Order
},
*handler () {
const order = this.request.body;
yield this.db().table('orders').insert(order).run();
}
};
module.exports = [
storeInventory,
orderPet
];