Skip to content
This repository has been archived by the owner on Sep 2, 2020. It is now read-only.

Pagination #113

Merged
merged 4 commits into from
Jun 13, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/index.js
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ server.use(restify.bodyParser({mapParams: false, rejectUnknown: true}));
server.use(middleware.verifyRequest())
server.use(middleware.attachResolvePath())
server.use(middleware.attachErrorLogger())
server.use(middleware.attachPageData())

applyRoutes(server);

14 changes: 14 additions & 0 deletions app/lib/middleware.js
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ module.exports = {
verifyRequest: verifyRequest,
attachResolvePath: attachResolvePath,
attachErrorLogger: attachErrorLogger,
attachPageData: attachPageData,
}

const jws = require('jws')
@@ -107,6 +108,19 @@ function attachErrorLogger() {
}
}

function attachPageData() {
return function (req, res, next) {
const page = parseInt(req.query.page, 10)
const count = parseInt(req.query.count, 10)

if (page > 0 && count > 0) {
req.pageData = { page: page, count: count }
}

return next()
}
}

function verifyRequest() {
if (process.env.NODE_ENV == 'test') {
log.warn('In test environment, bypassing request verification')
11 changes: 11 additions & 0 deletions app/lib/send-paginated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = function sendPaginated(req, res, responseData, total) {
if (req.pageData) {
responseData.pageData = {
page: req.pageData.page,
count: req.pageData.count,
total: total
};
}

return res.send(200, responseData);
}
21 changes: 17 additions & 4 deletions app/routes/applications.js
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ const Badges = require('../models/badge');
const errorHelper = require('../lib/error-helper')
const middleware = require('../lib/middleware')
const hash = require('../lib/hash').hash
const sendPaginated = require('../lib/send-paginated');

const dbErrorHandler = errorHelper.makeDbHandler('application')

@@ -55,13 +56,25 @@ exports = module.exports = function applyApplicationRoutes (server) {
if (req.issuer) query.issuerId = req.issuer.id;
if (req.program) query.programId = req.program.id;

Applications.get(query, options, function foundRows (error, rows) {
if (req.pageData) {
options.limit = req.pageData.count;
options.page = req.pageData.page;
options.includeTotal = true;
}

Applications.get(query, options, function foundRows (error, result) {
if (error)
return dbErrorHandler(error, null, res, next);

res.send({applications: rows.map(function (application) {
return Applications.toResponse(application, req);
})});
var total = 0;
var rows = result;
if (req.pageData) {
total = result.total;
rows = result.rows;
}

var responseData = {applications: rows.map(function (application) { return Applications.toResponse(application, req); })}
sendPaginated(req, res, responseData, total);

return next();
});
41 changes: 36 additions & 5 deletions app/routes/badge-instances.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ const util = require('util')
const unixtimeFromDate = require('../lib/unixtime-from-date')
const sha256 = require('../lib/hash').sha256
const customError = require('../lib/custom-error')
const sendPaginated = require('../lib/send-paginated')
const Badges = require('../models/badge')
const ClaimCodes = require('../models/claim-codes')
const Webhooks = require('../models/webhook')
@@ -259,8 +260,23 @@ exports = module.exports = function applyBadgeRoutes (server) {
server.get(prefix.program + getInstancesSuffix,
findProgramBadge, getBadgeInstances)
function getBadgeInstances(req, res, next) {
BadgeInstances.get({ badgeId: req.badge.id}, {relationships: true, relationshipsDepth: 2}).then(function (rows) {
return res.send({instances: rows.map(function (row) { return BadgeInstances.toResponse(row, req); })});
var options = {relationships: true, relationshipsDepth: 2};

if (req.pageData) {
options.limit = req.pageData.count;
options.page = req.pageData.page;
options.includeTotal = true;
}

BadgeInstances.get({ badgeId: req.badge.id}, options).then(function (result) {
var total = 0;
var rows = result;
if (req.pageData) {
total = result.total;
rows = result.rows;
}
var responseData = {instances: rows.map(function (row) { return BadgeInstances.toResponse(row, req); })}
return sendPaginated(req, res, responseData, total);
})
.error(function (err) {
log.error(err, 'error fetching badge instances');
@@ -361,13 +377,28 @@ exports = module.exports = function applyBadgeRoutes (server) {
BadgeInstances.get([query, queryParams]).then(function (rows) {
var instanceIds = rows.map(function(row) { return row.id; });
if (instanceIds.length) {
return BadgeInstances.get( { id: instanceIds }, { relationships: true, relationshipsDepth: 2 });
var options = { relationships: true, relationshipsDepth: 2 };
if (req.pageData) {
options.limit = req.pageData.count;
options.page = req.pageData.page;
options.includeTotal = true;
}

return BadgeInstances.get( { id: instanceIds }, options);
}
else {
return Promise.resolve([]);
}
}).then(function (rows) {
res.send({instances: rows.map(function (row) { return BadgeInstances.toResponse(row, req); })});
}).then(function (result) {
var total = 0;
var rows = result;
if (req.pageData) {
total = result.total;
rows = result.rows;
}

var responseData = {instances: rows.map(function (row) { return BadgeInstances.toResponse(row, req); })}
return sendPaginated(req, res, responseData, total);
}).error(function (err) {
if (!err.restCode)
log.error(err, 'unknown error in getUserInstances route')
21 changes: 19 additions & 2 deletions app/routes/badges.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ const Milestones = require('../models/milestone');
const imageHelper = require('../lib/image-helper')
const errorHelper = require('../lib/error-helper')
const middleware = require('../lib/middleware')
const sendPaginated = require('../lib/send-paginated');

const putBadgeHelper = imageHelper.putModel(Badges)
const dbErrorHandler = errorHelper.makeDbHandler('badge')
@@ -61,11 +62,27 @@ exports = module.exports = function applyBadgeRoutes (server) {
if (req.issuer) query.issuerId = req.issuer.id
if (req.program) query.programId = req.program.id

Badges.get(query, options, function foundRows (error, rows) {
if (req.pageData) {
options.limit = req.pageData.count;
options.page = req.pageData.page;
options.includeTotal = true;
}

Badges.get(query, options, function foundRows (error, result) {
if (error)
return dbErrorHandler(error, null, res, next);

var total = 0;
var rows = result;
if (req.pageData) {
total = result.total;
rows = result.rows;
}

var responseData = { badges: rows.map(Badges.toResponse) };

res.send({badges: rows.map(Badges.toResponse)});
sendPaginated(req, res, responseData, total);

return next();
});
}
24 changes: 20 additions & 4 deletions app/routes/claim-codes.js
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ const Badges = require('../models/badge')
const ClaimCodes = require('../models/claim-codes')
const middleware = require('../lib/middleware')
const errorHelper = require('../lib/error-helper')
const sendPaginated = require('../lib/send-paginated');

// #TODO: factor this out, see ./badge-instances.js

@@ -107,13 +108,28 @@ exports = module.exports = function applyClaimCodesRoutes (server) {
const query = {badgeId: req.badge.id}
const options = {relationships: false}

if (req.pageData) {
options.limit = req.pageData.count;
options.page = req.pageData.page;
options.includeTotal = true;
}

ClaimCodes
.get(query, options)
.then(function (claimCodes) {
res.send(200, {
claimCodes: claimCodes.map(ClaimCodes.toResponse),
.then(function (result) {
var total = 0;
var rows = result;
if (req.pageData) {
total = result.total;
rows = result.rows;
}

var responseData = {
claimCodes: rows.map(ClaimCodes.toResponse),
badge: req.badge.toResponse(),
})
};

sendPaginated(req, res, responseData, total);
})
.error(req.error('Error getting claim code list'))
}
21 changes: 19 additions & 2 deletions app/routes/issuers.js
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ const Issuers = require('../models/issuer');
const imageHelper = require('../lib/image-helper')
const errorHelper = require('../lib/error-helper')
const middleware = require('../lib/middleware')
const sendPaginated = require('../lib/send-paginated');

const putIssuer = imageHelper.putModel(Issuers)
const dbErrorHandler = errorHelper.makeDbHandler('issuer')
@@ -16,10 +17,26 @@ exports = module.exports = function applyIssuerRoutes (server) {
function showAllIssuers(req, res, next) {
const options = {relationships: true}
const query = {systemId: req.system.id}
Issuers.get(query, options, function foundRows(error, rows) {

if (req.pageData) {
options.limit = req.pageData.count;
options.page = req.pageData.page;
options.includeTotal = true;
}

Issuers.get(query, options, function foundRows(error, result) {
if (error)
return dbErrorHandler(error, null, res, next)
return res.send({issuers: rows.map(Issuers.toResponse)});

var total = 0;
var rows = result;
if (req.pageData) {
total = result.total;
rows = result.rows;
}

var responseData = {issuers: rows.map(Issuers.toResponse)}
return sendPaginated(req, res, responseData, total)
});
}

22 changes: 18 additions & 4 deletions app/routes/milestones.js
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ const Milestones = require('../models/milestone');
const MilestoneBadges = require('../models/milestone-badge')
const middleware = require('../lib/middleware');
const errorHelper = require('../lib/error-helper')
const sendPaginated = require('../lib/send-paginated');

exports = module.exports = function applyBadgeRoutes(server) {
server.get('/systems/:systemSlug/milestones', [
@@ -14,11 +15,24 @@ exports = module.exports = function applyBadgeRoutes(server) {
function showAllMilestones(req, res, next) {
const query = { systemId: req.system.id };
const options = { relationships: true };

if (req.pageData) {
options.limit = req.pageData.count;
options.page = req.pageData.page;
options.includeTotal = true;
}

Milestones.get(query, options)
.then(function (milestones) {
return res.send(200, {
milestones: milestones.map(Milestones.toResponse)
});
.then(function (result) {
var total = 0;
var rows = result;
if (req.pageData) {
total = result.total;
rows = result.rows;
}

var responseData = { milestones: rows.map(Milestones.toResponse) };
return sendPaginated(req, res, responseData, total);
})
.catch(handleError(req, next));
}
20 changes: 18 additions & 2 deletions app/routes/programs.js
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ const Programs = require('../models/program');
const imageHelper = require('../lib/image-helper')
const errorHelper = require('../lib/error-helper')
const middleware = require('../lib/middleware')
const sendPaginated = require('../lib/send-paginated');

const putProgram = imageHelper.putModel(Programs)
const dbErrorHandler = errorHelper.makeDbHandler('program')
@@ -17,11 +18,26 @@ exports = module.exports = function applyProgramRoutes (server) {
function showAllPrograms(req, res, next) {
const options = {relationships: true}
const query = {issuerId: req.issuer.id}
Programs.get(query, options, function foundRows(error, rows) {

if (req.pageData) {
options.limit = req.pageData.count;
options.page = req.pageData.page;
options.includeTotal = true;
}

Programs.get(query, options, function foundRows(error, result) {
if (error)
return dbErrorHandler(error, null, res, next)

res.send({programs: rows.map(Programs.toResponse)});
var total = 0;
var rows = result;
if (req.pageData) {
total = result.total;
rows = result.rows;
}

var responseData = {programs: rows.map(Programs.toResponse)};
sendPaginated(req, res, responseData, total);
return next();
});
}
19 changes: 17 additions & 2 deletions app/routes/reviews.js
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ const middleware = require('../lib/middleware')
const hash = require('../lib/hash').hash
const request = require('request')
const log = require('../lib/logger')
const sendPaginated = require('../lib/send-paginated');
const _ = require('underscore');

const dbErrorHandler = errorHelper.makeDbHandler('application')
@@ -39,11 +40,25 @@ exports = module.exports = function applyReviewRoutes (server) {
var query = { applicationId : req.application.id };
var options = {relationships: true};

Reviews.get(query, options, function foundRows (error, rows) {
if (req.pageData) {
options.limit = req.pageData.count;
options.page = req.pageData.page;
options.includeTotal = true;
}

Reviews.get(query, options, function foundRows (error, result) {
if (error)
return dbErrorHandler(error, null, res, next);

var total = 0;
var rows = result;
if (req.pageData) {
total = result.total;
rows = result.rows;
}

res.send({reviews: rows.map(Reviews.toResponse)});
var responseData = {reviews: rows.map(Reviews.toResponse)};
sendPaginated(req, res, responseData, total);
return next();
});
}
Loading