From 699f437898a06f3512c2ed3fb909920ceff53b19 Mon Sep 17 00:00:00 2001 From: ctc-devops <90984711+ctc-devops@users.noreply.github.com> Date: Wed, 3 Jan 2024 11:35:26 -0800 Subject: [PATCH] Make Backend Routes for Published Schedule (#29) * Modified GET queries with joins on catalog table * created publishSchedule.js file * Mounted published schedule route on app.js * Added GET and POST route controller functions for published schedule * put and delete, not yet complete * updated publishedSchedule.js * updated to use numeric syntax for sql queries * small fixes in ppublishedSchedule.js * pull request feedback * pull request feedback cont. * fixed misc bugs --------- Co-authored-by: Sean Fong Co-authored-by: Philip Jian Co-authored-by: michellelin1 Co-authored-by: ThatMegamind <92563733+ThatMegamind@users.noreply.github.com> --- app.js | 4 +- routes/publishedSchedule.js | 138 ++++++++++++++++++++++++++ server/queries/published_schedule.sql | 26 ----- 3 files changed, 140 insertions(+), 28 deletions(-) create mode 100644 routes/publishedSchedule.js delete mode 100644 server/queries/published_schedule.sql diff --git a/app.js b/app.js index 12279c2..20aa7a6 100644 --- a/app.js +++ b/app.js @@ -1,5 +1,6 @@ const express = require('express'); const cors = require('cors'); +const publishedScheduleRouter = require('./routes/publishedSchedule'); require('dotenv').config(); @@ -25,10 +26,9 @@ app.use( // add all routes under here app.use(express.json()); // for req.body +app.use('/published-schedule', publishedScheduleRouter); app.use('/users', users); - app.use('/catalog', catalogRouter); - app.use('/nodeMailer', email); app.listen(PORT, () => { diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js new file mode 100644 index 0000000..d5fe471 --- /dev/null +++ b/routes/publishedSchedule.js @@ -0,0 +1,138 @@ +const express = require('express'); +const { db } = require('../server/db'); +const { keysToCamel } = require('../common/utils'); + +const publishedScheduleRouter = express.Router(); + +// GET - Returns all data from the published_schedule table +publishedScheduleRouter.get('/', async (req, res) => { + try { + const allPublishedSchedules = await db.query( + ` + SELECT + PS.id, + C.host, + C.title, + PS.confirmed, + PS.confirmed_on, + PS.start_time, + PS.end_time, + PS.cohort, + PS.notes + FROM + published_schedule PS + LEFT JOIN catalog C ON PS.event_id = C.id; + `, + ); + res.status(200).json(keysToCamel(allPublishedSchedules)); + } catch (err) { + res.status(500).send(err.message); + } +}); + +// GET/:id - returns the rows that match the given id +publishedScheduleRouter.get('/:id', async (req, res) => { + try { + const { id } = req.params; + const publishedScheduleResult = await db.query( + ` + SELECT + PS.id, + C.host, + C.title, + PS.confirmed, + PS.confirmed_on, + PS.start_time, + PS.end_time, + PS.cohort, + PS.notes + FROM + published_schedule PS + LEFT JOIN catalog C ON PS.event_id = C.id + WHERE PS.id = $1; + `, + [id], + ); + res.status(200).json(keysToCamel(publishedScheduleResult)); + } catch (err) { + res.status(500).send(err.message); + } +}); + +// POST - Adds a new row to the published_schedule table +publishedScheduleRouter.post('/', async (req, res) => { + const { id, eventId, confirmed, confirmedOn, startTime, endTime, cohort, notes } = req.body; + try { + await db.query( + ` + INSERT INTO + published_schedule ( + id, + event_id, + confirmed, + confirmed_on, + start_time, + end_time, + cohort, + notes + ) + VALUES + ($1, $2, $3, $4, $5, $6, $7, $8); + `, + [id, eventId, confirmed, confirmedOn, startTime, endTime, cohort, notes], + ); + res.status(201).json({ + status: 'Success', + id, + }); + } catch (err) { + res.status(500).send(err.message); + } +}); + +// PUT/:id - Updates an existing row given an id +publishedScheduleRouter.put('/:id', async (req, res) => { + try { + const { id } = req.params; + const { eventId, confirmed, confirmedOn, startTime, endTime, cohort, notes } = req.body; + const updatedPublishedSchedule = await db.query( + ` + UPDATE published_schedule + SET + event_id = COALESCE($1, event_id), + confirmed = COALESCE($2, confirmed), + confirmed_on = COALESCE($3, confirmed_on), + start_time = COALESCE($4, start_time), + end_time = COALESCE($5, end_time), + cohort = COALESCE($6, cohort), + notes = COALESCE($7, notes) + WHERE id = $8 + + RETURNING *; + `, + [eventId, confirmed, confirmedOn, startTime, endTime, cohort, notes, id], + ); + res.status(200).json(keysToCamel(updatedPublishedSchedule)); + } catch (err) { + res.status(500).send(err.message); + } +}); + +// DELETE/:id - deletes an existing row given an id +publishedScheduleRouter.delete('/:id', async (req, res) => { + try { + const { id } = req.params; + const deletedEntry = await db.query( + ` + DELETE FROM published_schedule + WHERE id = $1 RETURNING *; + `, + [id], + ); + res.status(200).send(deletedEntry); + } catch (err) { + res.status(500).send(err.message); + } +}); + +module.exports = publishedScheduleRouter; diff --git a/server/queries/published_schedule.sql b/server/queries/published_schedule.sql deleted file mode 100644 index a730dcb..0000000 --- a/server/queries/published_schedule.sql +++ /dev/null @@ -1,26 +0,0 @@ -/*GET - Returns all data from the published_schedule table*/ -SELECT * FROM published_schedule; - -/*GET/:id - returns the rows that match the given id*/ -SELECT * FROM published_schedule WHERE id = ?; - -/*POST - Adds a new row to the published_schedule table -Note: Confirmed should be defaulted to true*/ -INSERT INTO published_schedule (id, event_id, confirmed, confirmed_on, start_time, end_time, cohort, notes) -VALUES (?, ?, true, ?, ?, ?, ?, ?); - -/*PUT - Updates an existing row given an id -Notes: All fields are optional*/ -UPDATE published_schedule -SET - event_id = COALESCE(?, event_id), - confirmed = COALESCE(?, confirmed), - confirmed_on = COALESCE(?, confirmed_on), - start_time = COALESCE(?, start_time), - end_time = COALESCE(?, end_time), - cohort = COALESCE(?, cohort), - notes = COALESCE(?, notes) -WHERE id = ?; - -/*DELETE - deletes an existing row given an id*/ -DELETE FROM published_schedule WHERE id = ?; \ No newline at end of file