2014-05-22 19:08:41 +00:00
|
|
|
var express = require('express');
|
|
|
|
var router = express.Router();
|
2014-05-25 12:15:29 +00:00
|
|
|
var paginate = require('../util/paginate');
|
2014-05-22 19:08:41 +00:00
|
|
|
|
|
|
|
/* All authors */
|
|
|
|
router.get('/', function(req, res) {
|
2014-05-25 12:15:29 +00:00
|
|
|
var query = 'SELECT * FROM authors LIMIT ? OFFSET ?';
|
2014-05-22 19:08:41 +00:00
|
|
|
var authors = new Array();
|
2014-05-25 12:15:29 +00:00
|
|
|
req.paginate = new paginate(req);
|
|
|
|
req.db.each(query, req.paginate.perpage + 1, req.paginate.offset, function (err, row) {
|
|
|
|
if (authors.length < req.paginate.perpage)
|
|
|
|
authors.push(row);
|
|
|
|
else
|
|
|
|
req.paginate.hasNext = true;
|
2014-05-22 19:08:41 +00:00
|
|
|
}, function(err) {
|
2014-05-25 12:15:29 +00:00
|
|
|
res.links(req.paginate.links());
|
2014-05-22 19:08:41 +00:00
|
|
|
res.json(authors);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Single author */
|
|
|
|
router.get('/:id', function(req, res) {
|
|
|
|
req.db.get('SELECT * FROM authors WHERE id = ?', req.params.id, function(err, row) {
|
|
|
|
res.json(row);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|