added foundation for pagination

This commit is contained in:
bill bittner 2017-10-12 17:56:23 -07:00
parent 9b0778527f
commit 219a045779
3 changed files with 75 additions and 16 deletions

View file

@ -144,21 +144,17 @@ module.exports = {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let longChannelId; let longChannelId;
let shortChannelId; let shortChannelId;
// 1. get the long channel Id
db db
.getLongChannelId(channelName, channelId) .getLongChannelId(channelName, channelId) // 1. get the long channel Id
// 2. get all claims for that channel .then(result => { // 2. get all claims for that channel
.then(result => {
longChannelId = result; longChannelId = result;
return db.getShortChannelIdFromLongChannelId(longChannelId, channelName); return db.getShortChannelIdFromLongChannelId(longChannelId, channelName);
}) })
// 3. get all Claim records for this channel .then(result => { // 3. get all Claim records for this channel
.then(result => {
shortChannelId = result; shortChannelId = result;
return db.getAllChannelClaims(longChannelId); return db.getAllChannelClaims(longChannelId);
}) })
// 4. add extra data not available from Claim table .then(allChannelClaims => { // 4. add extra data not available from Claim table
.then(allChannelClaims => {
if (allChannelClaims) { if (allChannelClaims) {
allChannelClaims.forEach(element => { allChannelClaims.forEach(element => {
const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1); const fileExtenstion = element.contentType.substring(element.contentType.lastIndexOf('/') + 1);

View file

@ -7,7 +7,9 @@ const SHOW = 'SHOW';
const SHOWLITE = 'SHOWLITE'; const SHOWLITE = 'SHOWLITE';
const CHANNEL = 'CHANNEL'; const CHANNEL = 'CHANNEL';
const CLAIM = 'CLAIM'; const CLAIM = 'CLAIM';
const CHANNELID_INDICATOR = ':'; const CLAIM_ID_CHAR = ':';
const CHANNEL_CHAR = '@';
const CLAIMS_PER_PAGE = 10;
function isValidClaimId (claimId) { function isValidClaimId (claimId) {
return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId)); return ((claimId.length === 40) && !/[^A-Za-z0-9]/g.test(claimId));
@ -32,6 +34,48 @@ function getAsset (claimType, channelName, channelId, name, claimId) {
} }
} }
function getPage (query) {
if (query.p) {
return parseInt(query.p);
}
return 0;
}
function extractPageFromClaims (claims, pageNumber) {
logger.debug('claims is array?', Array.isArray(claims));
logger.debug('pageNumber is number?', Number.isInteger(pageNumber));
return claims.slice(pageNumber * 10, pageNumber + 10);
}
function determineTotalPages (totalClaims) {
if (totalClaims === 0) {
return -1;
}
if (totalClaims < CLAIMS_PER_PAGE) {
return 0;
}
const fullPages = Math.floor(totalClaims / CLAIMS_PER_PAGE);
const remainder = totalClaims % CLAIMS_PER_PAGE;
if (remainder === 0) {
return fullPages - 1;
}
return fullPages;
}
function determinePreviousPage (currentPage) {
if (currentPage === 0) {
return 0;
}
return currentPage - 1;
}
function determineNextPage (totalPages, currentPage) {
if (currentPage === totalPages) {
return currentPage;
}
return currentPage + 1;
}
module.exports = (app) => { module.exports = (app) => {
// route to serve a specific asset // route to serve a specific asset
app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => { app.get('/:identifier/:name', ({ headers, ip, originalUrl, params }, res) => {
@ -75,7 +119,7 @@ module.exports = (app) => {
if (identifier.charAt(0) === '@') { if (identifier.charAt(0) === '@') {
channelName = identifier; channelName = identifier;
claimType = CHANNEL; claimType = CHANNEL;
const channelIdIndex = channelName.indexOf(CHANNELID_INDICATOR); const channelIdIndex = channelName.indexOf(CLAIM_ID_CHAR);
if (channelIdIndex !== -1) { if (channelIdIndex !== -1) {
channelId = channelName.substring(channelIdIndex + 1); channelId = channelName.substring(channelIdIndex + 1);
channelName = channelName.substring(0, channelIdIndex); channelName = channelName.substring(0, channelIdIndex);
@ -105,16 +149,18 @@ module.exports = (app) => {
}); });
}); });
// route to serve the winning asset at a claim // route to serve the winning asset at a claim
app.get('/:name', ({ headers, ip, originalUrl, params }, res) => { app.get('/:name', ({ headers, ip, originalUrl, params, query }, res) => {
// parse name param // parse name param
let name = params.name; let name = params.name;
let method; let method;
let fileExtension; let fileExtension;
let channelName = null; let channelName = null;
let channelId = null; let channelId = null;
if (name.charAt(0) === '@') { // (a) handle channel requests
if (name.charAt(0) === CHANNEL_CHAR) {
channelName = name; channelName = name;
const channelIdIndex = channelName.indexOf(CHANNELID_INDICATOR); const paginationPage = getPage(query);
const channelIdIndex = channelName.indexOf(CLAIM_ID_CHAR);
if (channelIdIndex !== -1) { if (channelIdIndex !== -1) {
channelId = channelName.substring(channelIdIndex + 1); channelId = channelName.substring(channelIdIndex + 1);
channelName = channelName.substring(0, channelIdIndex); channelName = channelName.substring(0, channelIdIndex);
@ -125,16 +171,27 @@ module.exports = (app) => {
getChannelContents(channelName, channelId) getChannelContents(channelName, channelId)
// 2. respond to the request // 2. respond to the request
.then(result => { .then(result => {
logger.debug('result'); const totalPages = determineTotalPages(result.claims.length);
if (!result.claims) { if (!result.claims) {
res.status(200).render('noChannel'); res.status(200).render('noChannel');
} else { } else {
res.status(200).render('channel', result); res.status(200).render('channel', {
channelName : result.channelName,
longChannelId : result.longChannelId,
shortChannelId: result.shortChannelId,
claims : extractPageFromClaims(result.claims, paginationPage),
previousPage : determinePreviousPage(paginationPage),
currentPage : paginationPage,
nextPage : determineNextPage(totalPages, paginationPage),
totalPages,
totalResults : result.claims.length,
});
} }
}) })
.catch(error => { .catch(error => {
handleRequestError('serve', originalUrl, ip, error, res); handleRequestError('serve', originalUrl, ip, error, res);
}); });
// (b) handle stream requests
} else { } else {
if (name.indexOf('.') !== -1) { if (name.indexOf('.') !== -1) {
method = SERVE; method = SERVE;

View file

@ -1,6 +1,12 @@
<div class="row row--full-height"> <div class="row row--full-height">
<h2>{{this.channelName}}<span class="h2--secondary">:{{this.longChannelId}}</span></h2> <h2>{{this.channelName}}<span class="h2--secondary">:{{this.longChannelId}}</span></h2>
<p>Below is all the free content in this channel.</p> <p>Page: {{this.currentPage}}/{{this.totalPages}}, Total Files: {{this.totalResults}}</p>
<p>
<a href="/{{this.channelName}}:{{this.longChannelId}}?p=0">First Page</a> |
<a href="/{{this.channelName}}:{{this.longChannelId}}?p={{this.previousPage}}">Last Page</a> |
<a href="/{{this.channelName}}:{{this.longChannelId}}?p={{this.nextPage}}">Next Page</a> |
<a href="/{{this.channelName}}:{{this.longChannelId}}?p={{this.totalPages}}">Last Page</a>
</p>
{{#each this.claims}} {{#each this.claims}}
{{> contentListItem}} {{> contentListItem}}
{{/each}} {{/each}}