Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ jspm_packages

# Optional REPL history
.node_repl_history

# dot env
.env
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,24 @@ We don't need to worry about update or delete.
* Import and use the controller functions in the appropiate Router

# Part 4 - Database
* In server/index.js, import and use mongoose. Connect to a database "advanced-express-practice"
* Create the mongoose models for Contact, Vehicle, Comment Product
* https://github.com/AustinCodingAcademy/express-mongodb
* Create a database somewhere for advanced-express-practice
* Create a new file database.js, implement the code for MongoClient and connect()
* Implement the mongodb client tool into your controllers for list, show, create
* Use insertMany() for create and find() for list and show
* How are your controllers going to get access to const db = client.db("advanced-express-practice");

# Part 5 - Mongoose

* In server/index.js, import and use mongoose. Connect to a database "advanced-express-practice"
* Create the mongoose models for * * Contact, Vehicle, Comment Product
* CommentModel - body
* ContactModel - name, occupation, avatar
* VehicleModel - year, make, model
* ProductModel - name, description
* Change the code in the controllers to use the Models instead of hard coded arrays


### Points
* Base - 10pts
* Data shows in the page for the Lists - 5pts each (20pts)
Expand Down
20 changes: 20 additions & 0 deletions controllers/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let Comment = require("../models/commentModel")

exports.list = function list(req, res) {
Comment.find((err,c)=>{
return res.json(c);
});
}
exports.show = function show(req, res) {
Comment.findById(req.params.id, (err,c)=>{
return res.json(c);
});
}
exports.create = function create(req, res) {
const newComment = new Comment({
body: req.body.body
})
newComment.save().then(savedComment=>{
console.log(savedComment)
})
}
22 changes: 22 additions & 0 deletions controllers/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
let Contact = require("../models/contactModel")

exports.list = function list(req, res) {
Contact.find((err,c)=>{
return res.json(c);
});
}
exports.show = function show(req, res) {
Contact.findById(req.params.id, (err,c)=>{
return res.json(c);
});
}
exports.create = function create(req, res) {
const newContact = new Contact({
name: req.body.name,
occupation: req.body.occupation,
avatar: req.body.avatar
})
newContact.save().then(savedContact=>{
console.log(savedContact)
})
}
21 changes: 21 additions & 0 deletions controllers/product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let Product = require("../models/productModel")

exports.list = function list(req, res) {
Product.find((err,p)=>{
return res.json(p);
});
}
exports.show = function show(req, res) {
Product.findById(req.params.id, (err,p)=>{
return res.json(p);
});
}
exports.create = function create(req, res) {
const newProduct = new Product({
name: req.body.name,
description: req.body.description
})
newProduct.save().then(savedProduct=>{
console.log(savedProduct)
})
}
25 changes: 25 additions & 0 deletions controllers/vehicle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
let Vehicle = require("../models/vehicleModel.js")

exports.list = function list(req, res) {
Vehicle.find((err,v)=>{
if(err){
console.log(err);
}
return res.json(v);
});
}
exports.show = function show(req, res) {
Vehicle.findById(req.params.id, (err,v)=>{
return res.json(v);
});
}
exports.create = function create(req, res) {
const newVehicle = new Vehicle({
year: req.body.year,
make: req.body.make,
model: req.body.model
})
newVehicle.save().then(savedVehicle=>{
console.log(savedVehicle)
})
}
26 changes: 26 additions & 0 deletions database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const MongoClient = require('mongodb').MongoClient;
const assert = require('assert');
const dotenv = require('dotenv').config();
let db = null;



// change this to your mongodb atlas uri
const url = `mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@aca-practice-bx4sb.mongodb.net/test?retryWrites=true&w=majority`;
const client = new MongoClient(url);
const dbName = 'advanced-express-practice';

let areYouConnected = (err) => {
if (err) {
console.log(err);
return
}
console.log('Connected successfully to server')
}

// Use connect method to connect to the server
client.connect(areYouConnected, { useNewUrlParser: true })

exports.getDatabase = function() {
return db;
}
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
let express = require("express");
let comments = require("./comments");
let products = require("./products");
let vehicles = require("./vehicles");
let contacts = require("./contacts");
const mongoose = require('mongoose');
const assert = require('assert');
const dotenv = require('dotenv').config();
let commentRoutes = require('./routes/comment');
let productRoutes = require('./routes/product');
let contactRoutes = require('./routes/contact');
let vehicleRoutes = require('./routes/vehicle');

mongoose.connect(`mongodb+srv://${process.env.DB_USER}:${process.env.DB_PASSWORD}@aca-practice-bx4sb.mongodb.net/test?retryWrites=true&w=majority`, {useNewUrlParser: true});

const bodyParser = require("body-parser");
const app = express();
Expand All @@ -11,6 +16,10 @@ app.use(express.static("public"));

const thePort = 3001;

app.use(vehicleRoutes);
app.use(commentRoutes);
app.use(productRoutes);
app.use(contactRoutes);

app.listen(thePort, (err) => {
if (err) {
Expand Down
10 changes: 10 additions & 0 deletions models/commentModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const commentSchema = new Schema ({
body: String
})

let Comment = mongoose.model('Comment', commentSchema);

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

const contactSchema = new Schema ({
name: String,
occupation: String,
avatar: String
})

let Contact = mongoose.model('Contact', contactSchema);

module.exports = Contact;
11 changes: 11 additions & 0 deletions models/productModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let productsSchema = new Schema({
name: String,
description: String
});

let Product = mongoose.model('Product', productsSchema);

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

const vehicleSchema = new Schema ({
year: Number,
make: String,
model: String
})

let Vehicle = mongoose.model('Vehicle', vehicleSchema);

module.exports = Vehicle;
Loading