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
|
|
|
|
2014-05-25 12:15:29 +00:00
|
|
|
/* All books */
|
2014-05-22 19:08:41 +00:00
|
|
|
router.get('/', function(req, res) {
|
2014-05-25 12:15:29 +00:00
|
|
|
var query = 'SELECT * FROM books LIMIT ? OFFSET ?';
|
|
|
|
var books = new Array();
|
|
|
|
req.paginate = new paginate(req);
|
|
|
|
req.db.each(query, req.paginate.perpage + 1, req.paginate.offset, function (err, row) {
|
|
|
|
if (books.length < req.paginate.perpage)
|
|
|
|
books.push(row);
|
|
|
|
else
|
|
|
|
req.paginate.hasNext = true;
|
|
|
|
}, function(err) {
|
|
|
|
res.links(req.paginate.links());
|
|
|
|
res.json(books);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/* Single book */
|
|
|
|
router.get('/:id', function(req, res) {
|
|
|
|
req.db.get('SELECT * FROM books WHERE id = ?', req.params.id, function(err, row) {
|
2014-06-18 20:51:41 +00:00
|
|
|
res.format({
|
|
|
|
html: function(){
|
|
|
|
res.render('book', row);
|
|
|
|
},
|
|
|
|
json: function(){
|
|
|
|
res.json(row);
|
|
|
|
}
|
|
|
|
});
|
2014-05-25 12:15:29 +00:00
|
|
|
});
|
2014-05-22 19:08:41 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|