pagination

acces données
This commit is contained in:
Meutel 2014-05-25 14:15:29 +02:00
parent 64f1e3bf01
commit f8ede30c07
6 changed files with 102 additions and 12 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@
*~
*.*.swp
node_modules
npm-debug.log

View File

@ -1,16 +1,19 @@
var express = require('express');
var router = express.Router();
var paginate = require('../util/paginate');
/* All authors */
router.get('/', function(req, res) {
var query = 'SELECT * FROM authors ';
query += ' LIMIT ? OFFSET ?';
var query = 'SELECT * FROM authors LIMIT ? OFFSET ?';
var authors = new Array();
var perpage = req.query.perpage;
perpage = perpage ? perpage : 10;
req.db.each(query, perpage, 0, function (err, row) {
authors.push(row);
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;
}, function(err) {
res.links(req.paginate.links());
res.json(authors);
});
});

View File

@ -1,9 +1,28 @@
var express = require('express');
var router = express.Router();
var paginate = require('../util/paginate');
/* GET home page. */
/* All books */
router.get('/', function(req, res) {
res.render('index', { title: 'Books' });
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) {
res.json(row);
});
});
module.exports = router;

View File

@ -1,9 +1,28 @@
var express = require('express');
var router = express.Router();
var paginate = require('../util/paginate');
/* GET home page. */
/* All series */
router.get('/', function(req, res) {
res.render('index', { title: 'Series' });
var query = 'SELECT * FROM series LIMIT ? OFFSET ?';
var series = new Array();
req.paginate = new paginate(req);
req.db.each(query, req.paginate.perpage + 1, req.paginate.offset, function (err, row) {
if (series.length < req.paginate.perpage)
series.push(row);
else
req.paginate.hasNext = true;
}, function(err) {
res.links(req.paginate.links());
res.json(series);
});
});
/* Single serie */
router.get('/:id', function(req, res) {
req.db.get('SELECT * FROM series WHERE id = ?', req.params.id, function(err, row) {
res.json(row);
});
});
module.exports = router;

View File

@ -1,9 +1,28 @@
var express = require('express');
var router = express.Router();
var paginate = require('../util/paginate');
/* GET home page. */
/* All tags */
router.get('/', function(req, res) {
res.render('index', { title: 'Tags' });
var query = 'SELECT * FROM tags LIMIT ? OFFSET ?';
var tags = new Array();
req.paginate = new paginate(req);
req.db.each(query, req.paginate.perpage + 1, req.paginate.offset, function (err, row) {
if (tags.length < req.paginate.perpage)
tags.push(row);
else
req.paginate.hasNext = true;
}, function(err) {
res.links(req.paginate.links());
res.json(tags);
});
});
/* Single tag */
router.get('/:id', function(req, res) {
req.db.get('SELECT * FROM tags WHERE id = ?', req.params.id, function(err, row) {
res.json(row);
});
});
module.exports = router;

29
util/paginate.js Normal file
View File

@ -0,0 +1,29 @@
var url = require('url');
var paginate = function(req) {
this.perpage = req.query.perpage ? req.query.perpage : 10;
this.page = Number(req.query.page ? req.query.page : 0);
this.offset = this.page * this.perpage;
this.oUrl = url.parse(req.originalUrl, true);
this.hasNext = false;
};
paginate.prototype = {
links: function() {
var links = {};
this.oUrl.search = null;
if (this.page > 0) {
this.oUrl.query.page = this.page - 1;
links.prev = url.format(this.oUrl);
this.oUrl.query.page = this.page;
}
if (this.hasNext) {
this.oUrl.query.page = this.page + 1;
links.next = url.format(this.oUrl);
this.oUrl.query.page = this.page;
}
return links;
}
};
module.exports = paginate;