From a743053c06198d022630f8ee8c641f0a6624ed0e Mon Sep 17 00:00:00 2001 From: Ethan Ho Date: Thu, 1 Feb 2024 13:45:34 -0800 Subject: [PATCH 1/3] Modified /catalog get request to accept limit and page parameter for pagination Co-authored-by: Maithy Le --- common/utils.js | 7 ++++++- routes/catalog.js | 10 +++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/common/utils.js b/common/utils.js index 14fad66..d9f6238 100644 --- a/common/utils.js +++ b/common/utils.js @@ -20,6 +20,11 @@ const isISODate = (str) => { } }; +// dependency for catalog.js +const isInteger = (value) => { + return value && /^\d+$/.test(value); +}; + const isObject = (o) => { return o === Object(o) && !isArray(o) && typeof o !== 'function' && !isISODate(o); }; @@ -52,4 +57,4 @@ const keysToCamel = (data) => { return data; }; -module.exports = { keysToCamel }; +module.exports = { keysToCamel, isInteger }; diff --git a/routes/catalog.js b/routes/catalog.js index 6d914b2..77ab008 100644 --- a/routes/catalog.js +++ b/routes/catalog.js @@ -1,14 +1,18 @@ const express = require('express'); - const { db } = require('../server/db'); const catalogRouter = express.Router(); -const { keysToCamel } = require('../common/utils'); +const { keysToCamel, isInteger } = require('../common/utils'); // -- GET - Returns all data from the catalog table catalogRouter.get('/', async (req, res) => { try { - const allInfo = await db.query(`SELECT * from catalog;`); + let { limit, page } = req.query; + limit = isInteger(limit) ? parseInt(limit, 10) : 10; + page = isInteger(page) ? parseInt(page, 10) : 1; + + const offset = (page - 1) * limit; + const allInfo = await db.query(`SELECT * from catalog LIMIT $1 OFFSET $2;`, [limit, offset]); res.status(200).json(keysToCamel(allInfo)); } catch (err) { res.status(500).send(err.message); From 317f0cfb4d1a047b649b30242f5dd4edbfde07c6 Mon Sep 17 00:00:00 2001 From: Ethan Ho Date: Thu, 1 Feb 2024 15:22:44 -0800 Subject: [PATCH 2/3] Modified /catalog get to return data and amount of events Co-authored-by: Maithy Le --- routes/catalog.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/routes/catalog.js b/routes/catalog.js index 77ab008..552e927 100644 --- a/routes/catalog.js +++ b/routes/catalog.js @@ -13,7 +13,8 @@ catalogRouter.get('/', async (req, res) => { const offset = (page - 1) * limit; const allInfo = await db.query(`SELECT * from catalog LIMIT $1 OFFSET $2;`, [limit, offset]); - res.status(200).json(keysToCamel(allInfo)); + const eventCount = await db.query(`SELECT COUNT(DISTINCT id) from catalog;`); + res.status(200).json(keysToCamel({ events: allInfo, count: eventCount })); } catch (err) { res.status(500).send(err.message); } From f3ee333938b85a74e9f5b103b6c554749e2f22fd Mon Sep 17 00:00:00 2001 From: michellelin1 Date: Mon, 5 Feb 2024 21:27:24 -0800 Subject: [PATCH 3/3] 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;