Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an endpoint to show specific verses #268

Merged
59 changes: 59 additions & 0 deletions api/controllers/shabads.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,65 @@ exports.search = async (req, res) => {
}
};

exports.resultsInfo = async (req, res) => {
const { VerseID } = req.params;
const results = 20;
let page = parseInt(req.query.page, 10) || 1;
if (lib.isListOfNumbers(VerseID)) {
let conn;
try {
conn = await req.app.locals.pool.getConnection();

const q = `SELECT ${allColumns} ${allFrom}
WHERE v.ID IN (${VerseID})`;
mandeepryaz marked this conversation as resolved.
Show resolved Hide resolved

const row = await conn.query(`SELECT COUNT(*) FROM (${q}) AS count`, []);

const totalResults = row[0]['COUNT(*)'];
const totalPages = Math.ceil(totalResults / results);
if (page > totalPages) {
page = totalPages;
}
const resultsInfo = {
totalResults,
pageResults: totalResults,
pages: {
page,
resultsPerPage: results,
totalPages,
},
};

if (totalResults > 0) {
if (page < totalPages) {
req.query.page = page + 1;
resultsInfo.pages.nextPage = `${req.protocol}://${req.get('host')}${req.baseUrl}${
req.path
}?${Object.keys(req.query)
.map(key => `${key}=${encodeURIComponent(req.query[key])}`)
.join('&')}`;
mandeepryaz marked this conversation as resolved.
Show resolved Hide resolved
}
const rows = await conn.query(`${q} LIMIT ?, ?`, [(page - 1) * results, results]);
const verses = rows.map(verse => lib.prepVerse(verse, true, false));
resultsInfo.pageResults = verses.length;
res.json({
resultsInfo,
verses,
});
} else {
res.json({
resultsInfo,
verses: [],
});
}
} catch (err) {
lib.error(err, res, 500);
} finally {
if (conn) conn.release();
}
}
};

exports.shabads = async (req, res) => {
let { ShabadID } = req.params;
const sinceDate = req.query.updatedsince ? lib.isValidDatetime(req.query.updatedsince) : null;
Expand Down
2 changes: 2 additions & 0 deletions api/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ route.get('/health', limiter.rate250, healthcheck.db);
// Shabad Routes
route.get('/search/:query', limiter.rate250, shabads.search);

route.get('/search-results/:VerseID', limiter.rate250, shabads.resultsInfo);

route.get('/shabads/:ShabadID', limiter.rate100, shabads.shabads);

route.get('/angs/:PageNo/:SourceID?', limiter.rate100, shabads.angs);
Expand Down