Skip to content
This repository was archived by the owner on Jan 14, 2023. It is now read-only.
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
88 changes: 73 additions & 15 deletions controllers/apiController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
70 changes: 55 additions & 15 deletions controllers/siteController.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
19 changes: 0 additions & 19 deletions data/posts.json

This file was deleted.

17 changes: 17 additions & 0 deletions helpers/dbClient.js
Original file line number Diff line number Diff line change
@@ -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
};
15 changes: 15 additions & 0 deletions models/Post.js
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
Expand Down
10 changes: 5 additions & 5 deletions views/admin.handlebars
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
<div class="row">
<p>Create a new Blog post</p>
<form name="sentMessage" id="contactForm" novalidate>
<form action="/save-post" method="POST" name="sentMessage" id="contactForm" novalidate>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Title</label>
<input type="text" class="form-control" placeholder="Blog Title" id="title" required data-validation-required-message="Please enter the title of the blog post.">
<input name="title" type="text" class="form-control" placeholder="Blog Title" id="title" required data-validation-required-message="Please enter the title of the blog post.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Summary</label>
<textarea rows="5" class="form-control" placeholder="Summary" id="summary" required data-validation-required-message="Please enter the summary of the blog post."></textarea>
<textarea name="summary"rows="5" class="form-control" placeholder="Summary" id="summary" required data-validation-required-message="Please enter the summary of the blog post."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Content of the Post</label>
<textarea rows="5" class="form-control" placeholder="Contents" id="contents" required data-validation-required-message="Please enter the contents of the blog post."></textarea>
<textarea name="contents" rows="5" class="form-control" placeholder="Contents" id="contents" required data-validation-required-message="Please enter the contents of the blog post."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
Expand All @@ -31,4 +31,4 @@
</div>
</form>
</div>
<script src="/js/posts-admin.js"></script>
<script src="/js/posts-admin.js"></script>
2 changes: 1 addition & 1 deletion views/index.handlebars
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{#each posts}}
<div class="post-preview">
<a href="post.html">
<a href="/post-{{this._id}}">
<h2 class="post-title">
{{this.title}}
</h2>
Expand Down
4 changes: 2 additions & 2 deletions views/layouts/main.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
<div class="site-heading">

{{#if title}}
<h1>{{title}} with Handlebars</h1>
<h1>{{title}}</h1>
{{else}}
<h1>My Profile with Handlebars</h1>
<h1>JOHN Profile</h1>
{{/if}}

<hr class="small">
Expand Down
12 changes: 12 additions & 0 deletions views/single-post.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="post-preview">
<a href="/{{post._id}}">
<h2 class="post-title">
{{post.title}}
</h2>
<hr />
<h3 class="post-subtitle">
{{post.contents}}
</h3>

</a>
</div>