diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2017-07-19 00:50:05 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2017-07-19 00:50:05 +0200 |
| commit | 64e8c03dea044752bf3f2f228462721fe565f950 (patch) | |
| tree | 41b48b67f69979bfc97be166129ee41c8dcb0c7f /lib/server/api.js | |
| parent | 11a70bc347587219b2ec7b63cf4a6ff69bb4199b (diff) | |
refactor all the worker stuff
Diffstat (limited to 'lib/server/api.js')
| -rw-r--r-- | lib/server/api.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/server/api.js b/lib/server/api.js new file mode 100644 index 0000000..12adada --- /dev/null +++ b/lib/server/api.js @@ -0,0 +1,48 @@ +const db = require('../db') + +module.exports = function api (app, type) { + const type_s = '/' + type + 's/' + const type_id = type_s + ':id' + + const model = db.models[type] + + // index + app.get(type_s, (req, res) => { + console.log('index', type) + model.index(req.query).then( data => res.json(data) ) + }) + + // show + app.get(type_id, (req, res) => { + console.log('show', type, req.params.id) + model.show(req.params.id).then( (data) => { + res.json(data) + }) + }) + + // create + app.post(type_s, (req, res) => { + console.log('create', type) + model.create(req.body).then( (data) => { + res.json(data) + })// .catch( () => res.sendStatus(500) ) + }) + + // update + app.put(type_id, (req, res) => { + console.log('update', type, req.params.id) + model.update(req.body.id, req.body).then( (data) => { + res.json(data) + })// .catch( () => res.sendStatus(500) ) + }) + + // destroy + app.delete(type_id, (req, res) => { + console.log('destroy', type, req.params.id) + model.destroy(req.params.id).then( (data) => { + res.json(data) + })// .catch( () => res.sendStatus(500) ) + }) + + return model +}
\ No newline at end of file |
