Skip to content

Commit

Permalink
Refactored more of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbjensen committed Mar 30, 2014
1 parent 91614e0 commit 2ef836b
Showing 1 changed file with 19 additions and 34 deletions.
53 changes: 19 additions & 34 deletions lib/controllers/jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -128,35 +143,15 @@ 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);
},



// 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);
},


Expand All @@ -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 {
Expand Down Expand Up @@ -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);
},


Expand Down

0 comments on commit 2ef836b

Please sign in to comment.