From 7095bad6246f5c8899e7077eb82d0e1479aa1116 Mon Sep 17 00:00:00 2001 From: Alyssia Tan <129691562+arralia@users.noreply.github.com> Date: Mon, 4 Mar 2024 21:41:15 -0800 Subject: [PATCH] completed multi-filter functionality (#56) Co-authored-by: ThatMegamind <92563733+ThatMegamind@users.noreply.github.com> --- routes/catalog.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/routes/catalog.js b/routes/catalog.js index 71f3840..f3c18a5 100644 --- a/routes/catalog.js +++ b/routes/catalog.js @@ -15,7 +15,8 @@ catalogRouter.get('/', async (req, res) => { const offset = (page - 1) * limit; - let query = ' FROM catalog WHERE 1=1'; + let query = 'FROM catalog WHERE 1=1'; + // removed space at beginning here const params = []; @@ -27,29 +28,41 @@ catalogRouter.get('/', async (req, res) => { } if (subject) { - query += ' AND subject = $2'; - params.push(subject); + const array = subject.split(','); + query += ' AND subject = ANY($2::subject[])'; + // for (let i = 0; i < array.length; i += 1) { + // query += `'${array[i]}'`; + // if (i < array.length - 1) { + // query += ', '; + // } + // } + // query += ')'; + // query += ' AND subject = $2'; + params.push(array); } else { params.push(''); } if (eventType) { - query += ' AND event_type = $3'; - params.push(eventType); + const array = eventType.split(','); + query += ' AND event_type = ANY($3::event[])'; + params.push(array); } else { params.push(''); } if (season) { - query += ' AND season = $4'; - params.push(season); + const array = season.split(','); + query += ' AND season = ANY($4::season[])'; + params.push(array); } else { params.push(''); } if (year) { - query += ' AND year = $5'; - params.push(year); + const array = year.split(','); + query += ' AND year = ANY($5::year[])'; + params.push(array); } else { params.push(''); }