Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make backend routes for Catelog #27

Merged
merged 7 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ require('dotenv').config();

const app = express();

const catalogRouter = require('./routes/catalog');

const PORT = process.env.PORT || 3001;

app.use(
Expand All @@ -16,6 +18,8 @@ app.use(
// add all routes under here
app.use(express.json()); // for req.body

app.use('/catalog', catalogRouter);

app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`);
});
92 changes: 92 additions & 0 deletions routes/catalog.js
Original file line number Diff line number Diff line change
@@ -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({
ThatMegamind marked this conversation as resolved.
Show resolved Hide resolved
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;
22 changes: 0 additions & 22 deletions server/queries/catalog.sql

This file was deleted.

2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Loading