Skip to content

John C — Sprint-Challenge-Mongo#257

Open
frankfaustino wants to merge 1 commit intobloominstituteoftechnology:masterfrom
CSPT1-TEAMS:JohnC
Open

John C — Sprint-Challenge-Mongo#257
frankfaustino wants to merge 1 commit intobloominstituteoftechnology:masterfrom
CSPT1-TEAMS:JohnC

Conversation

@frankfaustino
Copy link

No description provided.

An instance of an element or item in a collection.

# Q2 : Relationships in MongoDB
Relationships can be achieved in mongoDB through a ref type. To populate the document with the reference type you can do an extra db call or use a library(mongoose).
Copy link
Author

Choose a reason for hiding this comment

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

👌

@@ -0,0 +1,14 @@
const router = require('express').Router();
const budgetdb = require('./buget')
Copy link
Author

Choose a reason for hiding this comment

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

Missing some semi-colons ; in this file.

@@ -0,0 +1,12 @@
const Router = require('express').Router()
const Categorydb = require('./category');
Copy link
Author

Choose a reason for hiding this comment

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

We should be using camelCase for variable names.

const obj = req.body;
Categorydb.create(obj,(err,newcategory) => {
if (err) res.status(500).json(err);
res.status(202).json(newcategory);
Copy link
Author

Choose a reason for hiding this comment

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

HTTP status code should be 201, since we're creating a new document and sending it to our client (Postman).

https://httpstatuses.com/201

const mongoose = require('mongoose')
const Schema = mongoose.Schema;

const ExpenseSchema = new Schema({
Copy link
Author

Choose a reason for hiding this comment

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

Not to be too pedantic 😆, but for readability's sake, we should be adding spaces inside curly braces, after commas and colons:

const ExpenseSchema = new Schema({
    amount: {
        type: Number,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    budget: {
        type: Schema.Types.ObjectId,
        ref: 'Budget'
    },
    category: {
        type: Schema.Types.ObjectId,
        ref: 'Category'
    }
}

https://github.com/airbnb/javascript#whitespace--comma-spacing
https://github.com/airbnb/javascript#whitespace--in-braces

const Router = require('express').Router()
const Expenses = require('./expenses')

Router.post('/',(req,res) => {
Copy link
Author

@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.

Instead of sending the budget and category ObjectId along with the POST request, we can send a POST request that looks like this:

{
	"amount": 35,
	"description": "potatoes",
	"budget": "Monthly Spending",
	"category": "Groceries"
}

In order to do that, we need to add some logic to our route:

.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.message))
    })
    .catch(err => res.status(500).json(err.message))
  })

We have to get 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.

@frankfaustino
Copy link
Author

Well done! You are doing a great job, John! 😊 Please review the individual comments I left then close this PR once done.

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