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 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'; var initial = req.query.initial; if (initial) { query+=' WHERE'; if (initial == '0') { query+=' substr(books.sort,1,1) < ? OR substr(books.sort,1,1) > ?'; qparams.push('A'); qparams.push('Z'); } else { query+=' UPPER(books.sort) LIKE ?'; qparams.push(req.query.initial.toUpperCase()+'%'); } } query+= ' ORDER BY books.sort LIMIT ? OFFSET ?'; var books = new HashMap(); req.paginate = new paginate(req); 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