From b1e5d61761b188167bc3c4ea0b04a51eb77ad7c4 Mon Sep 17 00:00:00 2001 From: khaled Date: Sat, 26 Aug 2017 16:21:29 +0100 Subject: [PATCH] I have changed the apiController to read from mongoDB and not from the posts files --- controllers/apiController.js | 66 ++++++++++++++++++++++++++--------- controllers/siteController.js | 31 ++++++++-------- data/posts.json | 19 ---------- package.json | 3 +- 4 files changed, 67 insertions(+), 52 deletions(-) delete mode 100644 data/posts.json diff --git a/controllers/apiController.js b/controllers/apiController.js index 8db20fa..fd00546 100644 --- a/controllers/apiController.js +++ b/controllers/apiController.js @@ -2,27 +2,61 @@ const fs = require('fs'); const express = require('express'); const router = express.Router(); const bodyParser = require('body-parser') +const MongoClient = require('mongodb').MongoClient; router.use(bodyParser.json()); +const ObjectID = require('mongodb').ObjectID; -router.get('/posts', (req, res, next) => { - const filePath = __dirname + '/../data/posts.json'; - const callbackFunction = function(error, file) { - if(error) { - return next(error); - } - // we call .toString() to turn the file buffer to a String - const fileData = file.toString(); - // we use JSON.parse to get an object out the String - const postsJson = JSON.parse(fileData); - res.json(postsJson); - }; - fs.readFile(filePath, callbackFunction); + + +router.get('/students', (req, res) => { + const mongoConnection = 'mongodb://localhost:27017/profile'; + MongoClient.connect(mongoConnection, (err, db) => { + const cursor = db.collection('students').find(); + cursor.toArray((error, students) => { + db.close(); + res.json(students); + }); + }); +}); + +router.get('/students/:id', (req, res) => { + const studentId = req.params.id; + const mongoConnection = 'mongodb://localhost:27017/profile'; + MongoClient.connect(mongoConnection, (err, db) => { + const cursor = db.collection('students').find(ObjectID(studentId)); + cursor.toArray((error, students) => { + db.close(); + res.json(students); + }); + }); }); -router.post('/posts', (req, res) => { - console.log(req.body); - res.status(500).send('not implemented'); +router.get('/posts/:id', (req, res,next) => { + //const studentId = req.params.id; + const postId = req.params.id; + const mongoConnection = 'mongodb://localhost:27017/profile'; + MongoClient.connect(mongoConnection, (err, db) => { + const cursor = db.collection('posts').find(ObjectID(postId)); + cursor.toArray((error, posts) => { + db.close(); + res.json(posts); + }); + }); }); +router.get('/posts/', (req, res,next) => { + + const mongoConnection = 'mongodb://localhost:27017/profile'; + MongoClient.connect(mongoConnection, (err, db) => { + const cursor = db.collection('posts').find(); + cursor.toArray((error, posts) => { + db.close(); + res.json(posts); + }); + }); +}); + + + module.exports = router; \ No newline at end of file diff --git a/controllers/siteController.js b/controllers/siteController.js index 32ccd8d..38b0fe7 100644 --- a/controllers/siteController.js +++ b/controllers/siteController.js @@ -1,25 +1,24 @@ const fs = require('fs'); const express = require('express'); const router = express.Router(); +const MongoClient = require('mongodb').MongoClient; router.get('/', function (req, res) { - const filePath = __dirname + '/../data/posts.json'; - const callbackFunction = function(error, file) { - if(error) { - return next(error); - } - // we call .toString() to turn the file buffer to a String - const fileData = file.toString(); - // we use JSON.parse to get an object out the String - const postsJson = JSON.parse(fileData); - // send the json to the Template to render - res.render('index', { - title: "Michael's profile", - subheading: "A modern Website built in Node with Handlebars", - posts: postsJson + // Write code to connect to database and return posts + const mongoConnection = 'mongodb://localhost:27017/profile'; + + MongoClient.connect(mongoConnection, (err, db) => { + const cursor = db.collection('posts').find({}); + cursor.toArray((error, posts) => { + db.close(); + // res.json(posts); + res.render('index', { + title: "Michael's profile", + subheading: "A modern Website built in Node with Handlebars", + posts: posts + }); }); - }; - fs.readFile(filePath, callbackFunction); + }); }); diff --git a/data/posts.json b/data/posts.json deleted file mode 100644 index 3c9847e..0000000 --- a/data/posts.json +++ /dev/null @@ -1,19 +0,0 @@ -[ - { - "title": "My first blog post", - "summary": "This is a summary of my first blog post", - "content": "This is the full content of my first blog post that I published to my own website" - }, { - "title": "My second blog post", - "summary": "This is a summary of my second blog post", - "content": "This is the full content of my second blog post that I published to my own website" - }, { - "title": "My third blog post", - "summary": "This is a summary of my third blog post", - "content": "This is the full content of my third blog post that I published to my own website" - }, { - "title": "My forth blog post", - "summary": "This is a summary of my forth blog post", - "content": "This is the full content of my forth blog post that I published to my own website" - } -] \ No newline at end of file diff --git a/package.json b/package.json index 8fa76b4..ecd7a77 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,8 @@ "dependencies": { "body-parser": "^1.17.2", "express": "^4.15.4", - "express-handlebars": "^3.0.0" + "express-handlebars": "^3.0.0", + "mongodb": "^2.2.31" }, "devDependencies": { "nodemon": "^1.11.0"