From 64e8c03dea044752bf3f2f228462721fe565f950 Mon Sep 17 00:00:00 2001 From: Jules Laplace Date: Wed, 19 Jul 2017 00:50:05 +0200 Subject: refactor all the worker stuff --- lib/server/api.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lib/server/api.js (limited to 'lib/server/api.js') 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 -- cgit v1.2.3-70-g09d2