Skip to content

Commit

Permalink
Minor DB Updates (#32)
Browse files Browse the repository at this point in the history
* Create a pull trequest for branch 31-minor-db-updates

* updated db model and queries

---------

Co-authored-by: michellelin1 <[email protected]>
  • Loading branch information
ctc-devops and michellelin1 authored Jan 8, 2024
1 parent 699f437 commit 39cccb0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
15 changes: 8 additions & 7 deletions routes/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ catalogRouter.get('/:id', async (req, res) => {

// -- POST - Adds a new row to the catalog table
catalogRouter.post('/', async (req, res) => {
const { id, host, title, eventType, subject, description, year } = req.body;
const { host, title, eventType, subject, description, year } = req.body;
try {
await db.query(
const returnedData = await db.query(
`INSERT INTO catalog (id, host, title, event_type, subject, description, year)
VALUES ($1, $2, $3, $4, $5, $6, $7);`,
[id, host, title, eventType, subject, description, year],
VALUES (nextval('catalog_id_seq'), $1, $2, $3, $4, $5, $6)
RETURNING id;`,
[host, title, eventType, subject, description, year],
);
res.status(201).json({ id, status: 'Success' });
res.status(201).json({ id: returnedData[0].id, status: 'Success' });
} catch (err) {
res.status(500).json({
status: 'Failed',
Expand All @@ -52,15 +53,15 @@ catalogRouter.put('/:id', async (req, res) => {
const { host, title, eventType, subject, description, year } = req.body;

const updatedCatalog = await db.query(
`UPDATE catalog SET
`UPDATE catalog SET
${host ? 'host = $(host), ' : ''}
${title ? 'title = $(title),' : ''}
${eventType ? 'event_type = $(eventType), ' : ''}
${subject ? 'subject = $(subject), ' : ''}
${description ? 'description = $(description), ' : ''}
${year ? 'year = $(year), ' : ''}
id = '${id}'
WHERE id = '${id}'
WHERE id = '${id}'
RETURNING *;`,
{
host,
Expand Down
11 changes: 6 additions & 5 deletions routes/publishedSchedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ publishedScheduleRouter.get('/:id', async (req, res) => {

// 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;
const { eventId, confirmed, confirmedOn, startTime, endTime, cohort, notes } = req.body;
try {
await db.query(
const returnedData = await db.query(
`
INSERT INTO
published_schedule (
Expand All @@ -77,13 +77,14 @@ publishedScheduleRouter.post('/', async (req, res) => {
notes
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8);
(nextval('published_schedule_id_seq'), $1, $2, $3, $4, $5, $6, $7)
RETURNING id;
`,
[id, eventId, confirmed, confirmedOn, startTime, endTime, cohort, notes],
[eventId, confirmed, confirmedOn, startTime, endTime, cohort, notes],
);
res.status(201).json({
status: 'Success',
id,
id: returnedData[0].id,
});
} catch (err) {
res.status(500).send(err.message);
Expand Down
4 changes: 2 additions & 2 deletions server/schema/catalog.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
CREATE TYPE event AS ENUM ('guest speaker', 'study-trip', 'workshop');
CREATE TYPE event AS ENUM ('guest speaker', 'study-trip', 'workshop', 'other');
CREATE TYPE subject AS ENUM ('life skills', 'science', 'technology', 'engineering', 'math', 'college readiness');
CREATE TYPE year AS ENUM ('junior', 'senior', 'both');

DROP TABLE IF EXISTS catalog;
CREATE TABLE catalog (
id VARCHAR(10) PRIMARY KEY,
id SERIAL PRIMARY KEY,
host VARCHAR(50) NOT NULL,
title VARCHAR(50) NOT NULL,
event_type event NOT NULL,
Expand Down
6 changes: 3 additions & 3 deletions server/schema/published_schedule.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
DROP TABLE IF EXISTS published_schedule;
CREATE TABLE IF NOT EXISTS published_schedule (
id varchar(10) NOT NULL,
event_id varchar(10) NOT NULL,
id serial NOT NULL,
event_id integer NOT NULL,
confirmed boolean NOT NULL,
confirmed_on date NOT NULL,
start_time timestamp NOT NULL,
end_time timestamp NOT NULL,
cohort year NOT NULL,
cohort varchar[] NOT NULL,
notes varchar(100),
FOREIGN KEY (event_id)
REFERENCES catalog (id)
Expand Down

0 comments on commit 39cccb0

Please sign in to comment.