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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ We don't need to worry about update or delete.
* Import and use the controller functions in the appropiate Router

# Part 4 - Database
* 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
Expand Down
24 changes: 24 additions & 0 deletions controllers/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let CommentModel = require("../models/comments")

//get - all comments
exports.list = function list(req, res) {
CommentModel.find((e,comments)=>{
return res.json(comments);
});
}


//get - one comment
exports.show = function show(req, res) {
CommentModel.findById(req.params.id, (err,comments)=>{
return res.json(comments);
});
}

//post
exports.create = function create(req, res) {
let newComment = new CommentModel(req.body);
newComment.save(()=>{
return res.json(newComment);
});
}
23 changes: 23 additions & 0 deletions controllers/contacts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
let ContactModel = require("../models/contacts")

//get - all contacts
exports.list = function list(req, res) {
ContactModel.find((e,contacts)=>{
return res.json(contacts);
});
}

//get - one contact
exports.show = function show(req, res) {
ContactModel.findById(req.params.id, (err,contacts)=>{
return res.json(contacts);
});
}

//post
exports.create = function create(req, res) {
let newContact = new ContactModel(req.body);
newContact.save(()=>{
return res.json(newContact);
});
}
23 changes: 23 additions & 0 deletions controllers/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
let ProductModel = require("../models/products");

//get - all products
exports.list = function list(req, res) {
ProductModel.find((e,products)=>{
return res.json(products);
});
}

//get - one product
exports.show = function show(req, res) {
ProductModel.findById(req.params.id, (err,product)=>{
res.json(product)
});
}

//post
exports.create = function create(req, res) {
let newProduct = new ProductModel(req.body);
newProduct.save(()=>{
return res.json(newProduct);
});
}
24 changes: 24 additions & 0 deletions controllers/vehicles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
let VehicleModel = require("../models/vehicles")

//get - all vehicles
exports.list = function list(req, res) {
VehicleModel.find((e,vehicles)=>{
return res.json(vehicles);
});
}

//get - one vehicle
exports.show = function show(req, res) {
VehicleModel.findById(req.params.id, (err,vehicles)=>{
return res.json(vehicles);
});
}

//post
exports.create = function create(req, res) {
let newVehicle = new VehicleModel(req.body);
newVehicle.save(()=>{
return res.json(newVehicle);
});
}

22 changes: 17 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
let express = require("express");
let comments = require("./comments");
let products = require("./products");
let vehicles = require("./vehicles");
let contacts = require("./contacts");
let comments = require("./routes/comments");
let contacts = require("./routes/contacts");
let products = require("./routes/products");
let vehicles = require("./routes/vehicles");

const bodyParser = require("body-parser");
const mongoose = require('mongoose');
const app = express();

app.use(bodyParser.json());
app.use(express.static("public"));
app.use(comments);
app.use(contacts);
app.use(products);
app.use(vehicles);

const thePort = 3001;
mongoose.connect('mongodb+srv://daniela-idara:ebRXVvVywtErhDc9@daniela-idara-nnzoe.mongodb.net/test?retryWrites=true', {useNewUrlParser: true});

let db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("Success!");
});

const thePort = 3001;
app.listen(thePort, (err) => {
if (err) {
return console.log("Error", err);
Expand Down
9 changes: 9 additions & 0 deletions models/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const mongoose = require('mongoose');

let commentSchema = new mongoose.Schema({
body: String
});

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

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

let contactSchema = new mongoose.Schema({
name: String,
occupation: String,
avatar: String
});

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

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

let productSchema = new mongoose.Schema({
name: String,
description: String,
});

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

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

let vehicleSchema = new mongoose.Schema({
year: String,
make: String,
model: String
});

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

module.exports = Vehicle;
Loading