From 3198903ef9e392cb9928722f04e8ce1a08197fe3 Mon Sep 17 00:00:00 2001 From: ctc-devops <90984711+ctc-devops@users.noreply.github.com> Date: Sun, 4 Feb 2024 19:51:20 -0800 Subject: [PATCH 1/2] Filtered get /catalog route to take in req.query w/ fields title, subject, event_type, and year (#43) * init branch * modified get route to take in req.query w/ fields title, subject, event_type, season, and year * ordered by title in ascending order * = changed to ILIKE op for title input --------- Co-authored-by: subinqkim --- routes/catalog.js | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/routes/catalog.js b/routes/catalog.js index 6d914b2..2c5ed3a 100644 --- a/routes/catalog.js +++ b/routes/catalog.js @@ -4,12 +4,53 @@ const { db } = require('../server/db'); const catalogRouter = express.Router(); const { keysToCamel } = require('../common/utils'); - +// modify /catalog // -- GET - Returns all data from the catalog table catalogRouter.get('/', async (req, res) => { try { - const allInfo = await db.query(`SELECT * from catalog;`); - res.status(200).json(keysToCamel(allInfo)); + const { title, eventType, subject, season, year } = req.query; + let query = 'SELECT * FROM catalog WHERE 1=1'; + const params = []; + + if (title) { + query += ' AND title ILIKE $1'; + params.push(`%${title}%`); + } else { + params.push(''); + } + + if (subject) { + query += ' AND subject = $2'; + params.push(subject); + } else { + params.push(''); + } + + if (eventType) { + query += ' AND event_type = $3'; + params.push(eventType); + } else { + params.push(''); + } + + if (season) { + query += ' AND season = $4'; + params.push(season); + } else { + params.push(''); + } + + if (year) { + query += ' AND year = $5'; + params.push(year); + } else { + params.push(''); + } + + query += ' ORDER BY title ASC'; + + const reqInfo = await db.query(query, params); + res.status(200).json(keysToCamel(reqInfo)); } catch (err) { res.status(500).send(err.message); } From 95270877b51ac4cb5605c7f7464f996194165909 Mon Sep 17 00:00:00 2001 From: ctc-devops <90984711+ctc-devops@users.noreply.github.com> Date: Sun, 4 Feb 2024 20:11:52 -0800 Subject: [PATCH 2/2] Retreiving Published Schedule Events Grouped By Season (#40) * Create a pull trequest for branch 36-retreive-published-schedule-events-by-season * added season/year routes (untested) * group by date instead of timestamp * mapped season to month * fixed mapping december * added where clause --------- Co-authored-by: Cheryl Chen Co-authored-by: Cheryl Chen Co-authored-by: Cheryl Chen Co-authored-by: michellelin1 --- routes/publishedSchedule.js | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index 6793999..43c8376 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -30,6 +30,92 @@ publishedScheduleRouter.get('/', async (req, res) => { } }); +// GET /published-schedule/season - returns rows that match the season +publishedScheduleRouter.get('/season', async (req, res) => { + try { + let startTime; + let endTime; + + const { season, year } = req.query; + + // getting the intervals for each season + if (season.toLowerCase() === 'winter') { + startTime = `${year - 1}-12-01`; + endTime = `${year}-02-29`; + } else if (season.toLowerCase() === 'spring') { + startTime = `${year}-03-01`; + endTime = `${year}-05-31`; + } else if (season.toLowerCase() === 'summer') { + startTime = `${year}-06-01`; + endTime = `${year}-08-31`; + } else { + startTime = `${year}-09-01`; + endTime = `${year}-11-30`; + } + + const seasonResult = await db.query( + ` + WITH seasonPS AS + ( + SELECT + PS.id, + C.title, + C.event_type, + C.year, + PS.start_time, + PS.end_time, + PS.confirmed, + PS.confirmed_on, + PS.cohort, + PS.notes + FROM published_schedule PS + LEFT JOIN catalog C ON PS.event_id = C.id + WHERE + DATE(start_time) >= $1::date AND DATE(start_time) <= $2::date + ) + SELECT DATE(seasonPS.start_time), JSON_AGG(seasonPS.*) AS data + FROM seasonPS + GROUP BY DATE(start_time) + ORDER BY DATE(start_time) ASC; + `, + [startTime, endTime], + ); + res.status(200).json(keysToCamel(seasonResult)); + } catch (err) { + res.status(400).send(err.message); + } +}); + +// GET /published-schedule/date - returns all events occurring on a specific date +publishedScheduleRouter.get('/date', async (req, res) => { + try { + const { date } = req.query; + const seasonResult = await db.query( + ` + SELECT + PS.id, + C.title, + C.event_type, + C.year, + PS.start_time, + PS.end_time, + PS.confirmed, + PS.confirmed_on, + PS.cohort, + PS.notes + FROM published_schedule PS + LEFT JOIN catalog C ON PS.event_id = C.id + WHERE DATE(PS.start_time) = $1 + ORDER BY start_time ASC; + `, + [date], + ); + res.status(200).json(keysToCamel(seasonResult)); + } catch (err) { + res.status(400).send(err.message); + } +}); + // GET/:id - returns the rows that match the given id publishedScheduleRouter.get('/:id', async (req, res) => { try {