-
Notifications
You must be signed in to change notification settings - Fork 16
/
pets.js
104 lines (92 loc) · 2.23 KB
/
pets.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
100
101
102
103
104
'use strict';
const t = require('koa-joi-router').Joi;
const Category = t.object().label('Category').keys({
id: t.number(),
name: t.string()
});
const Tag = t.object().label('Tag').keys({
id: t.number(),
name: t.string()
});
const Pet = t.object().label('Pet').keys({
id: t.number().optional(),
name: t.string().required(),
category: Category,
tags: t.array().items(Tag),
photoUrls: t.array().items(t.string()).required(),
status: t.string()
.valid(['available', 'pending', 'sold'])
.description('pet status in the store')
});
const createPet = {
method: 'post',
path: '/pet',
meta: {
friendlyName: 'Add pet',
description: 'Add a new pet to the store'
},
validate: {
type: 'json',
body: Pet
},
*handler () {
const pet = this.request.body;
return yield this.db().table('pets').insert(pet).run();
}
};
const updatePet = {
method: 'put',
path: '/pet',
meta: {
friendlyName: 'Update pet',
description: 'Update an existing pet'
},
validate: {
type: 'json',
body: Pet
},
*handler () {
const pet = this.request.body;
if (!pet.id) this.throw(400, 'Invalid ID supplied');
const existing = this.db().get(pet.id).run();
if (!existing) this.throw(404, 'Pet not found');
return this.db().table('pets').update(pet).run();
}
};
const getPetByStatus = {
method: 'put',
path: '/pet',
meta: {
friendlyName: 'Find pets by status'
},
validate: {
type: 'json',
query: {
status: t.array()
.description('Status values that need to be considered for filter')
.items(t.string())
.min(1) // At least 1 should be provided
.single() // If only one is provided, wrap it in an array
},
output: {
200: {
body: t.array().items(Pet.requiredKeys('id', 'status')),
header: {
'Content-Type': t.string()
}
}
}
},
*handler () {
const query = this.request.query;
return yield this.db()
.table('pets')
.getAll(query.status)
.run();
}
};
module.exports = [
createPet,
updatePet,
getPetByStatus
];