-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild_controller.js
More file actions
69 lines (56 loc) · 1.6 KB
/
build_controller.js
File metadata and controls
69 lines (56 loc) · 1.6 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
const CONFIG = require('config')
const __ = CONFIG.universalPath
const _ = __.require('lib', 'utils')
const error_ = __.require('lib', 'error')
const buildLib = require('./build_lib')
module.exports = function (typeName) {
const model = require(`./models/types/${typeName}`)
const lib = buildLib(typeName, model)
return {
get: function (req, res) {
const id = req.params.id
if (!_.isUuid(id)) {
var query = (id == 'all') ? '' : id
lib.filter(query)
.then(_.Log('get with !uuid'))
.then(res.json.bind(res))
.catch(error_.Handler(res))
} else {
lib.currentVersionById(id)
.then(res.json.bind(res))
.catch(error_.Handler(res))
}
},
post: function (req, res) {
if (req.files) {
lib.upload(req.body, req.files.mediaUpload) // code smell: hard coded input id, configurable, or per type?
.then(res.json.bind(res))
.catch(error_.Handler(res))
} else {
lib.create(req.body)
.then(res.json.bind(res))
.catch(error_.Handler(res))
}
},
put: function (req, res) {
const id = req.params.id
if (!_.isUuid(id)) {
error_.bundle(res, 'invalid id', 400, id)
return
}
lib.update(req.body, id)
.then(res.json.bind(res))
.catch(error_.Handler(res))
},
delete: function (req, res) {
const id = req.params.id
if (!_.isUuid(id)) {
error_.bundle(res, 'invalid id', 400, id)
return
}
lib.delete(id)
.then(res.json.bind(res))
.catch(error_.Handler(res))
}
}
}