Project setup with dependencies, folder structure, basic code.#252
Project setup with dependencies, folder structure, basic code.#252d2rd wants to merge 9 commits intobloominstituteoftechnology:masterfrom
Conversation
…t and a modules.exports statements
…t and a modules.exports statements
| // res.status(500).json({ errorMessage: 'Error geting budgets', err }) | ||
| // }) | ||
| // }) | ||
| .post('/budget', (req, res) => { |
There was a problem hiding this comment.
The endpoint path should just be /, unless it was intentional to have the complete path be /api/budget/budget.
| }) | ||
| .post((req, res) => { | ||
| const { body } = req | ||
| const category = new Category(body.title) |
There was a problem hiding this comment.
The value we're passing into new Category should be a JSON object. Currently, we're passing in a string. Simple fix would be to change it to:
const category = new Category(req.body)| res.status(500).json({ errorMessage: 'Error getting expense', err }) | ||
| }) | ||
| }) | ||
| .post((req, res) => { |
There was a problem hiding this comment.
In this route, we should be fetching budget and category with the findOne method. Then using Promise.all, we grab the ObjectId for each from the response from MongoDB. Then create our new Expense.
.post((req, res) => {
let { amount, budget, category, description } = req.body
budget = Budget.findOne({ title: budget })
category = Category.findOne({ title: category })
Promise.all([budget, category])
.then(([budget_id, category_id]) => {
const expense = new Expense({
amount,
budget: budget_id,
category: category_id,
description
})
expense.save()
.then(response => res.status(201).json(response))
.catch(err => res.status(500).json(err))
})
.catch(err => res.status(500).json(err))
})By doing the above, we can now send a POST request from Postman that looks like this:
{
"amount": 35,
"description": "potatoes",
"budget": "Monthly Spending",
"category": "Groceries"
}|
Excellent work on the sprint challenge, David! Really dig the copious notes too. Please review each individual comment I left ya, then close this PR. Thanks! |
INITIAL PUSH: Project setup with dependencies, folder structure, basic code. Added margin notes.