From 7c11b5504c8661ba8efc24ddfbf5de6323294c7e Mon Sep 17 00:00:00 2001 From: liannejl Date: Fri, 1 Dec 2023 14:42:52 -0800 Subject: [PATCH] Completed Routes for Catelog --- routes/catalog.js | 36 ++++++++++++++++++++++++++---------- server/queries/catalog.sql | 22 ---------------------- 2 files changed, 26 insertions(+), 32 deletions(-) delete mode 100644 server/queries/catalog.sql diff --git a/routes/catalog.js b/routes/catalog.js index b41004a..281df50 100644 --- a/routes/catalog.js +++ b/routes/catalog.js @@ -1,15 +1,15 @@ const express = require('express'); -const db = require('../server/db'); +const { db } = require('../server/db'); const catalogRouter = express.Router(); -const { toCamel } = require('../common/utils'); +const { keysToCamel } = require('../common/utils'); // -- GET - Returns all data from the catalog table catalogRouter.get('/', async (req, res) => { try { const allInfo = await db.query(`SELECT * from catalog;`); - res.status(200).json(toCamel(allInfo)); + res.status(200).json(keysToCamel(allInfo)); } catch (err) { console.log(err); res.status(500).send(err.message); @@ -22,7 +22,7 @@ catalogRouter.get('/:id', async (req, res) => { try { const { id } = req.params; const allUsers = await db.query(`SELECT * FROM catalog WHERE id = $1;`, [id]); - res.status(200).json(toCamel(allUsers)); + res.status(200).json(keysToCamel(allUsers)); } catch (err) { console.log(err); res.status(500).send(err.message); @@ -62,11 +62,27 @@ catalogRouter.put('/:id', async (req, res) => { const { host, title, eventType, subject, description, year } = req.body; const updatedCatalog = await db.query( - `UPDATE catalog SET host = $1, title = $2, event_type = $3, subject = $4, - description = $5, year = $6 WHERE id = $7;`, - [host, title, eventType, subject, description, year, id], + `UPDATE catalog SET + ${host ? 'host = $(host), ' : ''} + ${title ? 'title = $(title),' : ''} + ${eventType ? 'event_type = $(eventType), ' : ''} + ${subject ? 'subject = $(subject), ' : ''} + ${description ? 'description = $(description), ' : ''} + ${year ? 'year = $(year), ' : ''} + id = '${id}' + WHERE id = '${id}' + RETURNING *;`, + { + host, + title, + eventType, + subject, + description, + year, + id, + }, ); - res.status(200).send(updatedCatalog.rows[0]); + res.status(200).send(keysToCamel(updatedCatalog)); } catch (err) { console.log(err); res.status(500).send(err.message); @@ -78,8 +94,8 @@ catalogRouter.put('/:id', async (req, res) => { catalogRouter.delete('/:id', async (req, res) => { try { const { id } = req.params; - const delUser = await db.query(`DELETE FROM catalog WHERE id = $1;`, [id]); - res.status(200).json(toCamel(delUser)); + const delUser = await db.query(`DELETE FROM catalog WHERE id = $1 RETURNING *;`, [id]); + res.status(200).send(keysToCamel(delUser)); } catch (err) { console.log(err); res.status(500).send(err.message); diff --git a/server/queries/catalog.sql b/server/queries/catalog.sql deleted file mode 100644 index d1a67d0..0000000 --- a/server/queries/catalog.sql +++ /dev/null @@ -1,22 +0,0 @@ --- GET - Returns all data from the catalog table -SELECT * FROM catalog; - - --- GET/:id - Returns the row that matches the id -SELECT * FROM catalog WHERE id = ?; - - --- POST - Adds a new row to the catalog table -INSERT INTO catalog (id, host, title, event_type, subject, description, year) -VALUES (?, ?, ?, ?, ?, ?, ?); - - --- PUT - Updates an existing row given an id --- All fields are optional -UPDATE catalog -SET host = ?, title = ?, event_type = ?, subject = ?, description = ?, year = ? -WHERE id = ?; - - --- DELETE - deletes an existing row given an id -DELETE FROM catalog WHERE id=?; \ No newline at end of file