Skip to content

Commit

Permalink
updated catalog schema + routes
Browse files Browse the repository at this point in the history
  • Loading branch information
michellelin1 authored Jan 23, 2024
1 parent 9556edd commit 3ef8a11
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
14 changes: 9 additions & 5 deletions routes/catalog.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,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 } = req.body;
const { host, title, eventType, subject, description, year, season, location } = req.body;
try {
const returnedData = await db.query(
`INSERT INTO catalog (id, host, title, event_type, subject, description, year)
VALUES (nextval('catalog_id_seq'), $1, $2, $3, $4, $5, $6)
`INSERT INTO catalog (id, host, title, event_type, subject, description, year, season, location)
VALUES (nextval('catalog_id_seq'), $1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id;`,
[host, title, eventType, subject, description, year],
[host, title, eventType, subject, description, year, season, location],
);
res.status(201).json({ id: returnedData[0].id, status: 'Success' });
} catch (err) {
Expand All @@ -50,7 +50,7 @@ catalogRouter.post('/', async (req, res) => {
catalogRouter.put('/:id', async (req, res) => {
try {
const { id } = req.params;
const { host, title, eventType, subject, description, year } = req.body;
const { host, title, eventType, subject, description, year, location, season } = req.body;

const updatedCatalog = await db.query(
`UPDATE catalog SET
Expand All @@ -60,6 +60,8 @@ catalogRouter.put('/:id', async (req, res) => {
${subject ? 'subject = $(subject), ' : ''}
${description ? 'description = $(description), ' : ''}
${year ? 'year = $(year), ' : ''}
${location ? 'location = $(location), ' : ''}
${season ? 'season = $(season), ' : ''}
id = '${id}'
WHERE id = '${id}'
RETURNING *;`,
Expand All @@ -71,6 +73,8 @@ catalogRouter.put('/:id', async (req, res) => {
description,
year,
id,
location,
season,
},
);
res.status(200).send(keysToCamel(updatedCatalog));
Expand Down
5 changes: 4 additions & 1 deletion server/schema/catalog.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 season AS ENUM ('spring', 'summer', 'fall', 'winter');

DROP TABLE IF EXISTS catalog;
CREATE TABLE catalog (
Expand All @@ -10,5 +11,7 @@ CREATE TABLE catalog (
event_type event NOT NULL,
subject subject NOT NULL,
description VARCHAR(50) NOT NULL,
year year NOT NULL
year year NOT NULL,
season season,
location VARCHAR(256)
);

0 comments on commit 3ef8a11

Please sign in to comment.