From 774d56b757f549514d090356df9fd3f934fbe35e Mon Sep 17 00:00:00 2001 From: YJohn Date: Sat, 26 Aug 2017 16:19:10 +0100 Subject: [PATCH 01/11] delete the JSON file and connect with Mongodb --- controllers/apiController.js | 80 +++++++++++++++++++++++++++++------ controllers/siteController.js | 33 +++++++-------- data/posts.json | 19 --------- package.json | 3 +- 4 files changed, 85 insertions(+), 50 deletions(-) delete mode 100644 data/posts.json diff --git a/controllers/apiController.js b/controllers/apiController.js index 8db20fa..dee282d 100644 --- a/controllers/apiController.js +++ b/controllers/apiController.js @@ -2,24 +2,78 @@ const fs = require('fs'); const express = require('express'); const router = express.Router(); const bodyParser = require('body-parser') - +const MongoClient = require('mongodb').MongoClient; +const ObjectID = require('mongodb').ObjectId; router.use(bodyParser.json()); +const getStudents = (req, res)=>{ + +} + +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.get('/students/country/:id', (req, res, next) => { + + res.json(students); +}); + 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); + const mongoConnection = 'mongodb://localhost:27017/profile'; + + MongoClient.connect(mongoConnection, (err, db) => { + const cursor = db.collection('post').find({}); + cursor.toArray((error, posts) => { + db.close(); + res.json(posts); + }); + }); +}); + + +router.get('/posts/:id', (req, res,next) => { + const postId = req.params.id; + const mongoConnection = 'mongodb://localhost:27017/profile'; + + MongoClient.connect(mongoConnection, (err, db) => { + const cursor = db.collection('post').find(ObjectID(postId)); + cursor.toArray((error, posts) => { + db.close(); + res.json(posts); + }); + }); }); + + + + + + + + + router.post('/posts', (req, res) => { console.log(req.body); res.status(500).send('not implemented'); diff --git a/controllers/siteController.js b/controllers/siteController.js index 32ccd8d..e597652 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('post').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" From d91b815b4881f188b0734aa43f0782cd2f5f1d2d Mon Sep 17 00:00:00 2001 From: YJohn Date: Sat, 26 Aug 2017 16:41:43 +0100 Subject: [PATCH 02/11] create a heroku server --- controllers/siteController.js | 1 + 1 file changed, 1 insertion(+) diff --git a/controllers/siteController.js b/controllers/siteController.js index e597652..80c4a02 100644 --- a/controllers/siteController.js +++ b/controllers/siteController.js @@ -2,6 +2,7 @@ const fs = require('fs'); const express = require('express'); const router = express.Router(); const MongoClient = require('mongodb').MongoClient; +const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/profile'; router.get('/', function (req, res) { // Write code to connect to database and return posts From ffcb178662c7894c30257cb7f47845fb7c67c979 Mon Sep 17 00:00:00 2001 From: YJohn Date: Sat, 26 Aug 2017 16:44:40 +0100 Subject: [PATCH 03/11] remove mongoconnection from default get --- controllers/siteController.js | 1 - 1 file changed, 1 deletion(-) diff --git a/controllers/siteController.js b/controllers/siteController.js index 80c4a02..cc6a862 100644 --- a/controllers/siteController.js +++ b/controllers/siteController.js @@ -6,7 +6,6 @@ const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/pr router.get('/', function (req, res) { // Write code to connect to database and return posts - const mongoConnection = 'mongodb://localhost:27017/profile'; MongoClient.connect(mongoConnection, (err, db) => { const cursor = db.collection('post').find({}); From f4192b95ff5c5aa48dc132a57f0b55d60e13fbff Mon Sep 17 00:00:00 2001 From: YJohn Date: Sat, 26 Aug 2017 16:58:36 +0100 Subject: [PATCH 04/11] update mongo collection --- controllers/siteController.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/controllers/siteController.js b/controllers/siteController.js index cc6a862..a05bf4c 100644 --- a/controllers/siteController.js +++ b/controllers/siteController.js @@ -6,9 +6,9 @@ const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/pr router.get('/', function (req, res) { // Write code to connect to database and return posts - + console.log(mongoConnection); MongoClient.connect(mongoConnection, (err, db) => { - const cursor = db.collection('post').find({}); + const cursor = db.collection('posts').find({}); cursor.toArray((error, posts) => { db.close(); // res.json(posts); From 97a1c5d7edb506a81cf5791474f53aaf2980020c Mon Sep 17 00:00:00 2001 From: YJohn Date: Sat, 26 Aug 2017 17:04:41 +0100 Subject: [PATCH 05/11] point the mongodb to another database connection --- controllers/apiController.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/controllers/apiController.js b/controllers/apiController.js index dee282d..36b3465 100644 --- a/controllers/apiController.js +++ b/controllers/apiController.js @@ -5,15 +5,13 @@ const bodyParser = require('body-parser') const MongoClient = require('mongodb').MongoClient; const ObjectID = require('mongodb').ObjectId; router.use(bodyParser.json()); - +const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/profile'; const getStudents = (req, res)=>{ } router.get('/students', (req, res) => { - const mongoConnection = 'mongodb://localhost:27017/profile'; - - MongoClient.connect(mongoConnection, (err, db) => { + MongoClient.connect(mongoConnection, (err, db) => { const cursor = db.collection('students').find({}); cursor.toArray((error, students) => { db.close(); @@ -23,8 +21,6 @@ router.get('/students', (req, res) => { }); 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) => { @@ -40,8 +36,6 @@ router.get('/students/country/:id', (req, res, next) => { }); router.get('/posts', (req, res, next) => { - const mongoConnection = 'mongodb://localhost:27017/profile'; - MongoClient.connect(mongoConnection, (err, db) => { const cursor = db.collection('post').find({}); cursor.toArray((error, posts) => { @@ -54,8 +48,6 @@ router.get('/posts', (req, res, next) => { router.get('/posts/:id', (req, res,next) => { const postId = req.params.id; - const mongoConnection = 'mongodb://localhost:27017/profile'; - MongoClient.connect(mongoConnection, (err, db) => { const cursor = db.collection('post').find(ObjectID(postId)); cursor.toArray((error, posts) => { From 54f6953e758338badeb46bf60b09b03e29fc301b Mon Sep 17 00:00:00 2001 From: YJohn Date: Sat, 26 Aug 2017 17:09:37 +0100 Subject: [PATCH 06/11] change the get command of post to Posts --- controllers/apiController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/controllers/apiController.js b/controllers/apiController.js index 36b3465..056731b 100644 --- a/controllers/apiController.js +++ b/controllers/apiController.js @@ -37,7 +37,7 @@ router.get('/students/country/:id', (req, res, next) => { router.get('/posts', (req, res, next) => { MongoClient.connect(mongoConnection, (err, db) => { - const cursor = db.collection('post').find({}); + const cursor = db.collection('posts').find({}); cursor.toArray((error, posts) => { db.close(); res.json(posts); From 4100c788cb3b0674cd4d5b130a32a29af5dc432d Mon Sep 17 00:00:00 2001 From: YJohn Date: Wed, 30 Aug 2017 22:27:41 +0100 Subject: [PATCH 07/11] added single-post view --- controllers/siteController.js | 21 ++++++++++++++++++--- views/index.handlebars | 2 +- views/single-view.handlebars | 10 ++++++++++ 3 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 views/single-view.handlebars diff --git a/controllers/siteController.js b/controllers/siteController.js index a05bf4c..1de753f 100644 --- a/controllers/siteController.js +++ b/controllers/siteController.js @@ -3,7 +3,8 @@ const express = require('express'); const router = express.Router(); const MongoClient = require('mongodb').MongoClient; const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/profile'; - +const objectID = require('mongodb').objectID; + router.get('/', function (req, res) { // Write code to connect to database and return posts console.log(mongoConnection); @@ -13,14 +14,28 @@ router.get('/', function (req, res) { db.close(); // res.json(posts); res.render('index', { - title: "Michael's profile", - subheading: "A modern Website built in Node with Handlebars", + title: "Yohannes's profile", + subheading: "A Great Website driven by a database", posts: posts }); }); }); }); +router.get('/post-:postId', (req, res) => { + const postId = req.params.postId; + MongoClient.connect(mongoConnection, (err, db) => { + const cursor = db.collection('posts').find(ObjectID(postId)); + cursor.toArray((error, posts) => { + db.close(); + res.render('single-post', { + title: posts[0].title, + subheading: "A Great Website driven by a database", + post: posts[0] + }); + }); + }); +}); router.get('/my-cv', function (req, res) { res.render('my-cv'); diff --git a/views/index.handlebars b/views/index.handlebars index 5cb63c7..be195d8 100755 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -1,6 +1,6 @@ {{#each posts}}
- +

{{this.title}}

diff --git a/views/single-view.handlebars b/views/single-view.handlebars new file mode 100644 index 0000000..86d7502 --- /dev/null +++ b/views/single-view.handlebars @@ -0,0 +1,10 @@ +
+ +

+ {{post.title}} +

+ +

+ {{post.content}} +

+
\ No newline at end of file From c5bcbf50e4f91bd49cc211a578eb83708f4482ee Mon Sep 17 00:00:00 2001 From: YJohn Date: Wed, 30 Aug 2017 23:08:13 +0100 Subject: [PATCH 08/11] refactoring the function --- controllers/siteController.js | 48 ++++++++++++++++------------------- helpers/dbClient.js | 17 +++++++++++++ 2 files changed, 39 insertions(+), 26 deletions(-) create mode 100644 helpers/dbClient.js diff --git a/controllers/siteController.js b/controllers/siteController.js index 1de753f..32756ce 100644 --- a/controllers/siteController.js +++ b/controllers/siteController.js @@ -2,39 +2,35 @@ const fs = require('fs'); const express = require('express'); const router = express.Router(); const MongoClient = require('mongodb').MongoClient; +const {objectID} = require('mongodb'); +const dbClient = require('../helpers/dbClient'); const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/profile'; -const objectID = require('mongodb').objectID; - -router.get('/', function (req, res) { - // Write code to connect to database and return posts - console.log(mongoConnection); - MongoClient.connect(mongoConnection, (err, db) => { - const cursor = db.collection('posts').find({}); - cursor.toArray((error, posts) => { - db.close(); - // res.json(posts); - res.render('index', { - title: "Yohannes's profile", - subheading: "A Great Website driven by a database", - posts: posts - }); + +router.get('/', (req, res) =>{ + const callback = (error, posts) => { + res.render('index', { + title: "Yohannes's profile", + subheading: "A Great Website driven by a database", + posts: posts }); - }); + }; + dbClient.getPosts({}, callback); }); router.get('/post-:postId', (req, res) => { const postId = req.params.postId; - MongoClient.connect(mongoConnection, (err, db) => { - const cursor = db.collection('posts').find(ObjectID(postId)); - cursor.toArray((error, posts) => { - db.close(); - res.render('single-post', { - title: posts[0].title, - subheading: "A Great Website driven by a database", - post: posts[0] - }); + + const callback = (error, posts) => { + + res.render('single-post', { + title: posts[0].title, + subheading: "A Great Website driven by a database", + post: posts[0] }); - }); + } + dbClient.getPosts({ + _id: objectID(postId) + }, callback); }); router.get('/my-cv', function (req, res) { diff --git a/helpers/dbClient.js b/helpers/dbClient.js new file mode 100644 index 0000000..ce0022e --- /dev/null +++ b/helpers/dbClient.js @@ -0,0 +1,17 @@ +const {MongoClient} = require('mongodb'); +const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/profile'; + +const getPosts = (query, successCallback) => { + MongoClient.connect(mongoConnection, (err, db) => { + const cursor = db.collection('posts').find(query); + cursor.toArray((error, posts) => { + db.close(); + successCallback(error, posts); + }); + }); +}; + + +module.exports={ + getPosts +}; \ No newline at end of file From 2247f7bf893b984c996a79412e416eca912ac8b7 Mon Sep 17 00:00:00 2001 From: YJohn Date: Fri, 1 Sep 2017 15:36:07 +0100 Subject: [PATCH 09/11] refactoring the function of site controller and fix a single post links --- controllers/apiController.js | 25 ++++++++++++------------- controllers/siteController.js | 4 ++-- views/index.handlebars | 2 +- views/layouts/main.handlebars | 4 ++-- views/single-post.handlebars | 12 ++++++++++++ views/single-view.handlebars | 4 ++-- 6 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 views/single-post.handlebars diff --git a/controllers/apiController.js b/controllers/apiController.js index 056731b..ab1759a 100644 --- a/controllers/apiController.js +++ b/controllers/apiController.js @@ -3,12 +3,9 @@ const express = require('express'); const router = express.Router(); const bodyParser = require('body-parser') const MongoClient = require('mongodb').MongoClient; -const ObjectID = require('mongodb').ObjectId; +const ObjectID = require('mongodb').ObjectID; router.use(bodyParser.json()); const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/profile'; -const getStudents = (req, res)=>{ - -} router.get('/students', (req, res) => { MongoClient.connect(mongoConnection, (err, db) => { @@ -31,7 +28,6 @@ router.get('/students/:id', (req, res)=>{ }); router.get('/students/country/:id', (req, res, next) => { - res.json(students); }); @@ -57,18 +53,21 @@ router.get('/posts/:id', (req, res,next) => { }); }); - - - - - - - +// router.post('/posts', (req, res) => { +// console.log(req.body); +// res.status(500).send('not implemented'); +// }); router.post('/posts', (req, res) => { console.log(req.body); - res.status(500).send('not implemented'); + const mongoConnection = 'mongodb://localhost:27017/profile'; + MongoClient.connect(mongoConnection, (err, db) => { + const cursor = db.collection('posts').insert(req.body, + (err, result) => { + res.sendStatus(200) + }); + }); }); module.exports = router; \ No newline at end of file diff --git a/controllers/siteController.js b/controllers/siteController.js index 32756ce..558977b 100644 --- a/controllers/siteController.js +++ b/controllers/siteController.js @@ -2,7 +2,7 @@ const fs = require('fs'); const express = require('express'); const router = express.Router(); const MongoClient = require('mongodb').MongoClient; -const {objectID} = require('mongodb'); +const ObjectID = require('mongodb').ObjectID; const dbClient = require('../helpers/dbClient'); const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/profile'; @@ -29,7 +29,7 @@ router.get('/post-:postId', (req, res) => { }); } dbClient.getPosts({ - _id: objectID(postId) + _id: ObjectID(postId) }, callback); }); diff --git a/views/index.handlebars b/views/index.handlebars index be195d8..ebceee2 100755 --- a/views/index.handlebars +++ b/views/index.handlebars @@ -1,6 +1,6 @@ {{#each posts}}
- +

{{this.title}}

diff --git a/views/layouts/main.handlebars b/views/layouts/main.handlebars index d0abad6..a950848 100644 --- a/views/layouts/main.handlebars +++ b/views/layouts/main.handlebars @@ -44,9 +44,9 @@
{{#if title}} -

{{title}} with Handlebars

+

{{title}}

{{else}} -

My Profile with Handlebars

+

JOHN Profile

{{/if}}
diff --git a/views/single-post.handlebars b/views/single-post.handlebars new file mode 100644 index 0000000..1774969 --- /dev/null +++ b/views/single-post.handlebars @@ -0,0 +1,12 @@ +
+ +

+ {{post.title}} +

+
+

+ {{post.content}} +

+ +
+
\ No newline at end of file diff --git a/views/single-view.handlebars b/views/single-view.handlebars index 86d7502..8ccb2ce 100644 --- a/views/single-view.handlebars +++ b/views/single-view.handlebars @@ -1,5 +1,4 @@
-

{{post.title}}

@@ -7,4 +6,5 @@

{{post.content}}

-
\ No newline at end of file +
+ From 38f851ff1b9fe641394ea13585e30b0a1143608d Mon Sep 17 00:00:00 2001 From: YJohn Date: Fri, 1 Sep 2017 16:47:44 +0100 Subject: [PATCH 10/11] deployed the mongodb to the heroku --- helpers/dbClient.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/helpers/dbClient.js b/helpers/dbClient.js index ce0022e..e46c299 100644 --- a/helpers/dbClient.js +++ b/helpers/dbClient.js @@ -1,5 +1,5 @@ const {MongoClient} = require('mongodb'); -const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/profile'; +const mongoConnection = process.env.MONGO_URL || 'mongodb://localhost:27017/profile'; const getPosts = (query, successCallback) => { MongoClient.connect(mongoConnection, (err, db) => { From b547527bf23957f0fbfbb7a1bcf96d8b821f9b4b Mon Sep 17 00:00:00 2001 From: YJohn Date: Sun, 3 Sep 2017 11:30:32 +0100 Subject: [PATCH 11/11] create a post.js file and update the API Controller and the site controller --- controllers/apiController.js | 33 +++++++++++++++++-------- controllers/siteController.js | 46 +++++++++++++++++++++++++++++------ models/Post.js | 15 ++++++++++++ package.json | 6 +++-- views/admin.handlebars | 10 ++++---- views/single-post.handlebars | 2 +- views/single-view.handlebars | 10 -------- 7 files changed, 86 insertions(+), 36 deletions(-) create mode 100644 models/Post.js delete mode 100644 views/single-view.handlebars diff --git a/controllers/apiController.js b/controllers/apiController.js index ab1759a..5792355 100644 --- a/controllers/apiController.js +++ b/controllers/apiController.js @@ -6,9 +6,12 @@ const MongoClient = require('mongodb').MongoClient; const ObjectID = require('mongodb').ObjectID; router.use(bodyParser.json()); const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/profile'; +const mongoose = require('mongoose'); +const Post = require('../models/Post.js'); + router.get('/students', (req, res) => { - MongoClient.connect(mongoConnection, (err, db) => { + MongoClient.connect(mongoConnection, (err, db) => { const cursor = db.collection('students').find({}); cursor.toArray((error, students) => { db.close(); @@ -16,7 +19,7 @@ router.get('/students', (req, res) => { }); }); }); -router.get('/students/:id', (req, res)=>{ +router.get('/students/:id', (req, res) => { const studentId = req.params.id; MongoClient.connect(mongoConnection, (err, db) => { const cursor = db.collection('students').find(ObjectID(studentId)); @@ -42,7 +45,7 @@ router.get('/posts', (req, res, next) => { }); -router.get('/posts/:id', (req, res,next) => { +router.get('/posts/:id', (req, res, next) => { const postId = req.params.id; MongoClient.connect(mongoConnection, (err, db) => { const cursor = db.collection('post').find(ObjectID(postId)); @@ -60,14 +63,24 @@ router.get('/posts/:id', (req, res,next) => { router.post('/posts', (req, res) => { - console.log(req.body); + const callback = (error, post) => { + if (error) { + console.error(error); + return res.sendStatus(500); + } + + console.log('post saved successfully', post); + res.send(post); + } + const mongoConnection = 'mongodb://localhost:27017/profile'; - MongoClient.connect(mongoConnection, (err, db) => { - const cursor = db.collection('posts').insert(req.body, - (err, result) => { - res.sendStatus(200) - }); - }); + mongoose.connect(mongoConnection); + // This is using the Model to create an object + // based on the fields submitted from the form + const newPost = new Post(req.body); + newPost.save(callback); }); + + module.exports = router; \ No newline at end of file diff --git a/controllers/siteController.js b/controllers/siteController.js index 558977b..3a5b946 100644 --- a/controllers/siteController.js +++ b/controllers/siteController.js @@ -3,10 +3,15 @@ const express = require('express'); const router = express.Router(); const MongoClient = require('mongodb').MongoClient; const ObjectID = require('mongodb').ObjectID; +const Post = require('../models/Post.js'); +const mongoose = require('mongoose'); +const formidable = require('express-formidable'); + const dbClient = require('../helpers/dbClient'); const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/profile'; -router.get('/', (req, res) =>{ +router.use(formidable()); +router.get('/', (req, res) => { const callback = (error, posts) => { res.render('index', { title: "Yohannes's profile", @@ -14,25 +19,50 @@ router.get('/', (req, res) =>{ posts: posts }); }; - dbClient.getPosts({}, callback); + mongoose.connect(mongoConnection); + Post.find({}, callback); }); router.get('/post-:postId', (req, res) => { const postId = req.params.postId; - const callback = (error, posts) => { + const callback = (error, post) => { res.render('single-post', { - title: posts[0].title, + title: post.title, subheading: "A Great Website driven by a database", - post: posts[0] + post: post }); } - dbClient.getPosts({ - _id: ObjectID(postId) - }, callback); + // dbClient.getPosts({ + // _id: ObjectID(postId) + // }, callback); + + mongoose.connect(mongoConnection); + Post.findById(postId, callback); +}); + +// const post = req.fields; +router.post('/save-post', (req, res) => { + + const callback = (error, post) => { + if (error) { + console.error(error); + return res.redirect('/error'); + } + + console.log('post saved successfully', post); + res.redirect('/'); + } + mongoose.connect(mongoConnection); + // This is using the Model to create an object + // based on the fields submitted from the form + const newPost = new Post(req.fields); + newPost.save(callback); }); + + router.get('/my-cv', function (req, res) { res.render('my-cv'); }); diff --git a/models/Post.js b/models/Post.js new file mode 100644 index 0000000..ab14ca4 --- /dev/null +++ b/models/Post.js @@ -0,0 +1,15 @@ +const mongoose = require('mongoose'); +const { Schema } = mongoose; + +const schema = new Schema({ + title: { + type: String, + require: true + }, + contents: String, + summary: String +}); + +const Post = mongoose.model('Posts', schema); + +module.exports = Post; \ No newline at end of file diff --git a/package.json b/package.json index ecd7a77..817cd46 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "This is the exercise that accompanies the first Node class. Based on the Node Girls Workshop https://github.com/node-girls/express-workshop", "scripts": { "start": "node server.js", - "dev": "nodemon server.js", + "dev": "nodemon --inspect server.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { @@ -20,8 +20,10 @@ "dependencies": { "body-parser": "^1.17.2", "express": "^4.15.4", + "express-formidable": "^1.0.0", "express-handlebars": "^3.0.0", - "mongodb": "^2.2.31" + "mongodb": "^2.2.31", + "mongoose": "^4.11.9" }, "devDependencies": { "nodemon": "^1.11.0" diff --git a/views/admin.handlebars b/views/admin.handlebars index 0839009..5cc2f38 100755 --- a/views/admin.handlebars +++ b/views/admin.handlebars @@ -1,24 +1,24 @@

Create a new Blog post

-
+
- +

- +

- +

@@ -31,4 +31,4 @@
- \ No newline at end of file + \ No newline at end of file diff --git a/views/single-post.handlebars b/views/single-post.handlebars index 1774969..6c7e0b2 100644 --- a/views/single-post.handlebars +++ b/views/single-post.handlebars @@ -5,7 +5,7 @@

- {{post.content}} + {{post.contents}}

diff --git a/views/single-view.handlebars b/views/single-view.handlebars deleted file mode 100644 index 8ccb2ce..0000000 --- a/views/single-view.handlebars +++ /dev/null @@ -1,10 +0,0 @@ -
-

- {{post.title}} -

- -

- {{post.content}} -

-
-