Created
July 7, 2014 04:52
-
-
Save rerthal/16a5ec8c58674791d45c to your computer and use it in GitHub Desktop.
templates
This file contains hidden or 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
/** | |
* @apiDefineStructure ${NAME}Params | |
* @apiParam {String} slug ${MODEL} identifier | |
*/ | |
/** | |
* @apiDefineStructure ${NAME}Success | |
* @apiSuccess {String} slug ${MODEL} identifier | |
* @apiSuccess {Date} createdAt Date of document creation. | |
* @apiSuccess {Date} updatedAt Date of document last change. | |
*/ | |
var VError, router, nconf, slug, auth, errorParser, ${MODEL}; | |
VError = require('verror'); | |
router = require('express').Router(); | |
nconf = require('nconf'); | |
slug = require('slug'); | |
auth = require('../lib/auth'); | |
errorParser = require('../lib/error-parser'); | |
${MODEL} = require('../models/${NAME}'); | |
/** | |
* @method | |
* @summary Setups default headers | |
* | |
* @param request | |
* @param response | |
* @param next | |
*/ | |
router.use(function (request, response, next) { | |
'use strict'; | |
response.header('Content-Type', 'application/json'); | |
response.header('Content-Encoding', 'UTF-8'); | |
response.header('Content-Language', 'en'); | |
response.header('Cache-Control', 'no-cache, no-store, must-revalidate'); | |
response.header('Pragma', 'no-cache'); | |
response.header('Expires', '0'); | |
response.header('Access-Control-Allow-Origin', '*'); | |
response.header('Access-Control-Allow-Methods', request.get('Access-Control-Request-Method')); | |
response.header('Access-Control-Allow-Headers', request.get('Access-Control-Request-Headers')); | |
next(); | |
}); | |
/** | |
* @api {post} /${NAME}s Creates a new ${NAME} in database. | |
* @apiName create${MODEL} | |
* @apiVersion ${API_VERSION} | |
* @apiGroup ${NAME} | |
* @apiPermission none | |
* @apiDescription | |
* Creates a new ${NAME} in database. | |
* | |
* @apiStructure ${NAME}Params | |
* @apiStructure ${NAME}Success | |
* | |
* @apiErrorExample | |
* HTTP/1.1 400 Bad Request | |
* { | |
* } | |
* | |
* @apiSuccessExample | |
* HTTP/1.1 201 Created | |
* { | |
* "slug": "", | |
* "createdAt": "2014-07-01T12:22:25.058Z", | |
* "updatedAt": "2014-07-01T12:22:25.058Z" | |
* } | |
*/ | |
router | |
.route('/${NAME}s') | |
.post(auth.signature()) | |
.post(auth.session()) | |
.post(function create${MODEL} (request, response, next) { | |
'use strict'; | |
var ${NAME}; | |
${NAME} = new ${MODEL}({ | |
'slug' : slug(request.param('')) | |
}); | |
return ${NAME}.save(function created${MODEL} (error) { | |
if (error) { | |
error = new VError(error, 'error creating ${NAME}'); | |
return next(error); | |
} | |
response.header('Location', '/${NAME}s/' + ${NAME}.slug); | |
response.header('Last-Modified', ${NAME}.updatedAt); | |
return response.send(201, ${NAME}); | |
}); | |
}); | |
/** | |
* @api {get} /${NAME}s List all ${NAME}s in database | |
* @apiName list${MODEL} | |
* @apiVersion ${API_VERSION} | |
* @apiGroup ${NAME} | |
* @apiPermission none | |
* @apiDescription | |
* List all ${NAME}s in database. | |
* | |
* @apiParam {String} [page=0] The page to be displayed. | |
* @apiStructure ${NAME}Success | |
* | |
* @apiSuccessExample | |
* HTTP/1.1 200 Ok | |
* [{ | |
* "slug": "", | |
* "createdAt": "2014-07-01T12:22:25.058Z", | |
* "updatedAt": "2014-07-01T12:22:25.058Z" | |
* }] | |
*/ | |
router | |
.route('/${NAME}s') | |
.get(auth.signature()) | |
.get(auth.session()) | |
.get(function list${MODEL} (request, response, next) { | |
'use strict'; | |
var pageSize, page, query; | |
pageSize = nconf.get('PAGE_SIZE'); | |
page = request.param('page', 0) * pageSize; | |
query = ${MODEL}.find(); | |
query.skip(page); | |
query.limit(pageSize); | |
return query.exec(function listed${MODEL} (error, ${NAME}s) { | |
if (error) { | |
error = new VError(error, 'error finding ${NAME}s'); | |
return next(error); | |
} | |
return response.send(200, ${NAME}s); | |
}); | |
}); | |
/** | |
* @api {get} /${NAME}s/:id Get ${NAME} info in database | |
* @apiName get${MODEL} | |
* @apiVersion ${API_VERSION} | |
* @apiGroup ${NAME} | |
* @apiPermission none | |
* @apiDescription | |
* Get ${NAME} info in database. | |
* | |
* @apiStructure ${NAME}Success | |
* | |
* @apiSuccessExample | |
* HTTP/1.1 200 Ok | |
* { | |
* "slug": "", | |
* "createdAt": "2014-07-01T12:22:25.058Z", | |
* "updatedAt": "2014-07-01T12:22:25.058Z" | |
* } | |
*/ | |
router | |
.route('/${NAME}s/:id') | |
.get(auth.signature()) | |
.get(auth.session()) | |
.get(errorParser.notFound('${NAME}')) | |
.get(function get${MODEL} (request, response) { | |
'use strict'; | |
var ${NAME}; | |
${NAME} = request.${NAME}; | |
response.header('Last-Modified', ${NAME}.updatedAt); | |
return response.send(200, ${NAME}); | |
}); | |
/** | |
* @api {put} /${NAME}s/:id Updates ${NAME} info in database | |
* @apiName update${MODEL} | |
* @apiVersion ${API_VERSION} | |
* @apiGroup ${NAME} | |
* @apiPermission none | |
* @apiDescription | |
* Updates ${NAME} info in database. | |
* | |
* @apiStructure ${NAME}Params | |
* @apiStructure ${NAME}Success | |
* | |
* @apiErrorExample | |
* HTTP/1.1 400 Bad Request | |
* { | |
* } | |
* | |
* @apiSuccessExample | |
* HTTP/1.1 200 Ok | |
* { | |
* "slug": "", | |
* "createdAt": "2014-07-01T12:22:25.058Z", | |
* "updatedAt": "2014-07-01T12:22:25.058Z" | |
* } | |
*/ | |
router | |
.route('/${NAME}s/:id') | |
.put(auth.signature()) | |
.put(auth.session()) | |
.put(errorParser.notFound('${NAME}')) | |
.put(function update${MODEL} (request, response, next) { | |
'use strict'; | |
var ${NAME}; | |
${NAME} = request.${NAME}; | |
${NAME}.slug = slug(request.param('')); | |
return ${NAME}.save(function updated${MODEL} (error) { | |
if (error) { | |
error = new VError(error, 'error updating ${NAME}'); | |
return next(error); | |
} | |
response.header('Last-Modified', ${NAME}.updatedAt); | |
return response.send(200, ${NAME}); | |
}); | |
}); | |
/** | |
* @api {delete} /${NAME}s/:id Removes ${NAME} from database | |
* @apiName remove${MODEL} | |
* @apiVersion ${API_VERSION} | |
* @apiGroup ${NAME} | |
* @apiPermission none | |
* @apiDescription | |
* Removes ${NAME} from database | |
*/ | |
router | |
.route('/${NAME}s/:id') | |
.delete(auth.signature()) | |
.delete(auth.session()) | |
.delete(errorParser.notFound('${NAME}')) | |
.delete(function remove${MODEL} (request, response, next) { | |
'use strict'; | |
var ${NAME}; | |
${NAME} = request.${NAME}; | |
return ${NAME}.remove(function removed${MODEL} (error) { | |
if (error) { | |
error = new VError(error, 'error removing ${NAME}: "${DS}s"', request.params.id); | |
return next(error); | |
} | |
response.header('Last-Modified', ${NAME}.updatedAt); | |
return response.send(204); | |
}); | |
}); | |
/** | |
* @method | |
* @summary Puts requested ${NAME} in request object | |
* | |
* @param request | |
* @param response | |
* @param next | |
* @param id | |
*/ | |
router.param('id', function find${MODEL} (request, response, next, id) { | |
'use strict'; | |
var query; | |
query = ${MODEL}.findOne(); | |
query.where('slug').equals(id); | |
query.exec(function found${MODEL} (error, ${NAME}) { | |
if (error) { | |
error = new VError(error, 'error finding ${NAME}: "${DS}s"', id); | |
return next(error); | |
} | |
request.${NAME} = ${NAME}; | |
return next(); | |
}); | |
}); | |
router.use(errorParser.mongoose()); | |
module.exports = router; |
This file contains hidden or 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
var VError, mongoose, jsonSelect, nconf, Schema, schema; | |
VError = require('verror'); | |
mongoose = require('mongoose'); | |
jsonSelect = require('mongoose-json-select'); | |
nconf = require('nconf'); | |
Schema = mongoose.Schema; | |
/** | |
* @class | |
* @summary System ${NAME} entity | |
* | |
* property {slug} ${MODEL} slug | |
* property {createdAt} | |
* property {updatedAt} | |
*/ | |
schema = new Schema({ | |
'slug' : { | |
'type' : String, | |
'unique' : true | |
}, | |
'createdAt' : { | |
'type' : Date, | |
'default' : Date.now | |
}, | |
'updatedAt' : { | |
'type' : Date | |
} | |
}, { | |
'collection' : '${NAME}s', | |
'strict' : true, | |
'toJSON' : { | |
'virtuals' : true | |
} | |
}); | |
schema.plugin(jsonSelect, { | |
'_id' : 0, | |
'slug' : 1, | |
'createdAt' : 1, | |
'updatedAt' : 1 | |
}); | |
/** | |
* @callback | |
* @summary Setups updatedAt | |
* | |
* @param next | |
*/ | |
schema.pre('save', function set${MODEL}UpdatedAt (next) { | |
'use strict'; | |
this.updatedAt = new Date(); | |
next(); | |
}); | |
module.exports = mongoose.model('${MODEL}', schema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment