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-06-19 20:51:00 +00:00
var query = 'SELECT authors.id as id,name,count(*) as count FROM authors,books_authors_link WHERE authors.id = books_authors_link.author GROUP BY books_authors_link.author ORDER BY sort 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-06-19 20:51:00 +00:00
if ( err ) console . log ( 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 ) {
2014-06-18 20:51:41 +00:00
res . format ( {
html : function ( ) {
row . title = row . name ;
res . render ( 'author' , row ) ;
} ,
json : function ( ) {
res . json ( row ) ;
}
} ) ;
2014-05-22 19:08:41 +00:00
} ) ;
} ) ;
module . exports = router ;