var express = require('express'); var router = express.Router(); var paginate = require('../util/paginate'); var HashMap = require('hashmap').HashMap; /* All books */ router.get('/', function(req, res) { var books = new HashMap(); req.paginate = new paginate(req); var qparams = new Array(); var query = 'SELECT books.id as id,title,series_index,name as series_name,series.id AS series_id'+ ' FROM books' + ' LEFT OUTER JOIN books_series_link ON books.id = books_series_link.book' + ' LEFT OUTER JOIN series ON series.id = books_series_link.series'; query = req.paginate.appendInitialQuery(query,'books.sort',qparams,true); if (req.query.sort == 'recent') query+= ' ORDER BY books.last_modified DESC'; else query+= ' ORDER BY books.sort'; query+= ' LIMIT ? OFFSET ?'; qparams.push(req.paginate.perpage + 1); qparams.push(req.paginate.offset); req.db.each(query, qparams, function (err, book) { if (books.count() < req.paginate.perpage) { books.set(''+book.id, book); } else req.paginate.hasNext = true; /* more than perpage */ }, function(err) { if (err) console.log(err); res.links(req.paginate.links()); /* query books authors */ var qAuthors = 'SELECT books_authors_link.book as book,authors.id as id,name FROM authors, books_authors_link WHERE books_authors_link.author = authors.id AND books_authors_link.book IN ('; for (var i=0;i