-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.js
54 lines (44 loc) · 1.14 KB
/
handlers.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
import _ from 'lodash'
export default function (Model) {
return {
create: (req, res, next) => {
_.forEach(req.params, (value, key) => {
if (value === '') req.params[key] = null
})
Model.create(req.params, function (error, entity) {
if (error) {
res.send(400, {
message: error.message,
errors: error.errors
})
} else {
res.send(200, entity)
}
next()
})
},
read: async (req, res, next) => {
var result
if (req.params.id) {
result = await Model.findById(req.params.id) || ''
} else {
result = await Model.find()
}
res.send(result)
next()
},
update: async (req, res, next) => {
_.forEach(req.params, (value, key) => {
if (value === '') req.params[key] = null
})
var entity = await Model.findByIdAndUpdate(req.params.id, { $set: _.omit(req.params, 'id') }, { new: true })
res.send(entity)
next()
},
del: async (req, res, next) => {
await Model.findByIdAndRemove(req.params.id)
res.send(204)
next()
}
}
}