diff --git a/lib/controllers/jobs.js b/lib/controllers/jobs.js index d7c3da1..6ee5989 100644 --- a/lib/controllers/jobs.js +++ b/lib/controllers/jobs.js @@ -21,6 +21,21 @@ function main (app) { }); } + function alter (action, message, req, res) { + app.models.job[action](req.params.id, function (err, job) { + if (err) { + res.json(422, err); + } else { + if (job) { + res.json(201, job); + } else { + res.json(404, {message: 'Job not found with id ' + req.params.id + (message || ' and that is available to '+ action)}); + } + } + }); + + } + return { @@ -128,17 +143,7 @@ function main (app) { // PUT /jobs/:id/take // take: function (req,res) { - app.models.job.take(req.params.id, function(err, job){ - if (err) { - res.json(422, err); - } else { - if (job) { - res.json(201, job); - } else { - res.json(404, {message: 'Job not found with id ' + req.params.id + ' and that is available to take'}); - } - } - }); + alter('take', null, req, res); }, @@ -146,17 +151,7 @@ function main (app) { // PUT /jobs/:id/release // release: function (req,res) { - app.models.job.release(req.params.id, function(err, job){ - if (err) { - res.json(422, err); - } else { - if (job) { - res.json(201, job); - } else { - res.json(404, {message: 'Job not found with id ' + req.params.id + ' and that is available to release'}); - } - } - }); + alter('release', null, req, res); }, @@ -168,7 +163,7 @@ function main (app) { if (req.body.job && req.body.job.metadata) { metadata = req.body.job.metadata; } - app.models.job.complete(req.params.id, metadata, function(err, job){ + app.models.job.complete(req.params.id, metadata, function (err, job) { if (err) { res.json(422, err); } else { @@ -206,17 +201,7 @@ function main (app) { retry: function (req, res) { - app.models.job.retry(req.params.id, function (err, job) { - if (err) { - res.json(422, err); - } else { - if (job) { - res.json(201, job); - } else { - res.json(404, {message: 'Job not found with id ' + req.params.id + ' and that can be retried'}); - } - } - }); + alter('retry', ' and that can be retried', req, res); },