Skip to content

Commit

Permalink
fixed schema (for realsies this time) and updated some sql queries be…
Browse files Browse the repository at this point in the history
…cause db changed
  • Loading branch information
h0ethan04 committed Mar 14, 2024
1 parent 0d3708a commit c693ca2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
20 changes: 9 additions & 11 deletions routes/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ catalogRouter.get('/:id', async (req, res) => {

// -- POST - Adds a new row to the catalog table
catalogRouter.post('/', async (req, res) => {
const { host, title, eventType, subject, description, year, season, location } = req.body;
const { host, title, eventType, subject, description, year, season } = req.body;
try {
const returnedData = await db.query(
`INSERT INTO catalog (id, host, title, event_type, subject, description, year, season, location, hidden)
VALUES (nextval('catalog_id_seq'), $1, $2, $3, $4, $5, $6, $7, $8, false)
`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 id;`,
[host, title, eventType, subject, description, year, season, location],
[host, title, eventType, subject, description, year, season],
);
res.status(201).json({ id: returnedData[0].id, status: 'Success' });
} catch (err) {
Expand All @@ -118,18 +118,17 @@ catalogRouter.post('/', async (req, res) => {
catalogRouter.put('/:id', async (req, res) => {
try {
const { id } = req.params;
const { host, title, eventType, subject, description, year, location, season } = req.body;
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), ' : ''}
${subject ? 'subject = $(subject), ' : ''}
${eventType ? 'event_type = $(eventType)::event[], ' : ''}
${subject ? 'subject = $(subject)::subject[], ' : ''}
${description ? 'description = $(description), ' : ''}
${year ? 'year = $(year), ' : ''}
${location ? 'location = $(location), ' : ''}
${season ? 'season = $(season), ' : ''}
${year ? 'year = $(year)::year[], ' : ''}
${season ? 'season = $(season)::season[], ' : ''}
id = '${id}'
WHERE id = '${id}'
RETURNING *;`,
Expand All @@ -141,7 +140,6 @@ catalogRouter.put('/:id', async (req, res) => {
description,
year,
id,
location,
season,
},
);
Expand Down
5 changes: 2 additions & 3 deletions server/schema/catalog.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ CREATE TABLE catalog (
id SERIAL PRIMARY KEY,
host VARCHAR(50) NOT NULL,
title VARCHAR(50) NOT NULL,
event_type event NOT NULL,
subject subject NOT NULL,
event_type event[] NOT NULL,
subject subject[] NOT NULL,
description VARCHAR(50) NOT NULL,
year year NOT NULL,
season season,
location VARCHAR(256),
hidden boolean NOT NULL
);

0 comments on commit c693ca2

Please sign in to comment.