From 89e264f637cbf57ef4eed3e6b91d635849b2fa1a Mon Sep 17 00:00:00 2001 From: HumairAK Date: Fri, 22 Jul 2016 20:20:47 -0400 Subject: made fixes to server callbacks --- node_simple.js | 14 ++++++++------ routes/admin.js | 1 + views/signup.hbs | 2 +- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/node_simple.js b/node_simple.js index f735b82..cd6d06f 100644 --- a/node_simple.js +++ b/node_simple.js @@ -112,11 +112,11 @@ exports.add_user = function (fields, callbackUser) { }; // find out if this user already exists by checking their email - exports.find_user( fields[0] ,callbackUser, function (result, callbackUser) { + exports.find_user( fields[0], function (result) { if (result == false) { // find out if the user_name is taken - exports.find_user_name( fields[1], callbackUser, function (docs) { + exports.find_user_name( fields[1], function (docs) { if (docs == false) { // if not ... // continue console.log("user name is valid"); @@ -599,7 +599,7 @@ exports.find_exam = function (fields, serverCallback, callback) { * Params: course_code - an string of format "CSC309" * title - the course description * */ -exports.add_course = function (course_code, title) { +exports.add_course = function (course_code, title, serverCallback) { var courseData = { course_code: course_code, @@ -609,6 +609,7 @@ exports.add_course = function (course_code, title) { exports.find_course(course_code, function (result) { if (result == true){ + serverCallback(false, "Course already exists"); console.log("course already exists"); } else if (result == false) { // add it @@ -621,7 +622,8 @@ exports.add_course = function (course_code, title) { courses.insert(courseData, function(err) { if (err) throw err; else { - console.log("couse added"); + console.log("course added"); + serverCallback(true, "Course added successfully."); db.close(function (err) { // close the connection when done if (err) throw err; }); @@ -656,8 +658,8 @@ exports.find_course = function (course_code, callback) { { course_code: course_code } ).toArray(function (err, docs) { if (err) throw err; - - if (docs.length == 0) { // if this course doesnt exist.... add it + // if this course doesnt exist.... add it (via add_course call) + if (docs.length == 0) { callback(false); } else { // course was found diff --git a/routes/admin.js b/routes/admin.js index c5cbcb4..9952218 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -1,3 +1,4 @@ +var dbFile = require("../node_simple.js"); var express = require('express'); var router = express.Router(); var csrf = require('csurf'); // Cross-Site Request Forgery prevention diff --git a/views/signup.hbs b/views/signup.hbs index 463e347..571363b 100644 --- a/views/signup.hbs +++ b/views/signup.hbs @@ -27,7 +27,7 @@ -

Sign Up

+

Sign Up

-- cgit v1.2.3 From 218b2c014b1ab68ee8289210820d7b6b742bceef Mon Sep 17 00:00:00 2001 From: nanalelfe Date: Fri, 22 Jul 2016 23:58:19 -0400 Subject: Populated user info into user_profile.hbs --- app.js | 2 + routes/user.js | 8 ++-- views/user_profile_alt.hbs | 97 +++++++++++++++++++++------------------------- 3 files changed, 49 insertions(+), 58 deletions(-) diff --git a/app.js b/app.js index 7421fcf..4f398a9 100644 --- a/app.js +++ b/app.js @@ -54,6 +54,8 @@ app.use(express.static(__dirname)); app.use(function(req, res, next) { res.locals.login = req.isAuthenticated(); // global variable res.locals.session = req.session; + res.locals.user = req.user; + console.log(res.locals.user); next(); }); diff --git a/routes/user.js b/routes/user.js index 840930a..014a7d2 100644 --- a/routes/user.js +++ b/routes/user.js @@ -49,10 +49,11 @@ router.post('/signup', loggedOut, function(req, res, next) { req.check('password', "Password should be between 6 and 12 characters.") .notEmpty().withMessage('Password required').isLength({min: 6, max: 12}); req.check('password', "The confirmation password doesn't match.").equals(req.body.confirmPassword); + + // phone number optional if (req.body.phone_num){ req.check('phone_num', 'Please enter a valid phone number').isMobilePhone('en-CA'); } - // password has to be at least 4 characters long var errors = req.validationErrors(); if (errors) { @@ -68,14 +69,11 @@ router.post('/signup', loggedOut, function(req, res, next) { })(req, res); } - //res.redirect('/signup'); - }); router.post('/signin', loggedOut, function(req, res, next) { req.check('usrname', 'Username field is empty.').notEmpty(); req.check('password', "Password field is empty.").notEmpty(); - // password has to be at least 4 characters long var errors = req.validationErrors(); if (errors) { @@ -89,9 +87,9 @@ router.post('/signin', loggedOut, function(req, res, next) { failureRedirect: '/user/signin', failureFlash: true })(req, res); - } + }); module.exports = router; diff --git a/views/user_profile_alt.hbs b/views/user_profile_alt.hbs index 8b102b8..413fd0f 100644 --- a/views/user_profile_alt.hbs +++ b/views/user_profile_alt.hbs @@ -32,7 +32,7 @@
-

Walter White

+

{{ user.f_name }} {{ user.l_name }}

@@ -47,78 +47,69 @@ - - + + + {{# if user.university }} + + + + + {{/if}} + {{# if user.department }} - + + {{/if}} - + - + - - - - - - - - - + + {{# if user.phone_num}} + + + + + {{/if}}
Univeristy:U of TEmail:{{ user.email }}
University:{{ user.university }}
Department:CS{{ user.department }}
Answered:50{{ user.answered }}
Messages:25{{ user.messages }}
Comments:100
Email:info@support.com
Phone Number:111-111-1111(Mobile){{ user.comments }}
Phone Number:{{ user.phone_num }}
-

You are following:

-
- - - - - + {{# if user.followers }} +

You are being followed by:

+
+
- avatar - -

The Dude

-

3 comments

-
+ + {{# each user.followers}} + + + - - - - - - - - - - - -
+ avatar + +

The Dude

+

3 comments

+
- avatar - -

He-Man

-

20 comments

-
- avatar - -

Lady Lips

-

56 comments

-
-
+ + {{/each}} + + +
+ {{ else }} +

You do not have any followers.

+ {{/if}}

Friends

-- cgit v1.2.3 From 0ccf1dc1b80fe2dff8f98538ffac673c6b326a57 Mon Sep 17 00:00:00 2001 From: HumairAK Date: Sat, 23 Jul 2016 00:12:11 -0400 Subject: Added admin cpanel functionality, added serverCallback in parameters for some database queries. --- assets/css/style.css | 8 ++ assets/js/script.js | 45 ++++++++ node_simple.js | 14 ++- routes/admin.js | 48 ++++++++- routes/index.js | 21 +--- routes/user.js | 2 +- views/admin.hbs | 284 +++++++++++++++++++++++++++++++++++---------------- 7 files changed, 304 insertions(+), 118 deletions(-) diff --git a/assets/css/style.css b/assets/css/style.css index eba03be..7b1538c 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -451,3 +451,11 @@ section.exam-info em { background: #80cca0; padding: 5px; } +/** Admin C-Panel **/ +.cpnl-details{ + text-align: left; +} + +.cpnl-container{ + padding: 4%; +} \ No newline at end of file diff --git a/assets/js/script.js b/assets/js/script.js index e752f0c..59fb358 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -49,6 +49,51 @@ function main(){ } }); + // ********* Admin Panel Scripts *********** // + var adminOptions = [ + $("#cpnl-addExam"), + $("#cpnl-addCourse"), + $("#cpnl-removeExam"), + $("#cpnl-removeCourse"), + $("#cpnl-removeUser")]; + + function hideCpnlPages(){ + adminOptions.forEach(function(item){ + item.hide(); + }); + } + + hideCpnlPages(); + + // Show General page + adminOptions[0].show(); + $('.cpnl-page').bind('click', function(e){ + + e.preventDefault(); + hideCpnlPages(); + + /* Each case will need to be changed to call a handler for fetching data + * from server*/ + switch($(this).text()) { + case "Add Exam": + $("#cpnl-addExam").show(); + break; + case "Add Course": + $("#cpnl-addCourse").show(); + break; + case "Remove Exam": + $("#cpnl-removeExam").show(); + break; + case "Remove Course": + $("#cpnl-removeCourse").show(); + break; + case "Remove User": + $("#cpnl-removeUser").show(); + break; + default: + console.log("Error: User page not found"); + } + }); // --- nav scripts --- diff --git a/node_simple.js b/node_simple.js index cd6d06f..cf06e61 100644 --- a/node_simple.js +++ b/node_simple.js @@ -112,11 +112,11 @@ exports.add_user = function (fields, callbackUser) { }; // find out if this user already exists by checking their email - exports.find_user( fields[0], function (result) { + exports.find_user( fields[0] ,callbackUser, function (result, callbackUser) { if (result == false) { // find out if the user_name is taken - exports.find_user_name( fields[1], function (docs) { + exports.find_user_name( fields[1], callbackUser, function (docs) { if (docs == false) { // if not ... // continue console.log("user name is valid"); @@ -682,7 +682,7 @@ exports.find_course = function (course_code, callback) { * Params: fields - an array of format ["course_code", year, "term", "type"] * * */ -exports.remove_exam = function (fields) { +exports.remove_exam = function (fields, serverCallback) { var course_code = fields[0]; var year = fields[1]; @@ -707,10 +707,14 @@ exports.remove_exam = function (fields) { if (err) throw err; else { if (docs.deletedCount == 1) { - console.log("exam was removed"); + serverCallback(true, "Exam was removed successfully"); + db.close(); + //console.log("exam was removed"); } else if (docs.deletedCount == 0) { - console.log("No such exam was found"); + serverCallback(false, "No such exam was found"); + db.close(); + //console.log("No such exam was found"); } } } diff --git a/routes/admin.js b/routes/admin.js index 9952218..35b208b 100644 --- a/routes/admin.js +++ b/routes/admin.js @@ -12,7 +12,7 @@ router.get('/', function(req,res){ }); /* Adds exam from front-end */ -router.post('/update/exam', function(req,res){ +router.post('/add/exam', function(req,res){ var course_code = req.body.course_code, year = req.body.year, type = req.body.type, @@ -27,8 +27,8 @@ router.post('/update/exam', function(req,res){ var fields = [ course_code, // String year, // Int - type, // String; Needs to be added to database code term, // String + type, // String instructors, // Array of strings page_count, // Int questions_count, // Int @@ -43,11 +43,11 @@ router.post('/update/exam', function(req,res){ } console.log(statusMessage); }); - res.redirect('/'); + res.redirect('/admin'); }); /* Adds course from front-end */ -router.post('/update/course', function(req,res){ +router.post('/add/course', function(req,res){ var course_code = req.body.course_code, title = req.body.title; @@ -59,8 +59,46 @@ router.post('/update/course', function(req,res){ } console.log("Status message: " + statusMessage) }); - res.redirect('/'); + res.redirect('/admin'); }); +router.post('/remove/exam', function(req,res){ + var course_code = req.body.course_code, + year = req.body.year, + term = req.body.term, + type = req.body.type, + campus = req.body.campus; + + var fields = [course_code, year, term, type]; + console.log(fields); + dbFile.remove_exam(fields, + function(examRemoved, statusMessage){ + if(examRemoved){ + console.log("Success."); + } else { + console.log("Failed."); + } + console.log(statusMessage); + }); + + res.redirect('/admin'); +}); + + + +/* Takes an input string delimited by commas, will split by comma and trim white + * spaces. Consider callback. + */ +function parseStringArray(input){ + var list = input.split(','); + var parsedList = []; + list.forEach(function(word){ + if (word != ""){ + parsedList.push(word.trim()); + } + }); + return parsedList; +} + module.exports = router; diff --git a/routes/index.js b/routes/index.js index 30189da..ac8e9a8 100644 --- a/routes/index.js +++ b/routes/index.js @@ -19,11 +19,11 @@ router.get('/user_solutions', function(req, res, next) { res.render('user_solutions'); }); -/* Render/GET questions page */ +/* Render/GET questions page router.get('/questions', function(req, res, next) { res.render('questions'); }); - +*/ //EXAMPLE EXPECTED DATA GIVEN BELOW: /*[ {courseCode: 'CSC240', @@ -86,8 +86,9 @@ router.get('/search', function(req, res, next) { router.get('/questions/:exam_id', function (req,res) { console.log(req.params.exam_id); dbFile.get_exam_info_by_ID(req.params.exam_id, function (questions) { - res.render('questions', {query: questions}); console.log(questions); + res.render('questions', {query: questions}); + }); }); @@ -144,18 +145,4 @@ function toProperCase(string) { return string.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); } -/* Takes an input string delimited by commas, will split by comma and trim white - * spaces. Consider callback. - */ -function parseStringArray(input){ - var list = input.split(','); - var parsedList = []; - list.forEach(function(word){ - if (word != ""){ - parsedList.push(word.trim()); - } - }); - return parsedList; -} - module.exports = router; \ No newline at end of file diff --git a/routes/user.js b/routes/user.js index 698c4f8..39b41d2 100644 --- a/routes/user.js +++ b/routes/user.js @@ -56,7 +56,7 @@ router.post('/signup', loggedOut, function(req, res, next) { if (errors) { req.session.errors = errors; req.session.success = false; - res.redirect('/signup'); + res.redirect('/user/signup'); } else { console.log("GOT SUCCESS"); passport.authenticate('local_signup', { diff --git a/views/admin.hbs b/views/admin.hbs index da33f41..36a3210 100644 --- a/views/admin.hbs +++ b/views/admin.hbs @@ -1,102 +1,206 @@
-
\ No newline at end of file -- cgit v1.2.3