Skip to content

Project setup with dependencies, folder structure, basic code.#252

Open
d2rd wants to merge 9 commits intobloominstituteoftechnology:masterfrom
CSPT1-TEAMS:Glenn-David_Sprint-Challenge-Mongo
Open

Project setup with dependencies, folder structure, basic code.#252
d2rd wants to merge 9 commits intobloominstituteoftechnology:masterfrom
CSPT1-TEAMS:Glenn-David_Sprint-Challenge-Mongo

Conversation

@d2rd
Copy link

@d2rd d2rd commented Jun 9, 2018

INITIAL PUSH: Project setup with dependencies, folder structure, basic code. Added margin notes.

// res.status(500).json({ errorMessage: 'Error geting budgets', err })
// })
// })
.post('/budget', (req, res) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) => {
Copy link

@frankfaustino frankfaustino Jun 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"
}

@frankfaustino
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants