generated from ctc-uci/npo-backend-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> --------- Co-authored-by: Alyssia Tan <[email protected]> Co-authored-by: liannejl <[email protected]> Co-authored-by: liannejl <[email protected]>
- Loading branch information
1 parent
15d3d25
commit a2965f7
Showing
4 changed files
with
97 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
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; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters