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
2 changes: 2 additions & 0 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 @@ -15,6 +16,7 @@ app.use(

// add all routes under here
app.use(express.json()); // for req.body
app.use('/published-schedule', publishedScheduleRouter);

app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
Expand Down
150 changes: 150 additions & 0 deletions routes/publishedSchedule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
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 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
AND PS.id = $1;
`,
[req.params.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,
event_id: eventId,
ThatMegamind marked this conversation as resolved.
Show resolved Hide resolved
confirmed_on: confirmedOn,
start_time: startTime,
end_time: 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, true, $3, $4, $5, $6, $7);
ThatMegamind marked this conversation as resolved.
Show resolved Hide resolved
`,
[id, eventId, confirmedOn, startTime, endTime, cohort, notes],
);
res.status(201).json({
status: 'Success',
ThatMegamind marked this conversation as resolved.
Show resolved Hide resolved
});
} catch (err) {
res.status(500).send(err.message);
}
});

// PUT/:id - Updates an existing row given an id
publishedScheduleRouter.put('/id', async (req, res) => {

try {
ThatMegamind marked this conversation as resolved.
Show resolved Hide resolved
const { id } = req.params;
const {
event_id: eventId,
confirmed_on: confirmedOn,
start_time: startTime,
end_time: 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(?, 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 = ?;
`,
[id, eventId, confirmedOn, startTime, endTime, cohort, notes],
);
res.status(204).json(keysToCamel(updatedPublishedSchedule));
} catch (err) {
res.status(500).send(err.message);
}
});

// DELETE/:id - deletes an existing row given an id
publishedScheduleRouter.put('/id', async (req, res) => {
try {
const { id } = req.params;
await db.query(
`
DELETE FROM published_schedule
WHERE id = ?;
`,
[id]
ThatMegamind marked this conversation as resolved.
Show resolved Hide resolved
);
res.status(200).send('Deleted row from Published Schedule');
} catch (err) {
res.status(500).send(err.message);
}
});

module.exports = publishedScheduleRouter;
29 changes: 27 additions & 2 deletions server/queries/published_schedule.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
/*GET - Returns all data from the published_schedule table*/
SELECT * FROM published_schedule;
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;

/*GET/:id - returns the rows that match the given id*/
SELECT * FROM published_schedule WHERE id = ?;
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
AND PS.id = ?;

/*POST - Adds a new row to the published_schedule table
Note: Confirmed should be defaulted to true*/
Expand Down
Loading