From 3b645a8aae7a96da97ce9ea15b761fcda862870b Mon Sep 17 00:00:00 2001 From: ThatMegamind <92563733+ThatMegamind@users.noreply.github.com> Date: Mon, 29 Apr 2024 11:56:21 -0700 Subject: [PATCH] Tl bug fixes (#68) * fix existing day issue * added year to return with ps * removed both option * removed redundant addition of C.year --------- Co-authored-by: michellelin1 --- routes/day.js | 25 +++++++++++++++++++------ server/schema/catalog.sql | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/routes/day.js b/routes/day.js index faf7762..9f9b469 100644 --- a/routes/day.js +++ b/routes/day.js @@ -40,12 +40,25 @@ dayRouter.get('/:id', async (req, res) => { dayRouter.post('/', async (req, res) => { try { const { eventDate, location, notes } = req.body; - const inUse = await db.query(`SELECT * FROM day WHERE event_date = $1;`, [eventDate]); - if (inUse.length) { - res.status(201).json({ - status: 'Failed', - message: 'Day already exists', - }); + const existingDay = await db.query(`SELECT * FROM day WHERE event_date = $1;`, [eventDate]); + if (existingDay.length) { + // day exists but has no events --> update location + notes return existing day_id + if (existingDay[0].day_count === 0) { + await db.query(`UPDATE day SET location = $1, notes = $2 WHERE id = $3;`, [ + location, + notes, + existingDay[0].id, + ]); + res.status(201).json({ + status: 'Success', + id: existingDay[0].id, + }); + } else { + res.status(201).json({ + status: 'Failed', + message: 'Day already exists', + }); + } return; } diff --git a/server/schema/catalog.sql b/server/schema/catalog.sql index 31b97a4..06544ad 100644 --- a/server/schema/catalog.sql +++ b/server/schema/catalog.sql @@ -1,6 +1,6 @@ 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'); +CREATE TYPE year AS ENUM ('junior', 'senior'); CREATE TYPE season AS ENUM ('spring', 'summer', 'fall'); DROP TABLE IF EXISTS catalog;