Skip to content

Commit

Permalink
change catalog put route
Browse files Browse the repository at this point in the history
  • Loading branch information
michellelin1 committed Mar 29, 2024
1 parent 2c13a25 commit 037f981
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
60 changes: 37 additions & 23 deletions routes/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions routes/publishedSchedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 037f981

Please sign in to comment.