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
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Heroku Link:-
https://tranquil-plateau-63335.herokuapp.com/

# Node MongoDB Workshop
This is the exercise/homework repo alongside the Node/MongoDB class.

Expand Down
49 changes: 37 additions & 12 deletions controllers/apiController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,52 @@ const fs = require('fs');
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser')
const { ObjectID } = require('mongodb');

router.use(bodyParser.json());

const dbClient = require('../helpers/dbClient.js');

router.get('/students', (req, res) => {
const callBack = (error, students) => {
if (error) {
res.sendStatus(500)
}
else {
res.json(students);
}
}
dbClient.getFromDatabase({}, "students", callBack)
});

router.get('/students/:id', (req, res) => {
const studentId = req.params.id;
const callBack = (error, students) => {
if (error) {
res.sendStatus(500)
}
else {
res.json(students[0]);
}
}
dbClient.getFromDatabase(ObjectID(studentId), "students", callBack)
});

router.get('/posts', (req, res, next) => {
const filePath = __dirname + '/../data/posts.json';
const callbackFunction = function(error, file) {
if(error) {
return next(error);
const callBack = (error, posts) => {
if (error) {
res.sendStatus(500)
}
else {
res.json(posts);
}
// 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);
}
dbClient.getFromDatabase({}, "posts", callBack)
});

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

module.exports = router;
module.exports =router;
58 changes: 37 additions & 21 deletions controllers/siteController.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,54 @@
const fs = require('fs');
const express = require('express');
const router = express.Router();
const { ObjectID } = require('mongodb');

router.get('/', function (req, res) {
const filePath = __dirname + '/../data/posts.json';
const callbackFunction = function(error, file) {
if(error) {
return next(error);
const dbClient = require('../helpers/dbClient.js');

router.get('/', (req, res) => {
const callBack = (error, posts) => {
if (error) {
res.sendStatus(500)
}
else {
res.render('index', {
title: "MongoDB profile",
subheading: "A modern Website built in Node with Handlebars",
posts: posts
});
}
// 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
});
};
fs.readFile(filePath, callbackFunction);
}
dbClient.getFromDatabase({}, "posts", callBack)

});

router.get('/post/:postid', (req, res) => {
const postId = req.params.postid;
const callBack = (error, posts) => {
if (error) {
res.sendStatus(500)
}
else {
res.render('single-post', {
title: posts[0].title,
subheading: "A modern Website built in Node with Handlebars",
post: posts[0]
});
}
}
dbClient.getFromDatabase(ObjectID(postId), "posts", callBack)
});

router.get('/my-cv', function (req, res) {
router.get('/my-cv', (req, res) => {
res.render('my-cv');
});

router.get('/admin', function (req, res) {
router.get('/admin', (req, res) => {
res.render('admin');
});

router.get('/contact', function (req, res) {
router.get('/contact', (req, res) => {
res.render('contact');
});

module.exports = router;
module.exports =router;
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.MONGODB_URI || 'mongodb://localhost:27017/profile';

const getFromDatabase = (query, collection, sucessCallBack) => {
MongoClient.connect(mongoConnection, (error, db) => {
const cursor = db.collection(collection).find(query);
cursor.toArray((error, collections) => {
db.close();
sucessCallBack(error, collections)
});
});
}

module.exports = {
getFromDatabase
};
Loading