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
99 changes: 82 additions & 17 deletions controllers/apiController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,92 @@ 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;
const mongoose = require('mongoose');
const Post = require('../models/Posts');

router.use(bodyParser.json());

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/:studentid', (req, res) => {
const studentID = req.params.studentid;
console.log(studentID);
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[0]);
});
});
});

router.post('/posts', (req, res) => {
console.log(req.body);
res.status(500).send('not implemented');

router.get('/students/country/:country', (req, res) => {
const studentsdefinedbyCountry = req.params.country;
const mongoConnection = 'mongodb://localhost:27017/profile';

MongoClient.connect(mongoConnection, (err, db) => {
const cursor = db.collection('students').find({ "Country": studentsdefinedbyCountry });
cursor.toArray((error, studentsdefinedbyCountry) => {
db.close();
res.json(studentsdefinedbyCountry);
});

});

});

module.exports = router;
router.get('/api/posts', (req, res, next) => {

const post = req.body;

console.log(req.body); // contains non-file fields
const callback = (error, post) => {
// handle any errors which might ocur
if (error) {
console.error(error);
return res.redirect('/error');
}

console.log('post saved successfully', post);
res.send(post);
}

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);

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').find({});
// cursor.toArray((error, posts) => {
// db.close();
// res.json(posts);
// });
// });

// });

module.exports = router;
89 changes: 74 additions & 15 deletions controllers/siteController.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,84 @@
const fs = require('fs');
const express = require('express');
const router = express.Router();
const { ObjectId } = require('mongodb');
const dbClient = require('../helpers/dbClient');
const MongoClient = require('mongodb').MongoClient;
const mongoose = require('mongoose');
const Post = require('../models/Posts');
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 mongoConnection = process.env.MONGO_URI || 'mongodb://localhost:27017/profile';
// const mongoose = require('mongoose');

// router.get('/', function (req, res) { '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: "Won's profile",
subheading: "A modern Website built in Node with Handlebars",
posts: posts
});
}
mongoose.connect(mongoConnection);
Post.find({}, callback);
});


router.post('/save-post', (req, res) => {

const post = req.fields;

console.log(req.fields); // contains non-file fields
const callback = (error, post) => {
// handle any errors which might ocur
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('/post-:postId', (req, res) => {
const postId = req.params.postId;

const callback = (error, posts) => {

res.render('single-view', {
title: posts[0].title,
subheading: "A modern Website built in Node with Handlebars",
post: posts[0]
});
};
fs.readFile(filePath, callbackFunction);

dbClient.getPosts({
_id: ObjectId(postId)
}, callback);
});








// creating a new function

router.get('/my-cv', function (req, res) {
res.render('my-cv');
});
Expand All @@ -35,4 +91,7 @@ router.get('/contact', function (req, res) {
res.render('contact');
});

router.get('/follow me', function (req, res) {
res.render('follow me');
})
module.exports = router;
3 changes: 3 additions & 0 deletions data/menu.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
}, {
"text": "Admin",
"link": "/admin"
}, {
"text": "Follow me",
"link": "/follow"
}
]
19 changes: 0 additions & 19 deletions data/posts.json

This file was deleted.

21 changes: 21 additions & 0 deletions helpers/dbClient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@



const {MongoClient} = require('mongodb');
const mongoConnection = process.env.MONGO_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
};
12 changes: 12 additions & 0 deletions models/Posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const mongoose = require('mongoose');
const { Schema } = mongoose;

const schema = new Schema({
title: String,
contents: String,
summary: String
});

const Post = mongoose.model('posts', schema);

module.exports = Post;
Loading