-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathprofileController.js
More file actions
98 lines (81 loc) · 3.13 KB
/
profileController.js
File metadata and controls
98 lines (81 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// import module `database` from `../models/db.js`
const db = require('../models/db.js');
// import module `User` from `../models/UserModel.js`
const User = require('../models/UserModel.js');
/*
defines an object which contains functions executed as callback
when a client requests for `profile` paths in the server
*/
const profileController = {
/*
executed when the client sends an HTTP GET request `/profile/:idNum`
as defined in `../routes/routes.js`
*/
getProfile: function (req, res) {
// query where `idNum` is equal to URL parameter `idNum`
var query = {idNum: req.params.idNum};
// fields to be returned
var projection = 'fName lName idNum';
var details = {};
// checks if a user is logged-in by checking the session data
if(req.session.idNum) {
/*
sets `details.flag` to true
to display the profile and logout tabs in the nav bar
sets the value of `details.name` to `req.session.name`
to display the name of the logged-in user
in the profile tab of the nav bar
sets the value of `details.uidNum` to `req.session.idNum`
to provide the link the profile of the logged-in user
in the profile tab of the nav bar
these values are rendered in `../views/partials/header.hbs`
*/
details.flag = true;
details.name = req.session.name;
details.uidNum = req.session.idNum;
}
// else if a user is not yet logged-in
else
/*
sets `details.flag` to false
to hide the profile and logout tabs in the nav bar
*/
details.flag = false;
/*
calls the function findOne()
defined in the `database` object in `../models/db.js`
this function searches the collection `users`
based on the value set in object `query`
the third parameter is a string containing fields to be returned
the fourth parameter is a callback function
this called when the database returns a value
saved in variable `result`
*/
db.findOne(User, query, projection, function(result) {
/*
if the user exists in the database
render the profile page with their details
*/
if(result != null) {
details.fName = result.fName;
details.lName = result.lName;
details.idNum = result.idNum;
// render `../views/profile.hbs`
res.render('profile', details);
}
/*
if the user does not exist in the database
render the error page
*/
else {
// render `../views/error.hbs`
res.render('error', details);
}
});
}
}
/*
exports the object `profileController` (defined above)
when another script exports from this file
*/
module.exports = profileController;