diff --git a/controllers/apiController.js b/controllers/apiController.js index 8db20fa..5792355 100644 --- a/controllers/apiController.js +++ b/controllers/apiController.js @@ -2,27 +2,85 @@ 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 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) => { + 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; + 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); + MongoClient.connect(mongoConnection, (err, db) => { + const cursor = db.collection('posts').find({}); + cursor.toArray((error, posts) => { + db.close(); + res.json(posts); + }); + }); +}); + + +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)); + cursor.toArray((error, posts) => { + db.close(); + res.json(posts); + }); + }); }); +// 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 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'; + 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 32ccd8d..3a5b946 100644 --- a/controllers/siteController.js +++ b/controllers/siteController.js @@ -1,27 +1,67 @@ const fs = require('fs'); 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'); -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 +const dbClient = require('../helpers/dbClient'); +const mongoConnection = process.env.MONGODB_URI || 'mongodb://localhost:27017/profile'; + +router.use(formidable()); +router.get('/', (req, res) => { + const callback = (error, posts) => { res.render('index', { - title: "Michael's profile", - subheading: "A modern Website built in Node with Handlebars", - posts: postsJson + title: "Yohannes's profile", + subheading: "A Great Website driven by a database", + posts: posts }); }; - fs.readFile(filePath, callbackFunction); + mongoose.connect(mongoConnection); + Post.find({}, callback); +}); + +router.get('/post-:postId', (req, res) => { + const postId = req.params.postId; + + const callback = (error, post) => { + + res.render('single-post', { + title: post.title, + subheading: "A Great Website driven by a database", + post: post + }); + } + // 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/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/helpers/dbClient.js b/helpers/dbClient.js new file mode 100644 index 0000000..e46c299 --- /dev/null +++ b/helpers/dbClient.js @@ -0,0 +1,17 @@ +const {MongoClient} = require('mongodb'); +const mongoConnection = process.env.MONGO_URL || '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 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 8fa76b4..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,7 +20,10 @@ "dependencies": { "body-parser": "^1.17.2", "express": "^4.15.4", - "express-handlebars": "^3.0.0" + "express-formidable": "^1.0.0", + "express-handlebars": "^3.0.0", + "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
-