Skip to content

Commit

Permalink
Completed Routes for Catelog
Browse files Browse the repository at this point in the history
  • Loading branch information
liannejl committed Dec 1, 2023
1 parent 0ee6a68 commit 7c11b55
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
36 changes: 26 additions & 10 deletions routes/catalog.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
22 changes: 0 additions & 22 deletions server/queries/catalog.sql

This file was deleted.

0 comments on commit 7c11b55

Please sign in to comment.