-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
114 lines (95 loc) · 3.03 KB
/
index.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
105
106
107
108
109
110
111
112
113
114
'use strict'
const inflect = require('i')()
// class RestfulController {
class RestfulController {
// create - POST /api/:resource
* store (request, response) {
const model = this.resource(request.param('resource'))
const data = request.input('model')
const result = yield model.create(data)
// todo: better response? handleError
response.json(result)
}
// readMany - GET /api/:resource
* index (request, response) {
const model = this.resource(request.param('resource'))
const query = model.query()
const where = JSON.parse(request.input('query'))
if (where) {
query.where(where)
}
const results = yield query.fetch()
response.json(results)
}
// query - POST /api/:resource/query
* query (request, response) {
console.log('query():request', request.params(), request.get())
const model = this.resource(request.param('resource'))
const query = model.query()
const where = request.input('where')
console.log('query():where', where, typeof where)
if (where) {
query.where(where)
}
const related = request.input('related')
console.log('query():related', related)
if (related) {
query.with(related)
}
const orderBy = request.input('orderBy')
console.log('query():orderBy', orderBy)
if (orderBy) {
query.orderBy(orderBy.column, orderBy.direction)
}
const limit = request.input('limit')
console.log('query():limit', limit)
if (limit) {
query.limit(limit)
}
const offset = request.input('offset')
console.log('query():offset', offset)
if (offset) {
query.offset(offset)
}
const results = yield query.fetch()
// console.log('index():results', results)
response.json(results)
}
// readOne - GET /api/:resource/:id
* show (request, response) {
console.log('show()', request.all())
const model = this.resource(request.param('resource'))
const query = model.query().where({ id: request.param('id') })
const related = request.input('related')
console.log('show():related', related)
if (related) {
query.with(related)
}
const result = yield query.first()
console.log('show():result', result)
response.json(result)
}
// update - PATCH /api/:resource/:id
* update (request, response) {
const model = this.resource(request.param('resource'))
const data = request.input('model')
const instance = yield model.find(request.param('id'))
const result = yield instance.update(data)
response.json(result)
}
// delete - DELETE /api/:resource/:id
* destroy (request, response) {
const model = this.resource(request.param('resource'))
const record = yield model.find(request.param('id'))
const result = yield record.delete()
response.json(result)
}
// return model instance from :resource
resource (resource) {
const _model = 'App/Model/' + inflect.classify(resource)
const model = use(_model)
return model
}
}
// module.exports = RestfulController
module.exports = RestfulController