From 8839e5fb3a11b0db97ba48a6f81e6075585c7c15 Mon Sep 17 00:00:00 2001 From: Meutel Date: Wed, 22 Jan 2014 20:39:31 +0100 Subject: [PATCH] paging in list --- lib/endpoint/author.js | 12 ++++++++---- lib/endpoint/endpoint.js | 2 ++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/endpoint/author.js b/lib/endpoint/author.js index a0402b6..e6943d8 100644 --- a/lib/endpoint/author.js +++ b/lib/endpoint/author.js @@ -18,7 +18,6 @@ Author.prototype = Object.create(Endpoint.prototype, { //TODO related action.loadResource = function(resId, callback) { logger.debug('loading author ' + resId); - // TODO load from db db.get('SELECT * FROM authors WHERE id = '+ resId, function(err, row) { callback(err, row); }); @@ -27,10 +26,15 @@ Author.prototype = Object.create(Endpoint.prototype, { break; case 'list': action.loadResources = function(onload, onend) { - //TODO load from bdd - db.each('SELECT * FROM authors LIMIT 30', function (err, row) { + var query = 'SELECT * FROM authors LIMIT ? OFFSET ?'; + if (!this.perPage) this.perPage = 30; + //TODO sanitize + if (!this.page) this.page = 0; + db.each(query, this.perPage, this.page*this.perPage, function (err, row) { onload(err, row); - }, function() { + }, function(err) { + //TODO err + if (err) logger.error(err); onend(); }); }; diff --git a/lib/endpoint/endpoint.js b/lib/endpoint/endpoint.js index 91a0985..418778e 100644 --- a/lib/endpoint/endpoint.js +++ b/lib/endpoint/endpoint.js @@ -21,6 +21,8 @@ Endpoint.prototype = { //TODO search } else if (col && method == 'GET') { action = new Action.ListAction(); + action.page = url.query.page; + action.perPage = url.query.per_page; } else if (!col && method == 'POST') { //TODO edit } else if (!col && method == 'GET') {