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;