From 650f4093fade9124494db13b08a2aa31694adc99 Mon Sep 17 00:00:00 2001 From: michellelin1 Date: Sun, 7 Jan 2024 17:41:20 -0800 Subject: [PATCH] updated db model and queries --- routes/catalog.js | 15 ++++++++------- routes/publishedSchedule.js | 11 ++++++----- server/schema/catalog.sql | 4 ++-- server/schema/published_schedule.sql | 6 +++--- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/routes/catalog.js b/routes/catalog.js index 0f98bb1..eac9d18 100644 --- a/routes/catalog.js +++ b/routes/catalog.js @@ -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', @@ -52,7 +53,7 @@ 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), ' : ''} @@ -60,7 +61,7 @@ catalogRouter.put('/:id', async (req, res) => { ${description ? 'description = $(description), ' : ''} ${year ? 'year = $(year), ' : ''} id = '${id}' - WHERE id = '${id}' + WHERE id = '${id}' RETURNING *;`, { host, diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index d5fe471..6793999 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -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 ( @@ -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); diff --git a/server/schema/catalog.sql b/server/schema/catalog.sql index f6f1907..d49128c 100644 --- a/server/schema/catalog.sql +++ b/server/schema/catalog.sql @@ -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, diff --git a/server/schema/published_schedule.sql b/server/schema/published_schedule.sql index b6d1932..7d04a9e 100644 --- a/server/schema/published_schedule.sql +++ b/server/schema/published_schedule.sql @@ -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)