diff options
Diffstat (limited to 'lib/server')
| -rw-r--r-- | lib/server/api.js | 48 | ||||
| -rw-r--r-- | lib/server/index.js | 124 |
2 files changed, 54 insertions, 118 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 diff --git a/lib/server/index.js b/lib/server/index.js index c6176fe..f8cea0c 100644 --- a/lib/server/index.js +++ b/lib/server/index.js @@ -13,7 +13,7 @@ const Loader = require('../vendor/Loader') let app, server, io -const db = require('../db') +const api = require('./api') const site = module.exports = {} @@ -33,18 +33,10 @@ site.init = function(){ bridge.connectSocketIo(io) - const api_folders = crud(app, 'folder', db.Folder, { - afterCreate: (folder) => { - fs.mkdir('data/' + folder.get('id') + '/', function(){ - console.log('created folder', folder.get('id'), folder.get('name')) - }) - } - }) - const api_files = crud(app, 'file', db.File) - const api_jobs = crud(app, 'job', db.Job) - const api_tasks = crud(app, 'task', db.Task, { - hasOne: { content_file_id: db.File, style_file_id: db.File } - }) + const api_folders = api(app, 'folder') + const api_files = api(app, 'file') + const api_jobs = api(app, 'job') + const api_tasks = api(app, 'task') app.get('/devices', (req, res) => { res.json( bridge.devices ) @@ -70,7 +62,7 @@ site.init = function(){ data[fn] = false loader.register(fn) fs.rename(file.path, 'data/' + req.params.id + '/' + fn, function(err){ - api_files.create({ + api_files.crud.create({ // table.string('username') 'folder_id': req.params.id, 'name': fn, @@ -88,108 +80,4 @@ site.init = function(){ }) loader.ready('upload') }) - - function crud(app, type_s, model, callbacks){ - callbacks = callbacks || {} - const type = '/' + type_s + 's/' - const type_id = type + ':id' - - const crud = db.crud(model) - - // index - app.get(type, (req, res) => { - console.log('index', type) - crud.index(req.query).then( (data) => { - - if (! callbacks.hasOne) { - res.json(data ? data.toJSON() : []) - } - else { - let recs = data.toJSON() - const loader = new Loader () - loader.onReady( () => { - console.log(type, 'ready') - res.json(recs) - }) - console.log('hasOne') - loader.register('hasOne') - Object.keys(callbacks.hasOne).forEach( (key,i) => { - loader.register(key) - console.log('key') - const type = callbacks.hasOne[key] - const id_lookup = {} - recs.forEach(r => { - const id = r[key] - id_lookup[id] = id_lookup[id] || [] - id_lookup[id].push(r) - }) - console.log(key, recs.length, Object.keys(id_lookup).length) - db.crud(type).show_ids(Object.keys(id_lookup)).then( (sub_recs) => { - console.log(key, 'sub_recs', sub_recs) - const short_key = key.replace('_id','') - sub_recs.toJSON().forEach(rec => { - id_lookup[rec.id].forEach( parent_rec => parent_rec[short_key] = rec ) - }) - loader.ready(key) - }) - }) - loader.ready('hasOne') - } - }) // }).catch( () => res.sendStatus(500) ) - }) - - // show - app.get(type_id, (req, res) => { - console.log('show', type, req.params.id) - crud.show(req.params.id).then( (data) => { - if (! callbacks.hasOne) { - res.json(data.toJSON()) - } - else { - rec = data.toJSON() - const loader = new Loader () - loader.onReady( () => { - res.json(rec) - }) - loader.register('hasOne') - Object.keys(callbacks.hasOne).forEach( (key,i) => { - loader.register(key) - const type = callbacks.hasOne[key] - db.crud(type).show(rec[key]).then( (sub_rec) => { - rec[key] = sub_rec - loader.ready(key) - }) - }) - loader.ready('hasOne') - } - }) // .catch( (err) => res.sendStatus(500) ) - }) - - // create - app.post(type, (req, res) => { - console.log('create', type) - crud.create(req.body).then( (data) => { - res.json(data.toJSON()) - callbacks.afterCreate && callbacks.afterCreate(data) - })// .catch( () => res.sendStatus(500) ) - }) - - // update - app.put(type_id, (req, res) => { - console.log('update', type, req.params.id) - crud.update(req.body.id, req.body).then( (data) => { - res.json(data.toJSON()) - })// .catch( () => res.sendStatus(500) ) - }) - - // destroy - app.delete(type_id, (req, res) => { - console.log('destroy', type, req.params.id) - crud.destroy(req.params.id).then( (data) => { - res.json(data.toJSON()) - })// .catch( () => res.sendStatus(500) ) - }) - - return crud - } } |
