From 89499a60a502b2c1c133e0e9b15cf07b37ae3b1e Mon Sep 17 00:00:00 2001 From: daniela-idara Date: Thu, 25 Jul 2019 00:36:28 -0500 Subject: [PATCH 1/6] gets single item --- index.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 3d47752..dbe4178 100644 --- a/index.js +++ b/index.js @@ -9,9 +9,59 @@ const app = express(); app.use(bodyParser.json()); app.use(express.static("public")); -const thePort = 3001; +app.get('/', function (req, res){ + res.json({name: "Daniela", age:32, color:"purple"}) +}) + +// Create express routes to get all things +// response.json() the appropriate array + +//Create express routes to get one thing +//Add .get() routes for /contacts/:id, /vehicles/:id, /comments/:id, /products/:id +//add a path variable for id +//use the params.id to .find() the item from the appropriate array + +//contacts +app.get('/contacts', (req, res)=>{ + res.json(contacts) +}) +app.get('/contacts/:id', (req, res) => { + let contact = contacts.find(p=>p._id == Number(req.params.id)); + res.json(contact) +}) + +//vehicles +app.get('/vehicles', (req, res)=>{ + res.json(vehicles) +}) + +app.get('/vehicles/:id', (req, res) => { + let vehicle = vehicles.find(p=>p._id === Number(req.params.id)); + res.json(vehicle) +}) +//comments +app.get('/comments', (req, res)=>{ + res.json(comments) +}) +app.get('/comments/:id', (req, res) => { + let comment = comments.find(p=>p._id === Number(req.params.id)); + res.json(comment) +}) + +//products +app.get('/products', (req, res)=>{ + res.json(products) +}) + +app.get('/products/:id', (req, res) => { + let product = products.find(p=>p._id === Number(req.params.id)); + res.json(product) +}) + + +const thePort = 3001; app.listen(thePort, (err) => { if (err) { return console.log("Error", err); From a69ee60904327d0cc34cbe7acf7e76bd8325abb1 Mon Sep 17 00:00:00 2001 From: daniela-idara Date: Thu, 25 Jul 2019 23:16:37 -0500 Subject: [PATCH 2/6] complete part one --- index.js | 54 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index dbe4178..8d8a079 100644 --- a/index.js +++ b/index.js @@ -9,10 +9,6 @@ const app = express(); app.use(bodyParser.json()); app.use(express.static("public")); -app.get('/', function (req, res){ - res.json({name: "Daniela", age:32, color:"purple"}) -}) - // Create express routes to get all things // response.json() the appropriate array @@ -21,6 +17,24 @@ app.get('/', function (req, res){ //add a path variable for id //use the params.id to .find() the item from the appropriate array +//comments +app.get('/comments', (req, res)=>{ + res.json(comments) +}) + +app.get('/comments/:id', (req, res) => { + let comment = comments.find(p=>p._id === Number(req.params.id)); + res.json(comment) +}) + +app.post('/comments/', (req, res) => { + let id = comments.length + 1; + let body = req.body.body; + let postId = req.body.postId; + comments.push({"_id":id, "body":body, "postId":postId}) + res.json(comments); +}) + //contacts app.get('/contacts', (req, res)=>{ res.json(contacts) @@ -30,6 +44,15 @@ app.get('/contacts/:id', (req, res) => { res.json(contact) }) +app.post('/contacts/', (req, res) => { + let id = contacts.length + 1; + let name = req.body.name; + let occupation = req.body.occupation; + let avatar = req.body.avatar; + contacts.push({"_id":id, "name":name, "occupation":occupation, "avatar":avatar}) + res.json(contacts); +}) + //vehicles app.get('/vehicles', (req, res)=>{ res.json(vehicles) @@ -40,14 +63,13 @@ app.get('/vehicles/:id', (req, res) => { res.json(vehicle) }) -//comments -app.get('/comments', (req, res)=>{ - res.json(comments) -}) - -app.get('/comments/:id', (req, res) => { - let comment = comments.find(p=>p._id === Number(req.params.id)); - res.json(comment) +app.post('/vehicles/', (req, res) => { + let id = vehicles.length + 1; + let year = req.body.year; + let make = req.body.make; + let model = req.body.model; + vehicles.push({year, make, model, "_id":id, "Vehicle Year":year, "Vehicle Make":make, "Vehicle Model":model}) + res.json(vehicles); }) //products @@ -60,6 +82,14 @@ app.get('/products/:id', (req, res) => { res.json(product) }) +app.post('/products/', (req, res) => { + let id = products.length + 1; + let name = req.body.name; + let description = req.body.description; + products.push({name, description, "_id":id, "Product Name":name, "Product desc":description}) + res.json(products); +}) + const thePort = 3001; app.listen(thePort, (err) => { From fbaf57e1d41cb0cbc845eac55501bd84c6e7eca6 Mon Sep 17 00:00:00 2001 From: daniela-idara Date: Fri, 26 Jul 2019 19:37:52 -0500 Subject: [PATCH 3/6] part 2 - routes --- index.js | 95 +++++----------------------------------------- routes/comments.js | 24 ++++++++++++ routes/contacts.js | 24 ++++++++++++ routes/products.js | 26 +++++++++++++ routes/vehicles.js | 26 +++++++++++++ 5 files changed, 109 insertions(+), 86 deletions(-) create mode 100644 routes/comments.js create mode 100644 routes/contacts.js create mode 100644 routes/products.js create mode 100644 routes/vehicles.js diff --git a/index.js b/index.js index 8d8a079..b648840 100644 --- a/index.js +++ b/index.js @@ -1,95 +1,18 @@ 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 app = express(); + app.use(bodyParser.json()); app.use(express.static("public")); - -// Create express routes to get all things -// response.json() the appropriate array - -//Create express routes to get one thing -//Add .get() routes for /contacts/:id, /vehicles/:id, /comments/:id, /products/:id -//add a path variable for id -//use the params.id to .find() the item from the appropriate array - -//comments -app.get('/comments', (req, res)=>{ - res.json(comments) -}) - -app.get('/comments/:id', (req, res) => { - let comment = comments.find(p=>p._id === Number(req.params.id)); - res.json(comment) -}) - -app.post('/comments/', (req, res) => { - let id = comments.length + 1; - let body = req.body.body; - let postId = req.body.postId; - comments.push({"_id":id, "body":body, "postId":postId}) - res.json(comments); -}) - -//contacts -app.get('/contacts', (req, res)=>{ - res.json(contacts) -}) -app.get('/contacts/:id', (req, res) => { - let contact = contacts.find(p=>p._id == Number(req.params.id)); - res.json(contact) -}) - -app.post('/contacts/', (req, res) => { - let id = contacts.length + 1; - let name = req.body.name; - let occupation = req.body.occupation; - let avatar = req.body.avatar; - contacts.push({"_id":id, "name":name, "occupation":occupation, "avatar":avatar}) - res.json(contacts); -}) - -//vehicles -app.get('/vehicles', (req, res)=>{ - res.json(vehicles) -}) - -app.get('/vehicles/:id', (req, res) => { - let vehicle = vehicles.find(p=>p._id === Number(req.params.id)); - res.json(vehicle) -}) - -app.post('/vehicles/', (req, res) => { - let id = vehicles.length + 1; - let year = req.body.year; - let make = req.body.make; - let model = req.body.model; - vehicles.push({year, make, model, "_id":id, "Vehicle Year":year, "Vehicle Make":make, "Vehicle Model":model}) - res.json(vehicles); -}) - -//products -app.get('/products', (req, res)=>{ - res.json(products) -}) - -app.get('/products/:id', (req, res) => { - let product = products.find(p=>p._id === Number(req.params.id)); - res.json(product) -}) - -app.post('/products/', (req, res) => { - let id = products.length + 1; - let name = req.body.name; - let description = req.body.description; - products.push({name, description, "_id":id, "Product Name":name, "Product desc":description}) - res.json(products); -}) - +app.use(comments); +app.use(contacts); +app.use(products); +app.use(vehicles); const thePort = 3001; app.listen(thePort, (err) => { diff --git a/routes/comments.js b/routes/comments.js new file mode 100644 index 0000000..f0c0d7d --- /dev/null +++ b/routes/comments.js @@ -0,0 +1,24 @@ +const express = require("express"); +const router = express.Router(); + +let comments = require("../comments") + +//comments +router.get('/comments', (req, res)=>{ + res.json(comments) +}) + +router.get('/comments/:id', (req, res) => { + let comment = comments.find(p=>p._id === Number(req.params.id)); + res.json(comment) +}) + +router.post('/comments/', (req, res) => { + let id = comments.length + 1; + let body = req.body.body; + let postId = req.body.postId; + comments.push({"_id":id, "body":body, "postId":postId}) + res.json(comments); +}) + +module.exports = router; diff --git a/routes/contacts.js b/routes/contacts.js new file mode 100644 index 0000000..3ada9d9 --- /dev/null +++ b/routes/contacts.js @@ -0,0 +1,24 @@ +const express = require("express"); +const router = express.Router(); + +let contacts = require("../contacts") + +//contacts +router.get('/contacts', (req, res)=>{ + res.json(contacts) + }) + router.get('/contacts/:id', (req, res) => { + let contact = contacts.find(p=>p._id == Number(req.params.id)); + res.json(contact) + }) + + router.post('/contacts/', (req, res) => { + let id = contacts.length + 1; + let name = req.body.name; + let occupation = req.body.occupation; + let avatar = req.body.avatar; + contacts.push({"_id":id, "name":name, "occupation":occupation, "avatar":avatar}) + res.json(contacts); + }) + + module.exports = router; \ No newline at end of file diff --git a/routes/products.js b/routes/products.js new file mode 100644 index 0000000..4c0ab34 --- /dev/null +++ b/routes/products.js @@ -0,0 +1,26 @@ +const express = require("express"); +const router = express.Router(); + +let products = require("../products"); + + +//products +router.get('/products', (req, res)=>{ + res.json(products) + }) + + router.get('/products/:id', (req, res) => { + let product = products.find(p=>p._id === Number(req.params.id)); + res.json(product) + }) + + router.post('/products/', (req, res) => { + let id = products.length + 1; + let name = req.body.name; + let description = req.body.description; + products.push({name, description, "_id":id, "Product Name":name, "Product desc":description}) + res.json(products); + }) + + +module.exports = router; diff --git a/routes/vehicles.js b/routes/vehicles.js new file mode 100644 index 0000000..7fbde3b --- /dev/null +++ b/routes/vehicles.js @@ -0,0 +1,26 @@ +const express = require("express"); +const router = express.Router(); + +let vehicles = require("../vehicles"); + +//vehicles +router.get('/vehicles', (req, res)=>{ + res.json(vehicles) + }) + + router.get('/vehicles/:id', (req, res) => { + let vehicle = vehicles.find(p=>p._id === Number(req.params.id)); + res.json(vehicle) + }) + + router.post('/vehicles/', (req, res) => { + let id = vehicles.length + 1; + let year = req.body.year; + let make = req.body.make; + let model = req.body.model; + vehicles.push({year, make, model, "_id":id, "Vehicle Year":year, "Vehicle Make":make, "Vehicle Model":model}) + res.json(vehicles); + }) + + +module.exports = router; From 810149b62b782b2d6de5dfdaa44e64cbab056e66 Mon Sep 17 00:00:00 2001 From: daniela-idara Date: Fri, 26 Jul 2019 20:40:01 -0500 Subject: [PATCH 4/6] part 3 - controllers + routes setup --- controllers/comments.js | 36 ++++++++++++++++++++++++++++++++++++ controllers/contacts.js | 38 ++++++++++++++++++++++++++++++++++++++ controllers/products.js | 36 ++++++++++++++++++++++++++++++++++++ controllers/vehicles.js | 40 ++++++++++++++++++++++++++++++++++++++++ routes/comments.js | 21 ++++----------------- routes/contacts.js | 22 ++++------------------ routes/products.js | 23 ++++------------------- routes/vehicles.js | 22 ++++------------------ 8 files changed, 166 insertions(+), 72 deletions(-) create mode 100644 controllers/comments.js create mode 100644 controllers/contacts.js create mode 100644 controllers/products.js create mode 100644 controllers/vehicles.js diff --git a/controllers/comments.js b/controllers/comments.js new file mode 100644 index 0000000..911cf92 --- /dev/null +++ b/controllers/comments.js @@ -0,0 +1,36 @@ +let comments = require("../comments") + +//get - all comments +exports.list = function list(req, res) { + return res.json(comments); +} + +//get - one comment +exports.show = function list(req, res) { + let comment = comments.find(p=>p._id === Number(req.params.id)); + res.json(comment) +} + +//post +exports.create = function list(req, res) { + let id = comments.length + 1; + let body = req.body.body; + let postId = req.body.postId; + comments.push({"_id":id, "body":body, "postId":postId}) + res.json(comments); +} + +//put +exports.update = function list(req, res) { + let comment = comments.find(p=>p._id === Number(req.params.id)); + comment.body = body.body; + comment.postId = body.postId; + res.json(comment) +} + +//delete +exports.remove = function list(req, res) { + let comment = comments.find(p=>p._id === Number(req.params.id)); + comment.isActive = false; + res.send("deleted"); +} \ No newline at end of file diff --git a/controllers/contacts.js b/controllers/contacts.js new file mode 100644 index 0000000..5ab478d --- /dev/null +++ b/controllers/contacts.js @@ -0,0 +1,38 @@ +let contacts = require("../contacts"); + +//get - all contacts +exports.list = function list(req, res) { + return res.json(contacts); +} + +//get - one contact +exports.show = function list(req, res) { + let contact = contacts.find(p=>p._id === Number(req.params.id)); + res.json(contact) +} + +//post +exports.create = function list(req, res) { + let id = contacts.length + 1; + let name = req.body.name; + let occupation = req.body.occupation; + let avatar = req.body.avatar; + contacts.push({"_id":id, "name":name, "occupation":occupation, "avatar":avatar}) + res.json(contacts); +} + +//put +exports.update = function list(req, res) { + let contact = contacts.find(p=>p._id === Number(req.params.id)); + contact.name = body.name; + contact.occupation = body.occupation; + contact.avatar = body.avatar; + res.json(contact) +} + +//delete +exports.remove = function list(req, res) { + let contact = contacts.find(p=>p._id === Number(req.params.id)); + contact.isActive = false; + res.send("deleted"); +} \ No newline at end of file diff --git a/controllers/products.js b/controllers/products.js new file mode 100644 index 0000000..13f8489 --- /dev/null +++ b/controllers/products.js @@ -0,0 +1,36 @@ +let products = require("../products"); + +//get - all products +exports.list = function list(req, res) { + return res.json(products); +} + +//get - one product +exports.show = function list(req, res) { + let product = products.find(p=>p._id === Number(req.params.id)); + res.json(product) +} + +//post +exports.create = function list(req, res) { + let id = products.length + 1; + let name = req.body.name; + let description = req.body.description; + products.push({name, description, "_id":id, "Product Name":name, "Product desc":description}) + res.json(products); +} + +//put +exports.update = function list(req, res) { + let product = products.find(p=>p._id === Number(req.params.id)); + product.name = body.name; + product.description = body.description; + res.json(product) +} + +//delete +exports.remove = function list(req, res) { + let product = products.find(p=>p._id === Number(req.params.id)); + product.isActive = false; + res.send("deleted"); +} \ No newline at end of file diff --git a/controllers/vehicles.js b/controllers/vehicles.js new file mode 100644 index 0000000..e46da4a --- /dev/null +++ b/controllers/vehicles.js @@ -0,0 +1,40 @@ +let vehicles = require("../vehicles"); + + +//get - all vehicles +exports.list = function list(req, res) { + return res.json(vehicles); +} + +//get - one vehicle +exports.show = function list(req, res) { + let vehicle = vehicles.find(p=>p._id === Number(req.params.id)); + res.json(vehicle) +} + +//post +exports.create = function list(req, res) { + let id = vehicles.length + 1; + let year = req.body.year; + let make = req.body.make; + let model = req.body.model; + vehicles.push({year, make, model, "_id":id, "Vehicle Year":year, "Vehicle Make":make, "Vehicle Model":model}) + res.json(vehicles); +} + +//put +exports.update = function list(req, res) { + let vehicle = vehicles.find(p=>p._id === Number(req.params.id)); + vehicle.year = body.year; + vehicle.make = body.make; + vehicle.model = body.model; + res.json(vehicle) +} + +//delete +exports.remove = function list(req, res) { + let vehicle = vehicles.find(p=>p._id === Number(req.params.id)); + vehicle.isActive = false; + res.send("deleted"); +} + diff --git a/routes/comments.js b/routes/comments.js index f0c0d7d..49b8c9a 100644 --- a/routes/comments.js +++ b/routes/comments.js @@ -1,24 +1,11 @@ const express = require("express"); const router = express.Router(); +const {list,show,create} = require("../controllers/comments"); -let comments = require("../comments") //comments -router.get('/comments', (req, res)=>{ - res.json(comments) -}) - -router.get('/comments/:id', (req, res) => { - let comment = comments.find(p=>p._id === Number(req.params.id)); - res.json(comment) -}) - -router.post('/comments/', (req, res) => { - let id = comments.length + 1; - let body = req.body.body; - let postId = req.body.postId; - comments.push({"_id":id, "body":body, "postId":postId}) - res.json(comments); -}) +router.get('/comments', list); +router.get('/comments/:id', show); +router.post('/comments/', create); module.exports = router; diff --git a/routes/contacts.js b/routes/contacts.js index 3ada9d9..759c57c 100644 --- a/routes/contacts.js +++ b/routes/contacts.js @@ -1,24 +1,10 @@ const express = require("express"); const router = express.Router(); - -let contacts = require("../contacts") +const {list,show,create} = require("../controllers/contacts"); //contacts -router.get('/contacts', (req, res)=>{ - res.json(contacts) - }) - router.get('/contacts/:id', (req, res) => { - let contact = contacts.find(p=>p._id == Number(req.params.id)); - res.json(contact) - }) - - router.post('/contacts/', (req, res) => { - let id = contacts.length + 1; - let name = req.body.name; - let occupation = req.body.occupation; - let avatar = req.body.avatar; - contacts.push({"_id":id, "name":name, "occupation":occupation, "avatar":avatar}) - res.json(contacts); - }) +router.get('/contacts', list); +router.get('/contacts/:id', show); +router.post('/contacts/', create); module.exports = router; \ No newline at end of file diff --git a/routes/products.js b/routes/products.js index 4c0ab34..9055fd7 100644 --- a/routes/products.js +++ b/routes/products.js @@ -1,26 +1,11 @@ const express = require("express"); const router = express.Router(); - -let products = require("../products"); - +const {list,show,create} = require("../controllers/products"); //products -router.get('/products', (req, res)=>{ - res.json(products) - }) - - router.get('/products/:id', (req, res) => { - let product = products.find(p=>p._id === Number(req.params.id)); - res.json(product) - }) - - router.post('/products/', (req, res) => { - let id = products.length + 1; - let name = req.body.name; - let description = req.body.description; - products.push({name, description, "_id":id, "Product Name":name, "Product desc":description}) - res.json(products); - }) +router.get('/products', list); +router.get('/products/:id', show); +router.post('/products/', create); module.exports = router; diff --git a/routes/vehicles.js b/routes/vehicles.js index 7fbde3b..c459291 100644 --- a/routes/vehicles.js +++ b/routes/vehicles.js @@ -1,26 +1,12 @@ const express = require("express"); const router = express.Router(); +const {list,show,create} = require("../controllers/vehicles"); -let vehicles = require("../vehicles"); //vehicles -router.get('/vehicles', (req, res)=>{ - res.json(vehicles) - }) - - router.get('/vehicles/:id', (req, res) => { - let vehicle = vehicles.find(p=>p._id === Number(req.params.id)); - res.json(vehicle) - }) - - router.post('/vehicles/', (req, res) => { - let id = vehicles.length + 1; - let year = req.body.year; - let make = req.body.make; - let model = req.body.model; - vehicles.push({year, make, model, "_id":id, "Vehicle Year":year, "Vehicle Make":make, "Vehicle Model":model}) - res.json(vehicles); - }) +router.get('/vehicles', list); +router.get('/vehicles/:id', show); +router.post('/vehicles/', create); module.exports = router; From 35b410f74c546421d31cd6c1c0f52ec3f69c5f33 Mon Sep 17 00:00:00 2001 From: daniela-idara Date: Sun, 28 Jul 2019 12:14:54 -0500 Subject: [PATCH 5/6] update README with part 4 --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index b26b556..7655487 100644 --- a/README.md +++ b/README.md @@ -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 From d6f2e43ce0ba02d33ba64468ac72985fd3e3c41b Mon Sep 17 00:00:00 2001 From: daniela-idara Date: Wed, 7 Aug 2019 18:08:38 -0500 Subject: [PATCH 6/6] mongoose works --- controllers/comments.js | 46 +++++------ controllers/contacts.js | 41 ++++------ controllers/products.js | 37 +++------ controllers/vehicles.js | 42 ++++------ index.js | 9 +++ models/comments.js | 9 +++ models/contacts.js | 11 +++ models/products.js | 10 +++ models/vehicles.js | 11 +++ package-lock.json | 165 ++++++++++++++++++++++++++++++++++++++++ package.json | 3 +- 11 files changed, 272 insertions(+), 112 deletions(-) create mode 100644 models/comments.js create mode 100644 models/contacts.js create mode 100644 models/products.js create mode 100644 models/vehicles.js diff --git a/controllers/comments.js b/controllers/comments.js index 911cf92..63504ed 100644 --- a/controllers/comments.js +++ b/controllers/comments.js @@ -1,36 +1,24 @@ -let comments = require("../comments") +let CommentModel = require("../models/comments") //get - all comments -exports.list = function list(req, res) { - return res.json(comments); -} +exports.list = function list(req, res) { + CommentModel.find((e,comments)=>{ + return res.json(comments); + }); + } + //get - one comment -exports.show = function list(req, res) { - let comment = comments.find(p=>p._id === Number(req.params.id)); - res.json(comment) +exports.show = function show(req, res) { + CommentModel.findById(req.params.id, (err,comments)=>{ + return res.json(comments); + }); } //post -exports.create = function list(req, res) { - let id = comments.length + 1; - let body = req.body.body; - let postId = req.body.postId; - comments.push({"_id":id, "body":body, "postId":postId}) - res.json(comments); -} - -//put -exports.update = function list(req, res) { - let comment = comments.find(p=>p._id === Number(req.params.id)); - comment.body = body.body; - comment.postId = body.postId; - res.json(comment) -} - -//delete -exports.remove = function list(req, res) { - let comment = comments.find(p=>p._id === Number(req.params.id)); - comment.isActive = false; - res.send("deleted"); -} \ No newline at end of file +exports.create = function create(req, res) { + let newComment = new CommentModel(req.body); + newComment.save(()=>{ + return res.json(newComment); + }); + } \ No newline at end of file diff --git a/controllers/contacts.js b/controllers/contacts.js index 5ab478d..f6636a0 100644 --- a/controllers/contacts.js +++ b/controllers/contacts.js @@ -1,38 +1,23 @@ -let contacts = require("../contacts"); +let ContactModel = require("../models/contacts") //get - all contacts exports.list = function list(req, res) { - return res.json(contacts); + ContactModel.find((e,contacts)=>{ + return res.json(contacts); + }); } //get - one contact -exports.show = function list(req, res) { - let contact = contacts.find(p=>p._id === Number(req.params.id)); - res.json(contact) +exports.show = function show(req, res) { + ContactModel.findById(req.params.id, (err,contacts)=>{ + return res.json(contacts); + }); } //post -exports.create = function list(req, res) { - let id = contacts.length + 1; - let name = req.body.name; - let occupation = req.body.occupation; - let avatar = req.body.avatar; - contacts.push({"_id":id, "name":name, "occupation":occupation, "avatar":avatar}) - res.json(contacts); +exports.create = function create(req, res) { + let newContact = new ContactModel(req.body); + newContact.save(()=>{ + return res.json(newContact); + }); } - -//put -exports.update = function list(req, res) { - let contact = contacts.find(p=>p._id === Number(req.params.id)); - contact.name = body.name; - contact.occupation = body.occupation; - contact.avatar = body.avatar; - res.json(contact) -} - -//delete -exports.remove = function list(req, res) { - let contact = contacts.find(p=>p._id === Number(req.params.id)); - contact.isActive = false; - res.send("deleted"); -} \ No newline at end of file diff --git a/controllers/products.js b/controllers/products.js index 13f8489..3e964d7 100644 --- a/controllers/products.js +++ b/controllers/products.js @@ -1,36 +1,23 @@ -let products = require("../products"); +let ProductModel = require("../models/products"); //get - all products exports.list = function list(req, res) { - return res.json(products); + ProductModel.find((e,products)=>{ + return res.json(products); + }); } //get - one product -exports.show = function list(req, res) { - let product = products.find(p=>p._id === Number(req.params.id)); +exports.show = function show(req, res) { + ProductModel.findById(req.params.id, (err,product)=>{ res.json(product) +}); } //post -exports.create = function list(req, res) { - let id = products.length + 1; - let name = req.body.name; - let description = req.body.description; - products.push({name, description, "_id":id, "Product Name":name, "Product desc":description}) - res.json(products); +exports.create = function create(req, res) { + let newProduct = new ProductModel(req.body); + newProduct.save(()=>{ + return res.json(newProduct); + }); } - -//put -exports.update = function list(req, res) { - let product = products.find(p=>p._id === Number(req.params.id)); - product.name = body.name; - product.description = body.description; - res.json(product) -} - -//delete -exports.remove = function list(req, res) { - let product = products.find(p=>p._id === Number(req.params.id)); - product.isActive = false; - res.send("deleted"); -} \ No newline at end of file diff --git a/controllers/vehicles.js b/controllers/vehicles.js index e46da4a..8af42f9 100644 --- a/controllers/vehicles.js +++ b/controllers/vehicles.js @@ -1,40 +1,24 @@ -let vehicles = require("../vehicles"); - +let VehicleModel = require("../models/vehicles") //get - all vehicles exports.list = function list(req, res) { - return res.json(vehicles); + VehicleModel.find((e,vehicles)=>{ + return res.json(vehicles); + }); } //get - one vehicle -exports.show = function list(req, res) { - let vehicle = vehicles.find(p=>p._id === Number(req.params.id)); - res.json(vehicle) +exports.show = function show(req, res) { + VehicleModel.findById(req.params.id, (err,vehicles)=>{ + return res.json(vehicles); + }); } //post -exports.create = function list(req, res) { - let id = vehicles.length + 1; - let year = req.body.year; - let make = req.body.make; - let model = req.body.model; - vehicles.push({year, make, model, "_id":id, "Vehicle Year":year, "Vehicle Make":make, "Vehicle Model":model}) - res.json(vehicles); -} - -//put -exports.update = function list(req, res) { - let vehicle = vehicles.find(p=>p._id === Number(req.params.id)); - vehicle.year = body.year; - vehicle.make = body.make; - vehicle.model = body.model; - res.json(vehicle) -} - -//delete -exports.remove = function list(req, res) { - let vehicle = vehicles.find(p=>p._id === Number(req.params.id)); - vehicle.isActive = false; - res.send("deleted"); +exports.create = function create(req, res) { + let newVehicle = new VehicleModel(req.body); + newVehicle.save(()=>{ + return res.json(newVehicle); + }); } diff --git a/index.js b/index.js index b648840..9aad1f3 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,7 @@ 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()); @@ -14,6 +15,14 @@ app.use(contacts); app.use(products); app.use(vehicles); +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) { diff --git a/models/comments.js b/models/comments.js new file mode 100644 index 0000000..389d4c3 --- /dev/null +++ b/models/comments.js @@ -0,0 +1,9 @@ +const mongoose = require('mongoose'); + +let commentSchema = new mongoose.Schema({ + body: String +}); + +let Comment = mongoose.model('Comment', commentSchema); + +module.exports = Comment; \ No newline at end of file diff --git a/models/contacts.js b/models/contacts.js new file mode 100644 index 0000000..87021e2 --- /dev/null +++ b/models/contacts.js @@ -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; \ No newline at end of file diff --git a/models/products.js b/models/products.js new file mode 100644 index 0000000..66b9eee --- /dev/null +++ b/models/products.js @@ -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; diff --git a/models/vehicles.js b/models/vehicles.js new file mode 100644 index 0000000..61fdd96 --- /dev/null +++ b/models/vehicles.js @@ -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; \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index a533283..4a2e57e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,19 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "async": { + "version": "2.6.2", + "resolved": "https://registry.npmjs.org/async/-/async-2.6.2.tgz", + "integrity": "sha512-H1qVYh1MYhEEFLsP97cVKqCGo7KfCyTt6uEWqsTBr9SO84oK9Uwbyd/yCW+6rKJLHksBNUVWZDAjfS+Ccx0Bbg==", + "requires": { + "lodash": "^4.17.11" + } + }, + "bluebird": { + "version": "3.5.1", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", + "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==" + }, "body-parser": { "version": "1.18.3", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.3.tgz", @@ -35,6 +48,11 @@ "type-is": "~1.6.16" } }, + "bson": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/bson/-/bson-1.1.1.tgz", + "integrity": "sha512-jCGVYLoYMHDkOsbwJZBCqwMHyH4c+wzgI9hG7Z6SZJRXWr+x58pdIbm2i9a/jFGCkRJqRUr8eoI7lDWa0hTkxg==" + }, "bytes": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", @@ -202,11 +220,27 @@ "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.8.0.tgz", "integrity": "sha1-6qM9bd16zo9/b+DJygRA5wZzix4=" }, + "kareem": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/kareem/-/kareem-2.3.0.tgz", + "integrity": "sha512-6hHxsp9e6zQU8nXsP+02HGWXwTkOEw6IROhF2ZA28cYbUk4eJ6QbtZvdqZOdD9YPKghG3apk5eOCvs+tLl3lRg==" + }, + "lodash": { + "version": "4.17.15", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz", + "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==" + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" }, + "memory-pager": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/memory-pager/-/memory-pager-1.5.0.tgz", + "integrity": "sha512-ZS4Bp4r/Zoeq6+NLJpP+0Zzm0pR8whtGPf1XExKLJBAczGMnSi3It14OiNCStjQjM6NU1okjQGSxgEZN8eBYKg==", + "optional": true + }, "merge-descriptors": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", @@ -235,6 +269,85 @@ "mime-db": "~1.37.0" } }, + "mongodb": { + "version": "3.2.7", + "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.2.7.tgz", + "integrity": "sha512-2YdWrdf1PJgxcCrT1tWoL6nHuk6hCxhddAAaEh8QJL231ci4+P9FLyqopbTm2Z2sAU6mhCri+wd9r1hOcHdoMw==", + "requires": { + "mongodb-core": "3.2.7", + "safe-buffer": "^5.1.2" + } + }, + "mongodb-core": { + "version": "3.2.7", + "resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.2.7.tgz", + "integrity": "sha512-WypKdLxFNPOH/Jy6i9z47IjG2wIldA54iDZBmHMINcgKOUcWJh8og+Wix76oGd7EyYkHJKssQ2FAOw5Su/n4XQ==", + "requires": { + "bson": "^1.1.1", + "require_optional": "^1.0.1", + "safe-buffer": "^5.1.2", + "saslprep": "^1.0.0" + } + }, + "mongoose": { + "version": "5.6.8", + "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.6.8.tgz", + "integrity": "sha512-BhgGU/KvnVX8WbamcWgtG/45rp+xZnaF9MhNbzESIIYxK7g5QurXYcaGGCm/JFiIdIxkVUgBycWG7UzRUEzvDg==", + "requires": { + "async": "2.6.2", + "bson": "~1.1.1", + "kareem": "2.3.0", + "mongodb": "3.2.7", + "mongodb-core": "3.2.7", + "mongoose-legacy-pluralize": "1.0.2", + "mpath": "0.6.0", + "mquery": "3.2.1", + "ms": "2.1.2", + "regexp-clone": "1.0.0", + "safe-buffer": "5.1.2", + "sift": "7.0.1", + "sliced": "1.0.1" + }, + "dependencies": { + "ms": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" + } + } + }, + "mongoose-legacy-pluralize": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz", + "integrity": "sha512-Yo/7qQU4/EyIS8YDFSeenIvXxZN+ld7YdV9LqFVQJzTLye8unujAWPZ4NWKfFA+RNjh+wvTWKY9Z3E5XM6ZZiQ==" + }, + "mpath": { + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.6.0.tgz", + "integrity": "sha512-i75qh79MJ5Xo/sbhxrDrPSEG0H/mr1kcZXJ8dH6URU5jD/knFxCVqVC/gVSW7GIXL/9hHWlT9haLbCXWOll3qw==" + }, + "mquery": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.1.tgz", + "integrity": "sha512-kY/K8QToZWTTocm0U+r8rqcJCp5PRl6e8tPmoDs5OeSO3DInZE2rAL6AYH+V406JTo8305LdASOQcxRDqHojyw==", + "requires": { + "bluebird": "3.5.1", + "debug": "3.1.0", + "regexp-clone": "^1.0.0", + "safe-buffer": "5.1.2", + "sliced": "1.0.1" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", @@ -293,6 +406,25 @@ "unpipe": "1.0.0" } }, + "regexp-clone": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/regexp-clone/-/regexp-clone-1.0.0.tgz", + "integrity": "sha512-TuAasHQNamyyJ2hb97IuBEif4qBHGjPHBS64sZwytpLEqtBQ1gPJTnOaQ6qmpET16cK14kkjbazl6+p0RRv0yw==" + }, + "require_optional": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", + "integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==", + "requires": { + "resolve-from": "^2.0.0", + "semver": "^5.1.0" + } + }, + "resolve-from": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz", + "integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=" + }, "safe-buffer": { "version": "5.1.2", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", @@ -303,6 +435,20 @@ "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" }, + "saslprep": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.3.tgz", + "integrity": "sha512-/MY/PEMbk2SuY5sScONwhUDsV2p77Znkb/q3nSVstq/yQzYJOH/Azh29p9oJLsl3LnQwSvZDKagDGBsBwSooag==", + "optional": true, + "requires": { + "sparse-bitfield": "^3.0.3" + } + }, + "semver": { + "version": "5.7.0", + "resolved": "https://registry.npmjs.org/semver/-/semver-5.7.0.tgz", + "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==" + }, "send": { "version": "0.16.2", "resolved": "https://registry.npmjs.org/send/-/send-0.16.2.tgz", @@ -346,6 +492,25 @@ "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.0.tgz", "integrity": "sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ==" }, + "sift": { + "version": "7.0.1", + "resolved": "https://registry.npmjs.org/sift/-/sift-7.0.1.tgz", + "integrity": "sha512-oqD7PMJ+uO6jV9EQCl0LrRw1OwsiPsiFQR5AR30heR+4Dl7jBBbDLnNvWiak20tzZlSE1H7RB30SX/1j/YYT7g==" + }, + "sliced": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/sliced/-/sliced-1.0.1.tgz", + "integrity": "sha1-CzpmK10Ewxd7GSa+qCsD+Dei70E=" + }, + "sparse-bitfield": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/sparse-bitfield/-/sparse-bitfield-3.0.3.tgz", + "integrity": "sha1-/0rm5oZWBWuks+eSqzM004JzyhE=", + "optional": true, + "requires": { + "memory-pager": "^1.0.2" + } + }, "statuses": { "version": "1.5.0", "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", diff --git a/package.json b/package.json index d82072f..302d7bd 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,7 @@ "devDependencies": {}, "dependencies": { "body-parser": "*", - "express": "*" + "express": "*", + "mongoose": "*" } }