Skip to content

Commit

Permalink
Converted to multi-line comments
Browse files Browse the repository at this point in the history
  • Loading branch information
arvention committed Apr 2, 2020
1 parent 3e2c1d7 commit 83b8897
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 101 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
130 changes: 84 additions & 46 deletions add_data.js
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -56,41 +72,53 @@ 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',
username: 'QueenInTheNorth',
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',
username: 'FacelessMan',
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',
Expand All @@ -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',
Expand All @@ -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',
Expand All @@ -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);

52 changes: 33 additions & 19 deletions controllers/controller.js
Original file line number Diff line number Diff line change
@@ -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;
Loading

0 comments on commit 83b8897

Please sign in to comment.