Skip to content

Commit

Permalink
can retrieve submission logs now
Browse files Browse the repository at this point in the history
  • Loading branch information
vkhoi committed May 11, 2017
1 parent f081fd7 commit c7fde0d
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 25 deletions.
17 changes: 17 additions & 0 deletions api/get-submission-logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
var express = require('express');
var router = express.Router();
var ensureAuthorized = require('../helpers/ensure-authorized');
var UserSubLog = require('../models/user-submission-log');

// Name: Get submission logs of user from database.
// Type: POST.
router.post('/', [], function(req, res) {
var username = req.body.username;
UserSubLog.getScore(username).then(function successCallback(scores) {
res.send({ scores: scores});
}, function errorCallback(err) {
res.status(500).send(err.toString());
});
});

module.exports = router;
1 change: 1 addition & 0 deletions api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ var router = express.Router();
router.use('/login', require('./login'));
router.use('/logout', require('./logout'));
router.use('/submit', require('./submit'));
router.use('/getSubmissionLogs', require('./get-submission-logs'));

module.exports = router;
9 changes: 9 additions & 0 deletions api/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ var User = require('../models/user');

var redisClient = redis.createClient();

// Verify if request is valid.
router.use(function(req, res, next) {
if (((req.body.username) && (req.body.password)) || (req.body.token))
next();
else {
res.status(400).send({ message: 'Bad request' });
}
});

// Name: Log in.
// Type: POST.
// Data: (username AND password) OR token.
Expand Down
10 changes: 10 additions & 0 deletions api/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ var redis = require('redis');

var redisClient = redis.createClient();

// Verify if request is valid.
router.use(function(req, res, next) {
if (req.body.token) {
next();
}
else {
res.status(400).send({ message: 'Bad request' });
}
});

// Name: Log out.
// Type: POST.
// Data: token.
Expand Down
17 changes: 14 additions & 3 deletions models/user-submission-log.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ function findUser(username, callback) {
// into the beginning of the name. (Ex: 123-[khoi][SEGMENT].cpp -> 123-cpp-[khoi][SEGMENT]).
// This helps insert new submission into the database easier.
function beautifyFilename(filename) {
console.log(filename);
var tokens = filename.split('.');
var tripped = tokens[0];
var ext = tokens[tokens.length - 1].toLowerCase();
Expand All @@ -64,7 +63,6 @@ function addSubmission(username, submissionName, fileContent) {

// Function to add new score.
function addScore(username, submissionName, score) {
console.log('addScore', submissionName);
findUser(username, function(err, user) {
var beautifulName = beautifyFilename(submissionName);
UserSubLog.update({ _id: user._id }, {
Expand All @@ -78,8 +76,21 @@ function addScore(username, submissionName, score) {
});
}

// Function to get all submissions and scores for a user.
function getScore(username) {
return new Promise(function(resolve, reject) {
findUser(username, function(err, user) {
if (err) reject(Error('Could not retrieve scores'));
else {
resolve(user.scores);
}
});
});
}

module.exports = {
addUser: addUser,
addSubmission: addSubmission,
addScore: addScore
addScore: addScore,
getScore: getScore
};
59 changes: 41 additions & 18 deletions public/controllers/home-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,50 @@ themisApp.controller('HomeController', ['$scope', '$state', '$http', 'AuthServic
vm.problems = ["SEGMENT", "ANT", "POLYGON"];
vm.selectedProblem = "SEGMENT";

vm.submissionLogs = [
{
id: 1,
problem: "SEGMENT",
score: 50
},
{
id: 2,
problem: "SEGMENT",
score: 70
},
{
id: 3,
problem: "SEGMENT",
score: 80
}
];
vm.submissionLogs = [];

function padNumber(number, L) {
var res = number.toString();
while (res.length < L)
res = "0" + res;
return res;
}

function timeToDate(timeStamp) {
var d = new Date(timeStamp);
return d.getFullYear() + "-" + padNumber(d.getDate(), 2) + "-" + padNumber(d.getMonth() + 1, 2) + " " + padNumber(d.getHours(), 2) + ":" + padNumber(d.getMinutes(), 2) + ":" + padNumber(d.getSeconds(), 2);
}

function getSubmissionLogs() {
$http.post('/api/getSubmissionLogs', { username: vm.username} ).then(function successCallback(res) {
var scores = res.data.scores;
Object.keys(scores).forEach(function(key) {
var timeStamp = key.split('-')[0];
var score = scores[key];
var problem = key.split('[').pop();
problem = problem.slice(0, -1);

vm.submissionLogs.push({
time: parseInt(timeStamp),
problem: problem,
score: score
});
});

vm.submissionLogs.sort(function(a, b) {
return b.time - a.time;
});
var N = vm.submissionLogs.length;
for (var i = 0; i < N; i++) {
vm.submissionLogs[i].id = N - i;
vm.submissionLogs[i].time = timeToDate(vm.submissionLogs[i].time);
}
});
}

vm.init = function() {
vm.username = Session.username;
getSubmissionLogs();
}
vm.init();

Expand All @@ -45,7 +69,6 @@ themisApp.controller('HomeController', ['$scope', '$state', '$http', 'AuthServic
}

vm.submit = function() {
console.log(vm.file.name);
Upload.upload({
url: '/api/submit',
data: {
Expand Down
10 changes: 6 additions & 4 deletions public/html/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,17 @@ <h3>Các lần bạn nộp</h3>
<thead>
<tr>
<th class="col-sm-1">#</th>
<th class="col-sm-8">Tên bài</th>
<th class="col-sm-3 text-center">Điểm</th>
<th class="col-sm-2">Vào lúc</th>
<th class="col-sm-7">Tên bài</th>
<th class="col-sm-2 text-center">Điểm</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="log in HomeCtrl.submissionLogs">
<td class="col-sm-1">{{ log.id }}</td>
<td class="col-sm-8">{{ log.problem }}</td>
<td class="col-sm-3 text-center">{{ log.score }}</td>
<td class="col-sm-2">{{ log.time }}</td>
<td class="col-sm-7">{{ log.problem }}</td>
<td class="col-sm-2 text-center">{{ log.score }}</td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit c7fde0d

Please sign in to comment.