diff --git a/routes/catalog.js b/routes/catalog.js index 4a97d75..3552435 100644 --- a/routes/catalog.js +++ b/routes/catalog.js @@ -110,30 +110,44 @@ catalogRouter.put('/:id', async (req, res) => { const { id } = req.params; const { host, title, eventType, subject, description, year, season } = req.body; - const updatedCatalog = await db.query( - `UPDATE catalog SET - ${host ? 'host = $(host), ' : ''} - ${title ? 'title = $(title),' : ''} - ${eventType ? 'event_type = $(eventType)::event[], ' : ''} - ${subject ? 'subject = $(subject)::subject[], ' : ''} - ${description ? 'description = $(description), ' : ''} - ${year ? 'year = $(year)::year[], ' : ''} - ${season ? 'season = $(season)::season[], ' : ''} - id = '${id}' - WHERE id = '${id}' + const { count } = ( + await db.query(`SELECT COUNT(*) FROM published_schedule WHERE event_id = $1;`, [id]) + )[0]; + + if (count === 1) { + const updatedCatalog = await db.query( + `UPDATE catalog SET + ${host ? 'host = $(host), ' : ''} + ${title ? 'title = $(title),' : ''} + ${eventType ? 'event_type = $(eventType)::event[], ' : ''} + ${subject ? 'subject = $(subject)::subject[], ' : ''} + ${description ? 'description = $(description), ' : ''} + ${year ? 'year = $(year)::year[], ' : ''} + ${season ? 'season = $(season)::season[], ' : ''} + id = '${id}' + WHERE id = '${id}' + RETURNING *;`, + { + host, + title, + eventType, + subject, + description, + year, + id, + season, + }, + ); + res.status(200).send(keysToCamel(updatedCatalog)); + } else { + const newCatalogEvent = await db.query( + `INSERT INTO catalog (id, host, title, event_type, subject, description, year, season, hidden) + VALUES (nextval('catalog_id_seq'), $1, $2, $3::event[], $4::subject[], $5, $6::year[], $7::season[], false) RETURNING *;`, - { - host, - title, - eventType, - subject, - description, - year, - id, - season, - }, - ); - res.status(200).send(keysToCamel(updatedCatalog)); + [host, title, eventType, subject, description, year, season], + ); + res.status(200).send(keysToCamel(newCatalogEvent)); + } } catch (err) { res.status(500).send(err.message); } diff --git a/routes/publishedSchedule.js b/routes/publishedSchedule.js index 1e090ce..30aa1af 100644 --- a/routes/publishedSchedule.js +++ b/routes/publishedSchedule.js @@ -284,11 +284,13 @@ publishedScheduleRouter.get('/:id', async (req, res) => { SELECT PS.id, PS.day_id, + PS.event_id, C.host, C.title, C.event_type, C.season, C.subject, + C.year, C.description, PS.confirmed, PS.confirmed_on,