generated from ctc-uci/npo-backend-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Co-authored-by: Philip Jian <[email protected]> Co-authored-by: michellelin1 <[email protected]> Co-authored-by: ThatMegamind <[email protected]>
- Loading branch information
1 parent
e6e02f1
commit 699f437
Showing
3 changed files
with
140 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.