|
| 1 | +const express = require('express'); |
| 2 | + |
| 3 | +const { db } = require('../server/db'); |
| 4 | + |
| 5 | +const catalogRouter = express.Router(); |
| 6 | +const { keysToCamel } = require('../common/utils'); |
| 7 | + |
| 8 | +// -- GET - Returns all data from the catalog table |
| 9 | +catalogRouter.get('/', async (req, res) => { |
| 10 | + try { |
| 11 | + const allInfo = await db.query(`SELECT * from catalog;`); |
| 12 | + res.status(200).json(keysToCamel(allInfo)); |
| 13 | + } catch (err) { |
| 14 | + res.status(500).send(err.message); |
| 15 | + } |
| 16 | +}); |
| 17 | + |
| 18 | +// -- GET/:id - Returns the row that matches the id |
| 19 | +catalogRouter.get('/:id', async (req, res) => { |
| 20 | + try { |
| 21 | + const { id } = req.params; |
| 22 | + const allUsers = await db.query(`SELECT * FROM catalog WHERE id = $1;`, [id]); |
| 23 | + res.status(200).json(keysToCamel(allUsers)); |
| 24 | + } catch (err) { |
| 25 | + res.status(500).send(err.message); |
| 26 | + } |
| 27 | +}); |
| 28 | + |
| 29 | +// -- POST - Adds a new row to the catalog table |
| 30 | +catalogRouter.post('/', async (req, res) => { |
| 31 | + const { id, host, title, eventType, subject, description, year } = req.body; |
| 32 | + try { |
| 33 | + await db.query( |
| 34 | + `INSERT INTO catalog (id, host, title, event_type, subject, description, year) |
| 35 | + VALUES ($1, $2, $3, $4, $5, $6, $7);`, |
| 36 | + [id, host, title, eventType, subject, description, year], |
| 37 | + ); |
| 38 | + res.status(201).json({ id, status: 'Success' }); |
| 39 | + } catch (err) { |
| 40 | + res.status(500).json({ |
| 41 | + status: 'Failed', |
| 42 | + msg: err.message, |
| 43 | + }); |
| 44 | + } |
| 45 | +}); |
| 46 | + |
| 47 | +// -- PUT - Updates an existing row given an id |
| 48 | +// -- All fields are optional |
| 49 | +catalogRouter.put('/:id', async (req, res) => { |
| 50 | + try { |
| 51 | + const { id } = req.params; |
| 52 | + const { host, title, eventType, subject, description, year } = req.body; |
| 53 | + |
| 54 | + const updatedCatalog = await db.query( |
| 55 | + `UPDATE catalog SET |
| 56 | + ${host ? 'host = $(host), ' : ''} |
| 57 | + ${title ? 'title = $(title),' : ''} |
| 58 | + ${eventType ? 'event_type = $(eventType), ' : ''} |
| 59 | + ${subject ? 'subject = $(subject), ' : ''} |
| 60 | + ${description ? 'description = $(description), ' : ''} |
| 61 | + ${year ? 'year = $(year), ' : ''} |
| 62 | + id = '${id}' |
| 63 | + WHERE id = '${id}' |
| 64 | + RETURNING *;`, |
| 65 | + { |
| 66 | + host, |
| 67 | + title, |
| 68 | + eventType, |
| 69 | + subject, |
| 70 | + description, |
| 71 | + year, |
| 72 | + id, |
| 73 | + }, |
| 74 | + ); |
| 75 | + res.status(200).send(keysToCamel(updatedCatalog)); |
| 76 | + } catch (err) { |
| 77 | + res.status(500).send(err.message); |
| 78 | + } |
| 79 | +}); |
| 80 | + |
| 81 | +// -- DELETE - deletes an existing row given an id |
| 82 | +catalogRouter.delete('/:id', async (req, res) => { |
| 83 | + try { |
| 84 | + const { id } = req.params; |
| 85 | + const delUser = await db.query(`DELETE FROM catalog WHERE id = $1 RETURNING *;`, [id]); |
| 86 | + res.status(200).send(keysToCamel(delUser)); |
| 87 | + } catch (err) { |
| 88 | + res.status(500).send(err.message); |
| 89 | + } |
| 90 | +}); |
| 91 | + |
| 92 | +module.exports = catalogRouter; |
0 commit comments