diff --git a/README.md b/README.md index ea14358..6f47027 100644 --- a/README.md +++ b/README.md @@ -73,8 +73,8 @@ app listening at port 9090 7. Based on the description above, this web application displays the profile of a certain user stored in the database based on their username provided as parameter in the URL requested by the client. To do this, we have to study the following files: - We need to have an `hbs` file which will be rendered with details from the database. Go to [`views/profile.hbs`](https://github.com/arvention/ccapdev-database/blob/master/views/profile.hbs) and study the needed values to be rendered in the profile page. -- Check [`models\db.js`](https://github.com/arvention/ccapdev-database/blob/master/models/db.js) which contains necessary database functions for CRUD (Create, Read, Update, Delete) operations. These methods were discussed in the video lecture. You may read through the inline comments in the [`file`](https://github.com/arvention/ccapdev-database/blob/master/models/db.js) to review. -- Check [`routes\routes.js`](https://github.com/arvention/ccapdev-database/blob/master/routes/routes.js) which connects a specific callback function defined in [`controllers/controller.js`](https://github.com/arvention/ccapdev-database/blob/master/controllers/controller.js) to its corresponding path and HTTP method. +- Check [`models/db.js`](https://github.com/arvention/ccapdev-database/blob/master/models/db.js) which contains necessary database functions for CRUD (Create, Read, Update, Delete) operations. These methods were discussed in the video lecture. You may read through the inline comments in the [`file`](https://github.com/arvention/ccapdev-database/blob/master/models/db.js) to review. +- Check [`routes/routes.js`](https://github.com/arvention/ccapdev-database/blob/master/routes/routes.js) which connects a specific callback function defined in [`controllers/controller.js`](https://github.com/arvention/ccapdev-database/blob/master/controllers/controller.js) to its corresponding path and HTTP method. - Study the callback functions defined in [`controllers/controller.js`](https://github.com/arvention/ccapdev-database/blob/master/controllers/controller.js). These functions are called when a client requests for a specific defined path in our server. All of these mentioned files are properly documented so you might want to open them to understand their content more. diff --git a/add_data.js b/add_data.js index ae4aca5..d589809 100644 --- a/add_data.js +++ b/add_data.js @@ -1,19 +1,27 @@ -// This script creates the database -// and inserts 8 user details in the collection `profiles` +/* + This script creates the database + and inserts 8 user details in the collection `profiles` +*/ // import module from `./models/db.js` const db = require('./models/db.js'); -// name of the collection (table) -// to perform CRUD (Create, Read, Update, Delete) operations +/* + name of the collection (table) + to perform CRUD (Create, Read, Update, Delete) operations +*/ const collection = 'profiles'; -// calls the function createDatabase() -// defined in the `database` object in `./models/db.js` +/* + calls the function createDatabase() + defined in the `database` object in `./models/db.js` +*/ db.createDatabase(); -// creates an object -// containing first name, last name, username, and bio of a user +/* + creates an object + containing first name, last name, username, and bio of a user +*/ var user = { fName: 'Ned', lName: 'Stark', @@ -24,13 +32,17 @@ var user = { But I grew up with soldiers. I learned how to die a long time ago.` }; -// calls the function insertOne() -// defined in the `database` object in `./models/db.js` -// stores the object `user` in the collection (table) `profiles` +/* + calls the function insertOne() + defined in the `database` object in `./models/db.js` + stores the object `user` in the collection (table) `profiles` +*/ db.insertOne(collection, user); -// creates an object -// containing first name, last name, username, and bio of a user +/* + creates an object + containing first name, last name, username, and bio of a user +*/ var user = { fName: 'Bran', lName: 'Stark', @@ -40,13 +52,17 @@ var user = { That's something the Gods can't forgive.` }; -// calls the function insertOne() -// defined in the `database` object in `./models/db.js` -// stores the object `user` in the collection (table) `profiles` +/* + calls the function insertOne() + defined in the `database` object in `./models/db.js` + stores the object `user` in the collection (table) `profiles` +*/ db.insertOne(collection, user); -// creates an object -// containing first name, last name, username, and bio of a user +/* + creates an object + containing first name, last name, username, and bio of a user +*/ var user = { fName: 'Jon', lName: 'Snow', @@ -56,13 +72,17 @@ var user = { Then there are no more answers, only better and better lies.` }; -// calls the function insertOne() -// defined in the `database` object in `./models/db.js` -// stores the object `user` in the collection (table) `profiles` +/* + calls the function insertOne() + defined in the `database` object in `./models/db.js` + stores the object `user` in the collection (table) `profiles` +*/ db.insertOne(collection, user); -// creates an object -// containing first name, last name, username, and bio of a user +/* + creates an object + containing first name, last name, username, and bio of a user +*/ var user = { fName: 'Sansa', lName: 'Stark', @@ -70,13 +90,17 @@ var user = { bio: `I'm A Slow Learner, It's True. But I Learn.` }; -// calls the function insertOne() -// defined in the `database` object in `./models/db.js` -// stores the object `user` in the collection (table) `profiles` +/* + calls the function insertOne() + defined in the `database` object in `./models/db.js` + stores the object `user` in the collection (table) `profiles` +*/ db.insertOne(collection, user); -// creates an object -// containing first name, last name, username, and bio of a user +/* + creates an object + containing first name, last name, username, and bio of a user +*/ var user = { fName: 'Arya', lName: 'Stark', @@ -84,13 +108,17 @@ var user = { bio: `Not today.` }; -// calls the function insertOne() -// defined in the `database` object in `./models/db.js` -// stores the object `user` in the collection (table) `profiles` +/* + calls the function insertOne() + defined in the `database` object in `./models/db.js` + stores the object `user` in the collection (table) `profiles` +*/ db.insertOne(collection, user); -// creates an object -// containing first name, last name, username, and bio of a user +/* + creates an object + containing first name, last name, username, and bio of a user +*/ var user = { fName: 'Cersei', lName: 'Lannister', @@ -99,13 +127,17 @@ var user = { There is no middle ground.` }; -// calls the function insertOne() -// defined in the `database` object in `./models/db.js` -// stores the object `user` in the collection (table) `profiles` +/* + calls the function insertOne() + defined in the `database` object in `./models/db.js` + stores the object `user` in the collection (table) `profiles` +*/ db.insertOne(collection, user); -// creates an object -// containing first name, last name, username, and bio of a user +/* + creates an object + containing first name, last name, username, and bio of a user +*/ var user = { fName: 'Tyrion', lName: 'Lannister', @@ -114,13 +146,17 @@ var user = { Wear it like armor, and it can never be used to hurt you` }; -// calls the function insertOne() -// defined in the `database` object in `./models/db.js` -// stores the object `user` in the collection (table) `profiles` +/* + calls the function insertOne() + defined in the `database` object in `./models/db.js` + stores the object `user` in the collection (table) `profiles` +*/ db.insertOne(collection, user); -// creates an object -// containing first name, last name, username, and bio of a user +/* + creates an object + containing first name, last name, username, and bio of a user +*/ var user = { fName: 'Daenerys', lName: 'Targaryen', @@ -132,8 +168,10 @@ var user = { I’m going to break the wheel.` }; -// calls the function insertOne() -// defined in the `database` object in `./models/db.js` -// stores the object `user` in the collection (table) `profiles` +/* + calls the function insertOne() + defined in the `database` object in `./models/db.js` + stores the object `user` in the collection (table) `profiles` +*/ db.insertOne(collection, user); diff --git a/controllers/controller.js b/controllers/controller.js index dde8234..63c5480 100644 --- a/controllers/controller.js +++ b/controllers/controller.js @@ -1,43 +1,57 @@ // import module from `../models/db.js` const db = require('../models/db.js'); -// defines an object which contains functions executed as callback -// when a client requests for a certain path in the server +/* + defines an object which contains functions executed as callback + when a client requests for a certain path in the server +*/ const controller = { - // executed when the client sends an HTTP GET request `/favicon.ico` - // as defined in `../routes/routes.js` + /* + executed when the client sends an HTTP GET request `/favicon.ico` + as defined in `../routes/routes.js` + */ getFavicon: function (req, res) { res.status(204); }, - // executed when the client sends an HTTP GET request `/:username` - // as defined in `../routes/routes.js` + /* + executed when the client sends an HTTP GET request `/:username` + as defined in `../routes/routes.js` + */ getProfile: function (req, res) { // gets the parameter `username` from the URL var u = req.params.username; - // creates an object `query` - // which assigns the value of the variable `u` to field `username` + /* + creates an object `query` + which assigns the value of the variable `u` to field `username` + */ var query = {username: u}; - // calls the function findOne() - // defined in the `database` object in `../models/db.js` - // this function searches the collection `profiles` - // based on the value set in object `query` - // the third parameter is a callback function - // this called when the database returns a value - // saved in variable `result` + /* + calls the function findOne() + defined in the `database` object in `../models/db.js` + this function searches the collection `profiles` + based on the value set in object `query` + the third parameter is a callback function + this called when the database returns a value + saved in variable `result` + */ db.findOne('profiles', query, function (result) { - // renders `../views/profile.hbs` - // with the values in variable `results` + /* + renders `../views/profile.hbs` + with the values in variable `results` + */ res.render('profile', result); }); } } -// exports the object `controller` (defined above) -// when another script exports from this file +/* + exports the object `controller` (defined above) + when another script exports from this file +*/ module.exports = controller; diff --git a/models/db.js b/models/db.js index e5ab5b3..c08b648 100644 --- a/models/db.js +++ b/models/db.js @@ -15,7 +15,9 @@ const options = { useUnifiedTopology: true }; // defines an object which contains necessary database functions const database = { - // creates database + /* + creates database + */ createDatabase: function() { client.connect(url, options, function (err, db) { if(err) throw err; @@ -24,7 +26,9 @@ const database = { }); }, - // creates collection `collection` + /* + creates collection `collection` + */ createCollection: function(collection) { client.connect(url, options, function(err, db) { if(err) throw err; @@ -37,7 +41,9 @@ const database = { }); }, - // inserts document `doc` to collection `collection` + /* + inserts document `doc` to collection `collection` + */ insertOne: function(collection, doc) { client.connect(url, options, function (err, db) { if(err) throw err; @@ -50,7 +56,9 @@ const database = { }); }, - // inserts array of documents `docs` to collection `collection` + /* + inserts array of documents `docs` to collection `collection` + */ insertMany: function(collection, docs) { client.connect(url, options, function (err, db) { if(err) throw err; @@ -63,10 +71,12 @@ const database = { }); }, - // searches for a single document in the collection `collection` - // based on the contents of object `query` - // callback function is called - // when the database has finished the execution of findOne() function + /* + searches for a single document in the collection `collection` + based on the contents of object `query` + callback function is called + when the database has finished the execution of findOne() function + */ findOne: function(collection, query, callback) { client.connect(url, options, function (err, db) { if(err) throw err; @@ -79,10 +89,12 @@ const database = { }); }, - // searches for multiple documents in the collection `collection` - // based on the contents of object `query` - // callback function is called - // when the database has finished the execution of findMany() function + /* + searches for multiple documents in the collection `collection` + based on the contents of object `query` + callback function is called + when the database has finished the execution of findMany() function + */ findMany: function(collection, query, sort=null, projection=null, callback) { client.connect(url, options, function (err, db) { if(err) throw err; @@ -96,8 +108,10 @@ const database = { }); }, - // deletes a single document in the collection `collection` - // based on the object `filter` + /* + deletes a single document in the collection `collection` + based on the object `filter` + */ deleteOne: function(collection, filter) { client.connect(url, options, function (err, db) { if(err) throw err; @@ -110,8 +124,10 @@ const database = { }); }, - // deletes multiple documents in the collection `collection` - // based on the object `filter` + /* + deletes multiple documents in the collection `collection` + based on the object `filter` + */ deleteMany: function(collection, filter) { client.connect(url, options, function (err, db) { if(err) throw err; @@ -124,7 +140,9 @@ const database = { }); }, - // drops the collection `collection` + /* + drops the collection `collection` + */ dropCollection: function(collection) { client.connect(url, options, function (err, db) { if(err) throw err; @@ -137,9 +155,11 @@ const database = { }); }, - // updates the value defined in the object `update` - // on a single document in the collection `collection` - // based on the object `filter` + /* + updates the value defined in the object `update` + on a single document in the collection `collection` + based on the object `filter` + */ updateOne: function(collection, filter, update) { client.connect(url, options, function (err, db) { if(err) throw err; @@ -152,9 +172,11 @@ const database = { }); }, - // updates the value defined in the object `update` - // on multiple documents in the collection `collection` - // based on the object `filter` + /* + updates the value defined in the object `update` + on multiple documents in the collection `collection` + based on the object `filter` + */ updateMany: function(collection, filter, update) { client.connect(url, options, function (err, db) { if(err) throw err; @@ -168,6 +190,8 @@ const database = { } } -// exports the object `database` (defined above) -// when another script exports from this file +/* + exports the object `database` (defined above) + when another script exports from this file +*/ module.exports = database; diff --git a/routes/routes.js b/routes/routes.js index cee9c1c..ab6abfd 100644 --- a/routes/routes.js +++ b/routes/routes.js @@ -7,17 +7,23 @@ const controller = require('../controllers/controller.js') const app = express(); -// execute function getFavicon() -// defined in object `controller` in `../controllers/controller.js` -// when a client sends an HTTP GET request for `/favicon.ico` +/* + execute function getFavicon() + defined in object `controller` in `../controllers/controller.js` + when a client sends an HTTP GET request for `/favicon.ico` +*/ app.get('/favicon.ico', controller.getFavicon); -// execute function getFavicon() -// defined in object `controller` in `../controllers/controller.js` -// when a client sends an HTTP GET request for `/:username` -// where `username` is a parameter +/* + execute function getFavicon() + defined in object `controller` in `../controllers/controller.js` + when a client sends an HTTP GET request for `/:username` + where `username` is a parameter +*/ app.get('/:username', controller.getProfile); -// exports the object `app` (defined above) -// when another script exports from this file +/* + exports the object `app` (defined above) + when another script exports from this file +*/ module.exports = app;