From a2965f795ab7daedbfbc0ad8734494b404b30b45 Mon Sep 17 00:00:00 2001 From: ctc-devops <90984711+ctc-devops@users.noreply.github.com> Date: Mon, 18 Dec 2023 11:55:38 -0800 Subject: [PATCH] Make backend routes for Catelog (#27) * created catalog.js, made progress * added :id to the router functions, changed delete query * Completed code for routes. Running into db.query is not a function error * Completed Routes for Catelog * removed comments, fixed yarn lock * removed console logs * fixed ? Co-authored-by: liannejl --------- Co-authored-by: Alyssia Tan Co-authored-by: liannejl Co-authored-by: liannejl --- app.js | 4 ++ routes/catalog.js | 92 ++++++++++++++++++++++++++++++++++++++ server/queries/catalog.sql | 22 --------- yarn.lock | 2 +- 4 files changed, 97 insertions(+), 23 deletions(-) create mode 100644 routes/catalog.js delete mode 100644 server/queries/catalog.sql diff --git a/app.js b/app.js index 641de4c..1f2c723 100644 --- a/app.js +++ b/app.js @@ -8,6 +8,8 @@ const users = require('./routes/users'); const app = express(); +const catalogRouter = require('./routes/catalog'); + const PORT = process.env.PORT || 3001; app.use( @@ -20,6 +22,8 @@ app.use( app.use(express.json()); // for req.body app.use('/users', users); +app.use('/catalog', catalogRouter); + app.listen(PORT, () => { console.log(`Server listening on ${PORT}`); }); diff --git a/routes/catalog.js b/routes/catalog.js new file mode 100644 index 0000000..0f98bb1 --- /dev/null +++ b/routes/catalog.js @@ -0,0 +1,92 @@ +const express = require('express'); + +const { db } = require('../server/db'); + +const catalogRouter = express.Router(); +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(keysToCamel(allInfo)); + } catch (err) { + res.status(500).send(err.message); + } +}); + +// -- GET/:id - Returns the row that matches the id +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(keysToCamel(allUsers)); + } catch (err) { + res.status(500).send(err.message); + } +}); + +// -- POST - Adds a new row to the catalog table +catalogRouter.post('/', async (req, res) => { + const { id, host, title, eventType, subject, description, year } = req.body; + try { + await db.query( + `INSERT INTO catalog (id, host, title, event_type, subject, description, year) + VALUES ($1, $2, $3, $4, $5, $6, $7);`, + [id, host, title, eventType, subject, description, year], + ); + res.status(201).json({ id, status: 'Success' }); + } catch (err) { + res.status(500).json({ + status: 'Failed', + msg: err.message, + }); + } +}); + +// -- PUT - Updates an existing row given an id +// -- All fields are optional +catalogRouter.put('/:id', async (req, res) => { + try { + const { id } = req.params; + const { host, title, eventType, subject, description, year } = req.body; + + const updatedCatalog = await db.query( + `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(keysToCamel(updatedCatalog)); + } catch (err) { + res.status(500).send(err.message); + } +}); + +// -- DELETE - deletes an existing row given an id +catalogRouter.delete('/:id', async (req, res) => { + try { + const { id } = req.params; + const delUser = await db.query(`DELETE FROM catalog WHERE id = $1 RETURNING *;`, [id]); + res.status(200).send(keysToCamel(delUser)); + } catch (err) { + res.status(500).send(err.message); + } +}); + +module.exports = catalogRouter; 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 diff --git a/yarn.lock b/yarn.lock index bef156e..53bf992 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2497,4 +2497,4 @@ yallist@^4.0.0: yaml@^1.10.0: version "1.10.2" resolved "https://registry.yarnpkg.com/yaml/-/yaml-1.10.2.tgz#2301c5ffbf12b467de8da2333a459e29e7920e4b" - integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg== + integrity sha512-r3vXyErRCYJ7wg28yvBY5VSoAF8ZvlcW9/BwUzEtUsjvX/DKs24dIkuwjtuprwJJHsbyUbLApepYTR1BN4uHrg== \ No newline at end of file