Skip to content

Commit

Permalink
Make Backend Routes for Published Schedule (#29)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
Co-authored-by: Philip Jian <[email protected]>
Co-authored-by: michellelin1 <[email protected]>
Co-authored-by: ThatMegamind <[email protected]>
  • Loading branch information
5 people authored Jan 3, 2024
1 parent e6e02f1 commit 699f437
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 28 deletions.
4 changes: 2 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const express = require('express');
const cors = require('cors');
const publishedScheduleRouter = require('./routes/publishedSchedule');

require('dotenv').config();

Expand All @@ -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, () => {
Expand Down
138 changes: 138 additions & 0 deletions routes/publishedSchedule.js
Original file line number Diff line number Diff line change
@@ -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;
26 changes: 0 additions & 26 deletions server/queries/published_schedule.sql

This file was deleted.

0 comments on commit 699f437

Please sign in to comment.