Skip to content

Commit

Permalink
Retrieve modules and display them
Browse files Browse the repository at this point in the history
Note: using literal queries as a placeholder
  • Loading branch information
yeojoey committed Mar 17, 2017
1 parent 7280534 commit 93f7935
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 12 deletions.
48 changes: 43 additions & 5 deletions source/controller/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,60 @@ var forceSyncIVLE = function (req, res, next) {
* @param uid
* @returns JSON
*/


var getTutorials = function (req, res, next) {
if (req.body.auth.success) {
var user = req.body.auth.decoded;
console.log(user.id);
Tutorial.getTutorialsession(user.id).catch(function (err) {
var tuts = [];


Tutorial.findAllTutorialInfoOfUser(user.id).catch(function (err) {
res.json({success: false, message: err});
}).then(function (data) {
res.json({success: true, result: 'Found tutorials'});
console.log(data[0].dataValues);
tuts = data;
res.json({success: true, message: 'Success', data: tuts});
});
// Tutorial.findTutorialSession(user.id).catch(function (err) {
// res.json({success: false, message: err});
// }).then(function (data) {

// var promises = [];

// for (i = 0; i < data.length; i++) {

// var tut = (data[i].dataValues);

// var tutInfo = {};

// console.log(tut.tutorialId);

// // Get tutorial details
// promises.push(
// Tutorial.findTutorialInfo(tut.tutorialId).catch(function (err) {
// res.json({success: false, message: err});
// }).then(function (data) {
// tutInfo = data[0].dataValues;
// console.log(tutInfo);
// tuts.push(tutInfo);
// })
// );



// }

// }).then(function () {
// console.log(111111);
// console.log(tuts);
// res.json({success: false, message: 'Success', data: tuts});

// });
} else {
res.send("Permission denied");
}
}


module.exports.get = get;
module.exports.forceSyncIVLE = forceSyncIVLE;
module.exports.getTutorials = getTutorials;
44 changes: 43 additions & 1 deletion source/models/Tutorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,53 @@ var findTutorialSession = function (uid) {
};


/**
* Find tutorial info by tutorial id
* @param tid
* @returns {Promise}
*/
var findTutorialInfo = function (tid) {
return tutorial.findAll({
where: {
id: tid
}
});
}

/**
* Finds all tutorial info of a user's tutorials
* @param uid
* @returns [{tutinfo}, {tutinfo} ...] <- SHOULD RETURN PROMISE
*/

var findAllTutorialInfoOfUser = function (uid) {
// LITERAL QUERY!!! NOT GOOD! CHANGE LATER!
return sequelize.query("SELECT * FROM tutorials WHERE id IN (SELECT tutorialId FROM userTutorials WHERE userId = 'a0127127')").
spread(function (results, metadata) {
return results;
});
// TODO: SUBQUERIES???
// SELECT * FROM tutorials WHERE id IN (SELECT tutorialId FROM userTutorials WHERE userId = 'a0127127');
// return tutorial.findAll({
// where: {
// id: {
// $in: {

// }
// }
// }
// });
}



module.exports = tutorial;
module.exports.forceSyncIVLE = forceSyncIVLE;
module.exports.findTutorial = findTutorial;
module.exports.findAndCountAllTutorials = findAndCountAllTutorials;
module.exports.findTutorialSession = findTutorialSession;
module.exports.findTutorialInfo = findTutorialInfo;
module.exports.findTutorialTutorID = findTutorialTutorID;
module.exports.checkIfInTutorialUserList = checkIfInTutorialUserList;
module.exports.findAndCountAllUsersInTutorial = findAndCountAllUsersInTutorial;
module.exports.findAndCountAllUsersInTutorial = findAndCountAllUsersInTutorial;
module.exports.findAllTutorialInfoOfUser = findAllTutorialInfoOfUser;
31 changes: 25 additions & 6 deletions source/view/dashboard.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
<script type="text/javascript" src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
</head>
<body>
User is: <%= user.name %>
<br>

<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<input id="uid" type="hidden" value="<%= user.id %>">

<button id="sync-ivle">sync</button>
<button id="get-mods">get mods</button>
User is: <%= user.name %>
<br>
<h3>Modules</h3>
<div id="tutorials"></div>

<script type="application/javascript">
Expand All @@ -29,32 +32,48 @@ User is: <%= user.name %>
console.log("sync success");
}
else {
console.log('sync failed, err: '+data.message);
console.log('sync failed, err: ' +data.message);
}
}
});
}
function getTutorials() {
$.ajax({
type: 'POST',
url: '/api/dashboard/getTutorials',
data: { },
dataType: 'json',
success: function(data) {
showTutorials(data.data);
}
});
}
function showTutorials(tutorials) {
console.log(tutorials.toString());
for (i = 0; i < tutorials.length; i++) {
$('#tutorials').append(tutorials[i].coursecode + "<br>");
}
}
/*
$('#sync-ivle').click(function() {
syncIVLE();
});
$('#get-mods').click(function() {
getTutorials();
});
*/
syncIVLE();
getTutorials();
showTutorials();
</script>

Expand Down

0 comments on commit 93f7935

Please sign in to comment.