diff options
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 |
