Skip to content

Commit

Permalink
merge catalog query+filter with pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
michellelin1 committed Feb 6, 2024
1 parent a99ea3a commit f3ee333
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions routes/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ const { keysToCamel, isInteger } = require('../common/utils');
catalogRouter.get('/', async (req, res) => {
try {
const { title, eventType, subject, season, year } = req.query;

let { limit, page } = req.query;
limit = isInteger(limit) ? parseInt(limit, 10) : 10;
page = isInteger(page) ? parseInt(page, 10) : 1;

const offset = (page - 1) * limit;
let query = 'FROM catalog WHERE 1=1';

let query = ' FROM catalog WHERE 1=1';

const params = [];

Expand Down Expand Up @@ -53,21 +53,16 @@ catalogRouter.get('/', async (req, res) => {
} else {
params.push('');
}

const eventCount = await db.query(`SELECT COUNT(*) ${query};`, params);

query += ' ORDER BY title ASC LIMIT $6 OFFSET $7;';
params.push(limit);
params.push(offset);

query += ' ORDER BY title ASC';

let countQuery = 'SELECT COUNT(*) ' + query + ';';
const eventCount = await db.query(query, params);

query += ' LIMIT $6 OFFSET $7;';
query = 'SELECT * ' + query;
const reqInfo = await db.query(`SELECT * ${query}`, params);

const reqInfo = await db.query(query, params);

res.status(200).json(keysToCamel({ events: reqInfo, count: eventCount }));

} catch (err) {
res.status(500).send(err.message);
}
Expand Down Expand Up @@ -152,4 +147,4 @@ catalogRouter.delete('/:id', async (req, res) => {
}
});

module.exports = catalogRouter;
module.exports = catalogRouter;

0 comments on commit f3ee333

Please sign in to comment.