-
Notifications
You must be signed in to change notification settings - Fork 0
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
setup_values_get_put_delete Issue #11 Resolved #16
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,123 @@ | ||
// const express = require('express'); | ||
// const { db } = require('../server/db'); | ||
const express = require('express'); | ||
const { db } = require('../server/db'); | ||
|
||
// Your code below (delete this line when committing): | ||
const valueRouter = express.Router(); | ||
|
||
// const valueRouter = express.Router(); | ||
valueRouter.get('/', async (req, res) => { | ||
try { | ||
const allValues = await db.query(` | ||
SELECT * | ||
FROM business; | ||
`); | ||
res.status(200).send(allValues); | ||
} catch (err) { | ||
res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
valueRouter.get('/:id', async (req, res) => { | ||
try { | ||
const { id } = req.params; | ||
const value = await db.query( | ||
` | ||
SELECT * | ||
FROM donation_tracking | ||
WHERE donation_id = $1; | ||
`, | ||
[id], | ||
); | ||
res.status(200).send(value); | ||
} catch (err) { | ||
res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
valueRouter.delete('/:id', async (req, res) => { | ||
try { | ||
const { id } = req.params; | ||
await db.query('DELETE from donation_tracking WHERE donation_id = $1;', [id]); | ||
res.status(200).send('Deleted donation'); | ||
} catch (err) { | ||
res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
valueRouter.put('/:id', async (req, res) => { | ||
try { | ||
const { id } = req.params; | ||
const { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. const { business_id: businessId, message, timestamp, been_dismissed: beenDismissed } = req.body; do it like so, we redefine the snake case into camel case so that we have consistent variables across our different code packages |
||
business_id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
donation_id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
food_bank_donation, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
reporter, | ||
email, | ||
date, | ||
canned_dog_food_quantity, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
dry_dog_food_quantity, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
canned_cat_food_quantity, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
dry_cat_food_quantity, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
misc_items, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
volunteer_hours, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} = req.body; | ||
if (!business_id) throw new Error('business_id is required.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove this, we will be doing frontend validation instead. |
||
if (!donation_id) throw new Error('donation_id line is required.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (!food_bank_donation) throw new Error('food_bank_donation is required.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (!reporter) throw new Error('business_id is required.'); | ||
if (!email) throw new Error('donation_id line is required.'); | ||
if (!date) throw new Error('food_bank_donation is required.'); | ||
if (!canned_dog_food_quantity) throw new Error('canned_dog_food_quantity is required.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (!dry_dog_food_quantity) throw new Error('dry_dog_food_quantity line is required.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (!canned_cat_food_quantity) throw new Error('canned_cat_food_quanitty is required.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (!dry_cat_food_quantity) throw new Error('dry_cat_food_quantity is required.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (!misc_items) throw new Error('misc_items line is required.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
if (!volunteer_hours) throw new Error('volunteer_hours is required.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove all of these "is required" lines", we will be doing frontend validation instead. |
||
|
||
const updatedValue = await db.query( | ||
`UPDATE donation_tracking | ||
SET donation_id = $(id) | ||
${business_id ? `, business_id = $(business_id) ` : ''} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
${donation_id ? `, donation_id = $(donation_id) ` : ''} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line is redundant with the `SET donation_id = $(id) part |
||
${food_bank_donation ? `, food_bank_donation = $(food_bank_donation) ` : ''} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
${reporter ? `, reporter = $(reporter) ` : ''} | ||
${email ? `, email = $(email) ` : ''} | ||
${date ? `, date = $(date) ` : ''} | ||
${ | ||
canned_dog_food_quantity | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
? `, canned_dog_food_quantity = $(canned_dog_food_quantity) ` | ||
: '' | ||
} | ||
${dry_dog_food_quantity ? `, dry_dog_food_quantity = $(dry_dog_food_quantity) ` : ''} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
${ | ||
canned_cat_food_quantity | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
? `, canned_cat_food_quanitty = $(canned_cat_food_quanitty) ` | ||
: '' | ||
} | ||
${dry_cat_food_quantity ? `, dry_cat_food_quantity = $(dry_cat_food_quantity) ` : ''} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
${misc_items ? `, misc_items = $(misc_items) ` : ''} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
${volunteer_hours ? `, volunteer_hours = $(volunteer_hours) ` : ''} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
WHERE id = $(id) | ||
RETURNING *;`, | ||
{ | ||
id, | ||
business_id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
donation_id, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
food_bank_donation, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
reporter, | ||
email, | ||
date, | ||
canned_dog_food_quantity, | ||
dry_dog_food_quantity, | ||
canned_cat_food_quantity, | ||
dry_cat_food_quantity, | ||
misc_items, | ||
volunteer_hours, | ||
}, | ||
); | ||
return res.status(200).send(updatedValue); | ||
} catch (err) { | ||
return res.status(500).send(err.message); | ||
} | ||
}); | ||
|
||
module.exports = valueRouter; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make sure to pull in the code next time before you work, otherwise we will lose some code