From f3ee333938b85a74e9f5b103b6c554749e2f22fd Mon Sep 17 00:00:00 2001 From: michellelin1 Date: Mon, 5 Feb 2024 21:27:24 -0800 Subject: [PATCH] merge catalog query+filter with pagination --- routes/catalog.js | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/routes/catalog.js b/routes/catalog.js index 05dba79..71f3840 100644 --- a/routes/catalog.js +++ b/routes/catalog.js @@ -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 = []; @@ -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); } @@ -152,4 +147,4 @@ catalogRouter.delete('/:id', async (req, res) => { } }); -module.exports = catalogRouter; \ No newline at end of file +module.exports = catalogRouter;