Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Backend Routes for Published Schedule #29

Merged
merged 13 commits into from
Jan 3, 2024
Merged
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) => {
ThatMegamind marked this conversation as resolved.
Show resolved Hide resolved
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',
ThatMegamind marked this conversation as resolved.
Show resolved Hide resolved
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
ThatMegamind marked this conversation as resolved.
Show resolved Hide resolved
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) => {
ThatMegamind marked this conversation as resolved.
Show resolved Hide resolved
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.

Loading