Created
September 29, 2015 04:27
-
-
Save ORESoftware/071f4176d35162a53430 to your computer and use it in GitHub Desktop.
db model routes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by amills001c on 6/15/15. | |
*/ | |
//logging | |
//config | |
var config = require('univ-config')('*SC-Admin*', 'config/conf'); | |
//core | |
var express = require('express'); | |
var router = express.Router(); | |
var IJSON = require('idempotent-json'); | |
//http helpers | |
var get = require('../lib/httpHelpers/getModel.js'); | |
var getAll = require('../lib/httpHelpers/getModels.js'); | |
var post = require('../lib/httpHelpers/postModel.js'); | |
var put = require('../lib/httpHelpers/putModel.js'); | |
var del = require('../lib/httpHelpers/deleteModel.js'); | |
/*** jobs routes ***/ | |
router.param('job_id', function (req, res, next, job_id) { | |
// TODO: typically we might sanity check that job_id is of the right format | |
if (job_id == undefined || job_id == null) { | |
console.log('null job_id'); | |
return next(new Error("job_id is null")); | |
} | |
else { | |
next(); | |
} | |
}); | |
// middleware specific to this router | |
//router.use(function timeLog(req, res, next) { | |
// console.log('Time: ', Date.now()); | |
// next(); | |
//}); | |
router.get('/', function (req, res, next) { | |
var JobModel = req.site.models.Job; | |
getAll(JobModel, 'Job', null, req, res, next); | |
}); | |
router.get('/:job_id', function (req, res, next) { | |
var JobModel = req.site.models.Job; | |
var job_id = req.params.job_id; | |
get(JobModel, 'Job', job_id, req, res, next); | |
}); | |
router.post('/', function (req, res, next) { | |
var JobModel = req.site.models.Job; | |
var jobData = req.body; | |
post(JobModel, null, jobData, req, res, next); | |
}); | |
router.put('/:job_id', function (req, res, next) { | |
//TODO: do new Job(data).update({upsert:true}) ? | |
var JobModel = req.site.models.Job; | |
var jobData = req.body; | |
put(JobModel, null, jobData, null, req, res, next); | |
}); | |
router.delete('/:job_id', function (req, res, next) { | |
var JobModel = req.site.models.Job; | |
var job_id = req.params.job_id; | |
del(JobModel, null, job_id, req, res, next); | |
}); | |
module.exports = router; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment