Skip to content
Open

april #265

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Answers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Describe the following: `DataBase`, `Collection` , `Document`
1.n MongoDB, databases hold collections of documents.
2.MongoDB stores documents in collections. Collections are analogous to tables in relational da
tabases.
3.MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents,

1. Describe how one can achieve the pattern of _relationships_ in MongoDB. What
needs to take place at the schema level? How do we _'fill'_ in the
appropriate relational data using mongoose?
1.You achieve it by having one-to-many relationships with document refrences.
2.schema lays out how data is stored
3. we need to populate

1. Explain a way to break up an API into Sub-Applications, which tool did we use to do that? We used Express Router
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

## Step 1 - Modeling our Data _hint_: **Three different models, three different files**

* For this project you'll need three different models, budget, Expense and
* [x]For this project you'll need three different models, budget, Expense and
Category.

### **Budget**
Expand Down
16 changes: 16 additions & 0 deletions budget/CategoryController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const router = require( "express" ).Router();
const Budget = require( "./CategoryModel" );













module.exports = router;
12 changes: 12 additions & 0 deletions budget/CategoryModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const mongoose = require( 'mongoose' );
const ObjectId = mongoose.Schema.Types.ObjectId;

const CategoryModel = mongoose.Schema( {
// _id: ObjectId( '543d2c72gsb23cd657438921' ),
title: {
type: String,
unique: true,
require: true,
}
});
module.exports = mongoose.model( 'CategoryModel', CategoryModel );
12 changes: 12 additions & 0 deletions budget/ExpenseController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const router = require( "express" ).Router();
const Budget = require( "./ExpenseModel" );









module.exports = mongoose.model( 'Expense', Expense );
33 changes: 33 additions & 0 deletions budget/ExpenseModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const mongoose = require( 'mongoose' );
const ObjectId = mongoose.Schema.Types.ObjectId;


const ExpenseSchema = mongoose.Schema( {
// _id: ObjectId( '503c2b66bcf86cs793443564' ),
// amount: 35,
// description: 'potatoes',
// budget: ObjectId( '507f1f77bcf86cd799439011' ), // Monthly Spending
// category: ObjectId( '543d2c72gsb23cd657438921' ) // Groceries
amount: {
type: Number,
unique: true,
require: true,
},
description: {
type: String,
unique: true,
require: true,
},
budget: {
type: ObjectId,
ref: 'Budget',
required: true
},
category: {
type: ObjectId,
ref: 'Category',
required: true
}
} );

module.exports = mongoose.model( 'ExpenseModel', ExpenseModel);
62 changes: 62 additions & 0 deletions budget/budgetController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const router = require( "express" ).Router();
const Budget = require( "./budgetModel" );



const get = ( req, res ) =>
{
Budget
.find()
.then( budget =>
{
res.json( { budget } )
} )
.catch( err =>
{
sendUserError( 500, "There was an error in retrieving budgets", res, err );
} );
};
const post = ( req, res ) =>
{
const { title, budgetAmount } = req.body;
const budget = new Budget( { title, budgetAmount } );
budget
.save()
.then( budget =>
{
res.status( 201 ).json( { budget } );
} )
.catch( err =>
{
sendUserError( 500, "There was an error in saving budget to database", res, err );
} );
};

const getId = ( req, res ) =>
{
const { id } = req.params;

Budget
.findById( id )
.then( budget =>
{
res.json( { budget } );
} )
.catch( err =>
{
sendUserError( 500, `There was an error in retrieving budget ${ id } from database`, res, err )
} );
};


router
.route( "/" )
.get( get )
.post( post );

router
.route( "/:id" )
.get( getId );


module.exports = router;
18 changes: 18 additions & 0 deletions budget/budgetModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const mongoose = require( 'mongoose' );
const ObjectId = mongoose.Schema.Types.ObjectId;

const Budget = mongoose.Schema({
//_id: ObjectId( '507f1f77bcf86cd799439011' ),

title: {
type: String,
unique: true,
require: true,
},
budgetAmount: {
type: Number,
default: 0,
}
} );

module.exports = mongoose.model('budgetModel', Budget)
Loading