diff options
| author | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-11 01:40:03 -0400 |
|---|---|---|
| committer | Sean Fridman <fridman@mail.sfsu.edu> | 2015-04-11 01:40:03 -0400 |
| commit | 6e40f185d99cbe7ad0a38166e0c4095b5b445f3e (patch) | |
| tree | 1bfa8b54189b27a4f3ddbc74ddf36f8252b14770 | |
| parent | a20297451b88c604b16a35223be4b25528713c6d (diff) | |
Add batch update endpoint for admin
| -rw-r--r-- | app/node_modules/okadminview/index.js | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/app/node_modules/okadminview/index.js b/app/node_modules/okadminview/index.js index 884c4b8..897c583 100644 --- a/app/node_modules/okadminview/index.js +++ b/app/node_modules/okadminview/index.js @@ -173,6 +173,40 @@ function OKAdminView(options) { } }); + router.put('/:type/__batch/', function putBatch(req, res, next) { + var type = req.params.type; + var body = req.body || {}; + var resourcesJSON = body[type]; + var resource = resourceCache.get(type); + if (!resourcesJSON || !resourcesJSON.length) { + res.status(400); + errorHandler(req, res)(new Error('Bad request')); + } else if (!resource) { + res.status(404); + errorHandler(req, res)(new Error('No such resource')); + } else { + try { + var ids = []; + var resourcesParsed = resourcesJSON.map(function(resourceJSON) { + var data = JSON.parse(resourceJSON); + ids.push(resource.getID(data)); + return data; + }); + } catch (e) { + errorHandler(req, res)(new Error('Resource batch contains invalid JSON')); + return; + } + Q.all([ + meta.get(), + resource.updateBatch(ids, resourcesParsed), + ]).then(function(results) { + var metadata = results.shift(); + req.flash('success', {action: 'batch_update'}); + res.redirect(303, '../..'); + }).fail(errorHandler(req, res)); + } + }); + router.put('/:type/:id/', function updateResource(req, res, next) { var type = req.params.type; var id = req.params.id; |
